blob: dd0f1abdacd256128515da151919304e8881bcb2 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200319 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
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
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200936 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400937 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
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001234 nolock = btrfs_is_free_space_inode(BTRFS_I(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;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001673 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(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;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001723 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(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)
Nikolay Borisov691fa052017-02-20 13:50:42 +02001740 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001741
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
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001857 if (btrfs_is_free_space_inode(BTRFS_I(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
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002796 nolock = btrfs_is_free_space_inode(BTRFS_I(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)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002917 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2918 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002919 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002920 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002921
Josef Bacik77cef2e2013-08-29 13:57:21 -04002922 if (ret || truncated) {
2923 u64 start, end;
2924
2925 if (truncated)
2926 start = ordered_extent->file_offset + logical_len;
2927 else
2928 start = ordered_extent->file_offset;
2929 end = ordered_extent->file_offset + ordered_extent->len - 1;
2930 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2931
2932 /* Drop the cache for the part of the extent we didn't write. */
2933 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002934
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002935 /*
2936 * If the ordered extent had an IOERR or something else went
2937 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002938 * back to the allocator. We only free the extent in the
2939 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002940 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002941 if ((ret || !logical_len) &&
2942 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002943 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002944 btrfs_free_reserved_extent(fs_info,
2945 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002946 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002947 }
2948
2949
Josef Bacik5fd02042012-05-02 14:00:54 -04002950 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002951 * This needs to be done to make sure anybody waiting knows we are done
2952 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 */
2954 btrfs_remove_ordered_extent(inode, ordered_extent);
2955
Liu Bo38c227d2013-01-29 03:18:40 +00002956 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002957 if (new) {
2958 if (ret) {
2959 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002961 } else {
2962 relink_file_extents(new);
2963 }
2964 }
Liu Bo38c227d2013-01-29 03:18:40 +00002965
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966 /* once for us */
2967 btrfs_put_ordered_extent(ordered_extent);
2968 /* once for the tree */
2969 btrfs_put_ordered_extent(ordered_extent);
2970
Josef Bacik5fd02042012-05-02 14:00:54 -04002971 return ret;
2972}
2973
2974static void finish_ordered_fn(struct btrfs_work *work)
2975{
2976 struct btrfs_ordered_extent *ordered_extent;
2977 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2978 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002979}
2980
Christoph Hellwigb2950862008-12-02 09:54:17 -05002981static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002982 struct extent_state *state, int uptodate)
2983{
Josef Bacik5fd02042012-05-02 14:00:54 -04002984 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002985 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002986 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002987 struct btrfs_workqueue *wq;
2988 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002989
liubo1abe9b82011-03-24 11:18:59 +00002990 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2991
Chris Mason8b62b722009-09-02 16:53:46 -04002992 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002993 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2994 end - start + 1, uptodate))
2995 return 0;
2996
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002997 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002998 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002999 func = btrfs_freespace_write_helper;
3000 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003002 func = btrfs_endio_write_helper;
3003 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003004
Liu Bo9e0af232014-08-15 23:36:53 +08003005 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3006 NULL);
3007 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003008
3009 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003010}
3011
Miao Xiedc380ae2014-09-12 18:43:55 +08003012static int __readpage_endio_check(struct inode *inode,
3013 struct btrfs_io_bio *io_bio,
3014 int icsum, struct page *page,
3015 int pgoff, u64 start, size_t len)
3016{
3017 char *kaddr;
3018 u32 csum_expected;
3019 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003020
3021 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3022
3023 kaddr = kmap_atomic(page);
3024 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003025 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003026 if (csum != csum_expected)
3027 goto zeroit;
3028
3029 kunmap_atomic(kaddr);
3030 return 0;
3031zeroit:
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003032 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3033 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003034 memset(kaddr + pgoff, 1, len);
3035 flush_dcache_page(page);
3036 kunmap_atomic(kaddr);
3037 if (csum_expected == 0)
3038 return 0;
3039 return -EIO;
3040}
3041
Chris Masond352ac62008-09-29 15:18:18 -04003042/*
Chris Masond352ac62008-09-29 15:18:18 -04003043 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003044 * if there's a match, we allow the bio to finish. If not, the code in
3045 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003046 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003047static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3048 u64 phy_offset, struct page *page,
3049 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003050{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003051 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003052 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003053 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003054 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003055
Chris Masond20f7042008-12-08 16:58:54 -05003056 if (PageChecked(page)) {
3057 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003058 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003059 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003060
3061 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003062 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003063
Yan Zheng17d217f2008-12-12 10:03:38 -05003064 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003065 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003066 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003067 return 0;
3068 }
3069
Miao Xiefacc8a222013-07-25 19:22:34 +08003070 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003071 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3072 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003073}
Chris Masonb888db2b2007-08-27 16:49:44 -04003074
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003075void btrfs_add_delayed_iput(struct inode *inode)
3076{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003077 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003078 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003079
3080 if (atomic_add_unless(&inode->i_count, -1, 1))
3081 return;
3082
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003083 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003084 if (binode->delayed_iput_count == 0) {
3085 ASSERT(list_empty(&binode->delayed_iput));
3086 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3087 } else {
3088 binode->delayed_iput_count++;
3089 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003090 spin_unlock(&fs_info->delayed_iput_lock);
3091}
3092
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003093void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003096 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003097 while (!list_empty(&fs_info->delayed_iputs)) {
3098 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003099
David Sterba8089fe62015-11-19 14:15:51 +01003100 inode = list_first_entry(&fs_info->delayed_iputs,
3101 struct btrfs_inode, delayed_iput);
3102 if (inode->delayed_iput_count) {
3103 inode->delayed_iput_count--;
3104 list_move_tail(&inode->delayed_iput,
3105 &fs_info->delayed_iputs);
3106 } else {
3107 list_del_init(&inode->delayed_iput);
3108 }
3109 spin_unlock(&fs_info->delayed_iput_lock);
3110 iput(&inode->vfs_inode);
3111 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003112 }
David Sterba8089fe62015-11-19 14:15:51 +01003113 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003114}
3115
Yan, Zhengd68fc572010-05-16 10:49:58 -04003116/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003117 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003118 * files in the subvolume, it removes orphan item and frees block_rsv
3119 * structure.
3120 */
3121void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3122 struct btrfs_root *root)
3123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003124 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003125 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003126 int ret;
3127
Josef Bacik8a35d952012-05-23 14:26:42 -04003128 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003129 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3130 return;
3131
Josef Bacik90290e12011-12-02 15:44:12 -05003132 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003133 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003134 spin_unlock(&root->orphan_lock);
3135 return;
3136 }
3137
3138 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3139 spin_unlock(&root->orphan_lock);
3140 return;
3141 }
3142
3143 block_rsv = root->orphan_block_rsv;
3144 root->orphan_block_rsv = NULL;
3145 spin_unlock(&root->orphan_lock);
3146
Miao Xie27cdeb72014-04-02 19:51:05 +08003147 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003148 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003149 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003150 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003151 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003152 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003153 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003154 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3155 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003156 }
3157
Josef Bacik90290e12011-12-02 15:44:12 -05003158 if (block_rsv) {
3159 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003160 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003161 }
3162}
3163
3164/*
Josef Bacik7b128762008-07-24 12:17:14 -04003165 * This creates an orphan entry for the given inode in case something goes
3166 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 *
3168 * NOTE: caller of this function should reserve 5 units of metadata for
3169 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003170 */
3171int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3172{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003174 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003175 struct btrfs_block_rsv *block_rsv = NULL;
3176 int reserve = 0;
3177 int insert = 0;
3178 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003179
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003181 block_rsv = btrfs_alloc_block_rsv(fs_info,
3182 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003183 if (!block_rsv)
3184 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003185 }
3186
Yan, Zhengd68fc572010-05-16 10:49:58 -04003187 spin_lock(&root->orphan_lock);
3188 if (!root->orphan_block_rsv) {
3189 root->orphan_block_rsv = block_rsv;
3190 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003191 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 block_rsv = NULL;
3193 }
Josef Bacik7b128762008-07-24 12:17:14 -04003194
Josef Bacik8a35d952012-05-23 14:26:42 -04003195 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3196 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197#if 0
3198 /*
3199 * For proper ENOSPC handling, we should do orphan
3200 * cleanup when mounting. But this introduces backward
3201 * compatibility issue.
3202 */
3203 if (!xchg(&root->orphan_item_inserted, 1))
3204 insert = 2;
3205 else
3206 insert = 1;
3207#endif
3208 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003209 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 }
Josef Bacik7b128762008-07-24 12:17:14 -04003211
Josef Bacik72ac3c02012-05-23 14:13:11 -04003212 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3213 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003215 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003216
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 /* grab metadata reservation from transaction handle */
3218 if (reserve) {
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02003219 ret = btrfs_orphan_reserve_metadata(trans, BTRFS_I(inode));
Josef Bacik3b6571c2016-05-27 13:03:04 -04003220 ASSERT(!ret);
3221 if (ret) {
3222 atomic_dec(&root->orphan_inodes);
3223 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3224 &BTRFS_I(inode)->runtime_flags);
3225 if (insert)
3226 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3227 &BTRFS_I(inode)->runtime_flags);
3228 return ret;
3229 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 }
3231
3232 /* insert an orphan item to track this unlinked/truncated file */
3233 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003234 ret = btrfs_insert_orphan_item(trans, root,
3235 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003236 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003237 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003238 if (reserve) {
3239 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3240 &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov703b3912017-02-20 13:50:40 +02003241 btrfs_orphan_release_metadata(BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003242 }
3243 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003244 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3245 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003246 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003247 return ret;
3248 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 }
3250 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
3252
3253 /* insert an orphan item to track subvolume contains orphan files */
3254 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003255 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003257 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003258 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003259 return ret;
3260 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003261 }
3262 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003263}
3264
3265/*
3266 * We have done the truncate/delete so we can go ahead and remove the orphan
3267 * item for this particular inode.
3268 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003269static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3270 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003271{
3272 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 int delete_item = 0;
3274 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003275 int ret = 0;
3276
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003278 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3279 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003281
Josef Bacik72ac3c02012-05-23 14:13:11 -04003282 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3283 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 spin_unlock(&root->orphan_lock);
3286
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003287 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003288 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003289 if (trans)
3290 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003291 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 }
Josef Bacik7b128762008-07-24 12:17:14 -04003293
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003294 if (release_rsv)
Nikolay Borisov703b3912017-02-20 13:50:40 +02003295 btrfs_orphan_release_metadata(BTRFS_I(inode));
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003296
Josef Bacik4ef31a42013-08-13 14:10:08 -04003297 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003298}
3299
3300/*
3301 * this cleans up any orphans that may be left on the list from the last use
3302 * of this root.
3303 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003304int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003307 struct btrfs_path *path;
3308 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 struct btrfs_key key, found_key;
3310 struct btrfs_trans_handle *trans;
3311 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003312 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003313 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3314
Yan, Zhengd68fc572010-05-16 10:49:58 -04003315 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003316 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003317
3318 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 if (!path) {
3320 ret = -ENOMEM;
3321 goto out;
3322 }
David Sterbae4058b52015-11-27 16:31:35 +01003323 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003324
3325 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003326 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 key.offset = (u64)-1;
3328
Josef Bacik7b128762008-07-24 12:17:14 -04003329 while (1) {
3330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 if (ret < 0)
3332 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
3334 /*
3335 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003336 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003337 * find the key and see if we have stuff that matches
3338 */
3339 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 if (path->slots[0] == 0)
3342 break;
3343 path->slots[0]--;
3344 }
3345
3346 /* pull out the item */
3347 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3349
3350 /* make sure the item matches what we want */
3351 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3352 break;
David Sterba962a2982014-06-04 18:41:45 +02003353 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003354 break;
3355
3356 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003357 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 /*
3360 * this is where we are basically btrfs_lookup, without the
3361 * crossing root thing. we store the inode number in the
3362 * offset of the orphan item.
3363 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364
3365 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003366 btrfs_err(fs_info,
3367 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003368 ret = -EINVAL;
3369 goto out;
3370 }
3371
3372 last_objectid = found_key.offset;
3373
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 found_key.objectid = found_key.offset;
3375 found_key.type = BTRFS_INODE_ITEM_KEY;
3376 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003377 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303378 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003379 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003381
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003382 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003383 struct btrfs_root *dead_root;
3384 struct btrfs_fs_info *fs_info = root->fs_info;
3385 int is_dead_root = 0;
3386
3387 /*
3388 * this is an orphan in the tree root. Currently these
3389 * could come from 2 sources:
3390 * a) a snapshot deletion in progress
3391 * b) a free space cache inode
3392 * We need to distinguish those two, as the snapshot
3393 * orphan must not get deleted.
3394 * find_dead_roots already ran before us, so if this
3395 * is a snapshot deletion, we should find the root
3396 * in the dead_roots list
3397 */
3398 spin_lock(&fs_info->trans_lock);
3399 list_for_each_entry(dead_root, &fs_info->dead_roots,
3400 root_list) {
3401 if (dead_root->root_key.objectid ==
3402 found_key.objectid) {
3403 is_dead_root = 1;
3404 break;
3405 }
3406 }
3407 spin_unlock(&fs_info->trans_lock);
3408 if (is_dead_root) {
3409 /* prevent this orphan from being found again */
3410 key.offset = found_key.objectid - 1;
3411 continue;
3412 }
3413 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003414 /*
3415 * Inode is already gone but the orphan item is still there,
3416 * kill the orphan item.
3417 */
Filipe Manana67710892016-06-06 11:51:25 +01003418 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003419 trans = btrfs_start_transaction(root, 1);
3420 if (IS_ERR(trans)) {
3421 ret = PTR_ERR(trans);
3422 goto out;
3423 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003424 btrfs_debug(fs_info, "auto deleting %Lu",
3425 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 ret = btrfs_del_orphan_item(trans, root,
3427 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003428 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003429 if (ret)
3430 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003432 }
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Josef Bacik7b128762008-07-24 12:17:14 -04003434 /*
3435 * add this inode to the orphan list so btrfs_orphan_del does
3436 * the proper thing when we hit it
3437 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003438 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3439 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003440 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003441
Josef Bacik7b128762008-07-24 12:17:14 -04003442 /* if we have links, this was a truncate, lets do that */
3443 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303444 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003445 iput(inode);
3446 continue;
3447 }
Josef Bacik7b128762008-07-24 12:17:14 -04003448 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003449
3450 /* 1 for the orphan item deletion. */
3451 trans = btrfs_start_transaction(root, 1);
3452 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003453 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003454 ret = PTR_ERR(trans);
3455 goto out;
3456 }
3457 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003458 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003459 if (ret) {
3460 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003462 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003463
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003465 if (ret)
3466 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003467 } else {
3468 nr_unlink++;
3469 }
3470
3471 /* this will do delete_inode and everything for us */
3472 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 if (ret)
3474 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 }
Miao Xie3254c872011-11-10 20:45:05 -05003476 /* release the path since we're done with it */
3477 btrfs_release_path(path);
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3480
3481 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003482 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003483 (u64)-1);
3484
Miao Xie27cdeb72014-04-02 19:51:05 +08003485 if (root->orphan_block_rsv ||
3486 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003487 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003489 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003490 }
Josef Bacik7b128762008-07-24 12:17:14 -04003491
3492 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003493 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003494 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003495 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003496
3497out:
3498 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003499 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 btrfs_free_path(path);
3501 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003502}
3503
Chris Masond352ac62008-09-29 15:18:18 -04003504/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003505 * very simple check to peek ahead in the leaf looking for xattrs. If we
3506 * don't find any xattrs, we know there can't be any acls.
3507 *
3508 * slot is the slot the inode is in, objectid is the objectid of the inode
3509 */
3510static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003511 int slot, u64 objectid,
3512 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003513{
3514 u32 nritems = btrfs_header_nritems(leaf);
3515 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003516 static u64 xattr_access = 0;
3517 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003518 int scanned = 0;
3519
Josef Bacikf23b5a52013-06-19 10:16:26 -04003520 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003521 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3522 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3523 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3524 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003525 }
3526
Chris Mason46a53cc2009-04-27 11:47:50 -04003527 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003528 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003529 while (slot < nritems) {
3530 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3531
3532 /* we found a different objectid, there must not be acls */
3533 if (found_key.objectid != objectid)
3534 return 0;
3535
3536 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003537 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003538 if (*first_xattr_slot == -1)
3539 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003540 if (found_key.offset == xattr_access ||
3541 found_key.offset == xattr_default)
3542 return 1;
3543 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003544
3545 /*
3546 * we found a key greater than an xattr key, there can't
3547 * be any acls later on
3548 */
3549 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3550 return 0;
3551
3552 slot++;
3553 scanned++;
3554
3555 /*
3556 * it goes inode, inode backrefs, xattrs, extents,
3557 * so if there are a ton of hard links to an inode there can
3558 * be a lot of backrefs. Don't waste time searching too hard,
3559 * this is just an optimization
3560 */
3561 if (scanned >= 8)
3562 break;
3563 }
3564 /* we hit the end of the leaf before we found an xattr or
3565 * something larger than an xattr. We have to assume the inode
3566 * has acls
3567 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568 if (*first_xattr_slot == -1)
3569 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003570 return 1;
3571}
3572
3573/*
Chris Masond352ac62008-09-29 15:18:18 -04003574 * read an inode from the btree into the in-memory inode
3575 */
Filipe Manana67710892016-06-06 11:51:25 +01003576static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003579 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 struct btrfs_inode_item *inode_item;
3582 struct btrfs_root *root = BTRFS_I(inode)->root;
3583 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003584 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003586 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003587 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003590
3591 ret = btrfs_fill_inode(inode, &rdev);
3592 if (!ret)
3593 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003594
3595 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003596 if (!path) {
3597 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003598 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003599 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003600
Chris Mason39279cc2007-06-12 06:35:45 -04003601 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003602
Chris Mason39279cc2007-06-12 06:35:45 -04003603 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003604 if (ret) {
3605 if (ret > 0)
3606 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003607 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003608 }
Chris Mason39279cc2007-06-12 06:35:45 -04003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003611
3612 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003613 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3616 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003617 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003618 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003619 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3620 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003621 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003622
David Sterbaa937b972014-12-12 17:39:12 +01003623 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3624 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003625
David Sterbaa937b972014-12-12 17:39:12 +01003626 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3627 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
David Sterbaa937b972014-12-12 17:39:12 +01003629 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3630 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
chandan r9cc97d62012-07-04 12:48:07 +05303632 BTRFS_I(inode)->i_otime.tv_sec =
3633 btrfs_timespec_sec(leaf, &inode_item->otime);
3634 BTRFS_I(inode)->i_otime.tv_nsec =
3635 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003637 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003638 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003639 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3640
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003641 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003642 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003643 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003644 rdev = btrfs_inode_rdev(leaf, inode_item);
3645
Josef Bacikaec74772008-07-24 12:12:38 -04003646 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003647 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003648
3649cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003650 /*
3651 * If we were modified in the current generation and evicted from memory
3652 * and then re-read we need to do a full sync since we don't have any
3653 * idea about which extents were modified before we were evicted from
3654 * cache.
3655 *
3656 * This is required for both inode re-read from disk and delayed inode
3657 * in delayed_nodes_tree.
3658 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003659 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003660 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3661 &BTRFS_I(inode)->runtime_flags);
3662
Filipe Mananabde6c242015-07-24 00:00:19 +01003663 /*
3664 * We don't persist the id of the transaction where an unlink operation
3665 * against the inode was last made. So here we assume the inode might
3666 * have been evicted, and therefore the exact value of last_unlink_trans
3667 * lost, and set it to last_trans to avoid metadata inconsistencies
3668 * between the inode and its parent if the inode is fsync'ed and the log
3669 * replayed. For example, in the scenario:
3670 *
3671 * touch mydir/foo
3672 * ln mydir/foo mydir/bar
3673 * sync
3674 * unlink mydir/bar
3675 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3676 * xfs_io -c fsync mydir/foo
3677 * <power failure>
3678 * mount fs, triggers fsync log replay
3679 *
3680 * We must make sure that when we fsync our inode foo we also log its
3681 * parent inode, otherwise after log replay the parent still has the
3682 * dentry with the "bar" name but our inode foo has a link count of 1
3683 * and doesn't have an inode ref with the name "bar" anymore.
3684 *
3685 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003686 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003687 * transaction commits on fsync if our inode is a directory, or if our
3688 * inode is not a directory, logging its parent unnecessarily.
3689 */
3690 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3691
Miao Xie67de1172013-12-26 13:07:06 +08003692 path->slots[0]++;
3693 if (inode->i_nlink != 1 ||
3694 path->slots[0] >= btrfs_header_nritems(leaf))
3695 goto cache_acl;
3696
3697 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003698 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003699 goto cache_acl;
3700
3701 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3702 if (location.type == BTRFS_INODE_REF_KEY) {
3703 struct btrfs_inode_ref *ref;
3704
3705 ref = (struct btrfs_inode_ref *)ptr;
3706 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3707 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3708 struct btrfs_inode_extref *extref;
3709
3710 extref = (struct btrfs_inode_extref *)ptr;
3711 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3712 extref);
3713 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003714cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003715 /*
3716 * try to precache a NULL acl entry for files that don't have
3717 * any xattrs or acls
3718 */
Li Zefan33345d012011-04-20 10:31:50 +08003719 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003720 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003721 if (first_xattr_slot != -1) {
3722 path->slots[0] = first_xattr_slot;
3723 ret = btrfs_load_inode_props(inode, path);
3724 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003725 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003726 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003727 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003728 root->root_key.objectid, ret);
3729 }
3730 btrfs_free_path(path);
3731
Al Viro72c04902009-06-24 16:58:48 -04003732 if (!maybe_acls)
3733 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003734
Chris Mason39279cc2007-06-12 06:35:45 -04003735 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003736 case S_IFREG:
3737 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 inode->i_fop = &btrfs_file_operations;
3740 inode->i_op = &btrfs_file_inode_operations;
3741 break;
3742 case S_IFDIR:
3743 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003744 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003745 break;
3746 case S_IFLNK:
3747 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003748 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003749 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3750 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003751 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003752 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003753 init_special_inode(inode, inode->i_mode, rdev);
3754 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003756
3757 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003758 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003759
3760make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003761 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003762 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003763 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003764}
3765
Chris Masond352ac62008-09-29 15:18:18 -04003766/*
3767 * given a leaf and an inode, copy the inode fields into the leaf
3768 */
Chris Masone02119d2008-09-05 16:13:11 -04003769static void fill_inode_item(struct btrfs_trans_handle *trans,
3770 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003771 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003772 struct inode *inode)
3773{
Liu Bo51fab692012-12-27 09:01:21 +00003774 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003775
Liu Bo51fab692012-12-27 09:01:21 +00003776 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003777
Liu Bo51fab692012-12-27 09:01:21 +00003778 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3779 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3780 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3781 &token);
3782 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3783 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
David Sterbaa937b972014-12-12 17:39:12 +01003785 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003786 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003787 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003788 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003792 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003793 inode->i_mtime.tv_nsec, &token);
3794
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003797 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003798 inode->i_ctime.tv_nsec, &token);
3799
chandan r9cc97d62012-07-04 12:48:07 +05303800 btrfs_set_token_timespec_sec(leaf, &item->otime,
3801 BTRFS_I(inode)->i_otime.tv_sec, &token);
3802 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3803 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3804
Liu Bo51fab692012-12-27 09:01:21 +00003805 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3806 &token);
3807 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3808 &token);
3809 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3810 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3811 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3812 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3813 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003814}
3815
Chris Masond352ac62008-09-29 15:18:18 -04003816/*
3817 * copy everything in the in-memory inode into the btree.
3818 */
Chris Mason21151332011-11-10 20:39:08 -05003819static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003820 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003821{
3822 struct btrfs_inode_item *inode_item;
3823 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003824 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003825 int ret;
3826
3827 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003828 if (!path)
3829 return -ENOMEM;
3830
Chris Masonb9473432009-03-13 11:00:37 -04003831 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003832 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3833 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003834 if (ret) {
3835 if (ret > 0)
3836 ret = -ENOENT;
3837 goto failed;
3838 }
3839
Chris Mason5f39d392007-10-15 16:14:19 -04003840 leaf = path->nodes[0];
3841 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003842 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003843
Chris Masone02119d2008-09-05 16:13:11 -04003844 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003845 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003846 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003847 ret = 0;
3848failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003849 btrfs_free_path(path);
3850 return ret;
3851}
3852
Chris Masond352ac62008-09-29 15:18:18 -04003853/*
Chris Mason21151332011-11-10 20:39:08 -05003854 * copy everything in the in-memory inode into the btree.
3855 */
3856noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3857 struct btrfs_root *root, struct inode *inode)
3858{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003859 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003860 int ret;
3861
3862 /*
3863 * If the inode is a free space inode, we can deadlock during commit
3864 * if we put it into the delayed code.
3865 *
3866 * The data relocation inode should also be directly updated
3867 * without delay
3868 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003869 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003870 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003871 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003872 btrfs_update_root_times(trans, root);
3873
Chris Mason21151332011-11-10 20:39:08 -05003874 ret = btrfs_delayed_update_inode(trans, root, inode);
3875 if (!ret)
3876 btrfs_set_inode_last_trans(trans, inode);
3877 return ret;
3878 }
3879
3880 return btrfs_update_inode_item(trans, root, inode);
3881}
3882
Josef Bacikbe6aef62012-10-22 15:43:12 -04003883noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3884 struct btrfs_root *root,
3885 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003886{
3887 int ret;
3888
3889 ret = btrfs_update_inode(trans, root, inode);
3890 if (ret == -ENOSPC)
3891 return btrfs_update_inode_item(trans, root, inode);
3892 return ret;
3893}
3894
3895/*
Chris Masond352ac62008-09-29 15:18:18 -04003896 * unlink helper that gets used here in inode.c and in the tree logging
3897 * recovery code. It remove a link in a directory with a given name, and
3898 * also drops the back refs in the inode to the directory
3899 */
Al Viro92986792011-03-04 17:14:37 +00003900static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003902 struct btrfs_inode *dir,
3903 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003904 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003905{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003906 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003909 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003911 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003912 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003913 u64 ino = btrfs_ino(inode);
3914 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003915
3916 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003917 if (!path) {
3918 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003919 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003920 }
3921
Chris Masonb9473432009-03-13 11:00:37 -04003922 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003923 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003924 name, name_len, -1);
3925 if (IS_ERR(di)) {
3926 ret = PTR_ERR(di);
3927 goto err;
3928 }
3929 if (!di) {
3930 ret = -ENOENT;
3931 goto err;
3932 }
Chris Mason5f39d392007-10-15 16:14:19 -04003933 leaf = path->nodes[0];
3934 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003935 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003936 if (ret)
3937 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003938 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003939
Miao Xie67de1172013-12-26 13:07:06 +08003940 /*
3941 * If we don't have dir index, we have to get it by looking up
3942 * the inode ref, since we get the inode ref, remove it directly,
3943 * it is unnecessary to do delayed deletion.
3944 *
3945 * But if we have dir index, needn't search inode ref to get it.
3946 * Since the inode ref is close to the inode item, it is better
3947 * that we delay to delete it, and just do this deletion when
3948 * we update the inode item.
3949 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003950 if (inode->dir_index) {
3951 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003952 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003953 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003954 goto skip_backref;
3955 }
3956 }
3957
Li Zefan33345d012011-04-20 10:31:50 +08003958 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3959 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003960 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003961 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003962 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003963 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003964 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003965 goto err;
3966 }
Miao Xie67de1172013-12-26 13:07:06 +08003967skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003968 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003970 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003971 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003972 }
Chris Mason39279cc2007-06-12 06:35:45 -04003973
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003974 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3975 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003977 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003978 goto err;
3979 }
Chris Masone02119d2008-09-05 16:13:11 -04003980
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003981 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3982 index);
Chris Mason6418c962010-10-30 07:34:24 -04003983 if (ret == -ENOENT)
3984 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003985 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003986 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003987err:
3988 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003989 if (ret)
3990 goto out;
3991
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003992 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003993 inode_inc_iversion(&inode->vfs_inode);
3994 inode_inc_iversion(&dir->vfs_inode);
3995 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3996 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3997 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003998out:
Chris Mason39279cc2007-06-12 06:35:45 -04003999 return ret;
4000}
4001
Al Viro92986792011-03-04 17:14:37 +00004002int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004004 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004005 const char *name, int name_len)
4006{
4007 int ret;
4008 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4009 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004010 drop_nlink(&inode->vfs_inode);
4011 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004012 }
4013 return ret;
4014}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004015
4016/*
4017 * helper to start transaction for unlink and rmdir.
4018 *
Josef Bacikd52be812013-05-29 14:54:47 -04004019 * unlink and rmdir are special in btrfs, they do not always free space, so
4020 * if we cannot make our reservations the normal way try and see if there is
4021 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4022 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 */
Josef Bacikd52be812013-05-29 14:54:47 -04004024static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027
Josef Bacike70bea52011-10-11 14:18:24 -04004028 /*
4029 * 1 for the possible orphan item
4030 * 1 for the dir item
4031 * 1 for the dir index
4032 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004033 * 1 for the inode
4034 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004035 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004036}
4037
Chris Mason39279cc2007-06-12 06:35:45 -04004038static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4039{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004042 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 int ret;
4044
Josef Bacikd52be812013-05-29 14:54:47 -04004045 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004047 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004048
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004049 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4050 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004051
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004052 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4053 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4054 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004055 if (ret)
4056 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004057
Yan, Zhenga22285a2010-05-16 10:48:46 -04004058 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004059 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004060 if (ret)
4061 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 }
Josef Bacik7b128762008-07-24 12:17:14 -04004063
Tsutomu Itohb5324022011-07-19 07:27:20 +00004064out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004065 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004066 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004067 return ret;
4068}
4069
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4071 struct btrfs_root *root,
4072 struct inode *dir, u64 objectid,
4073 const char *name, int name_len)
4074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004075 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076 struct btrfs_path *path;
4077 struct extent_buffer *leaf;
4078 struct btrfs_dir_item *di;
4079 struct btrfs_key key;
4080 u64 index;
4081 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004082 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004083
4084 path = btrfs_alloc_path();
4085 if (!path)
4086 return -ENOMEM;
4087
Li Zefan33345d012011-04-20 10:31:50 +08004088 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 if (IS_ERR_OR_NULL(di)) {
4091 if (!di)
4092 ret = -ENOENT;
4093 else
4094 ret = PTR_ERR(di);
4095 goto out;
4096 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097
4098 leaf = path->nodes[0];
4099 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4100 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4101 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004102 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004103 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 goto out;
4105 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004106 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4109 root->root_key.objectid, dir_ino,
4110 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004113 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 goto out;
4115 }
Li Zefan33345d012011-04-20 10:31:50 +08004116 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 if (IS_ERR_OR_NULL(di)) {
4119 if (!di)
4120 ret = -ENOENT;
4121 else
4122 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124 goto out;
4125 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004126
4127 leaf = path->nodes[0];
4128 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004129 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130 index = key.offset;
4131 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004132 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004134 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137 goto out;
4138 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004139
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004140 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004141 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004142 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004143 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004144 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004145 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004147 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149}
4150
Chris Mason39279cc2007-06-12 06:35:45 -04004151static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4152{
David Howells2b0143b2015-03-17 22:25:59 +00004153 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004154 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004156 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004157 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004158
David Sterbab3ae2442012-09-13 16:04:34 -06004159 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004160 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004161 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004162 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004163
Josef Bacikd52be812013-05-29 14:54:47 -04004164 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004166 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004167
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004168 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169 err = btrfs_unlink_subvol(trans, root, dir,
4170 BTRFS_I(inode)->location.objectid,
4171 dentry->d_name.name,
4172 dentry->d_name.len);
4173 goto out;
4174 }
4175
Josef Bacik7b128762008-07-24 12:17:14 -04004176 err = btrfs_orphan_add(trans, inode);
4177 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004179
Filipe Manana44f714d2016-06-06 16:11:13 +01004180 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4181
Chris Mason39279cc2007-06-12 06:35:45 -04004182 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004183 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4184 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4185 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004186 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004187 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004188 /*
4189 * Propagate the last_unlink_trans value of the deleted dir to
4190 * its parent directory. This is to prevent an unrecoverable
4191 * log tree in the case we do something like this:
4192 * 1) create dir foo
4193 * 2) create snapshot under dir foo
4194 * 3) delete the snapshot
4195 * 4) rmdir foo
4196 * 5) mkdir foo
4197 * 6) fsync foo or some file inside foo
4198 */
4199 if (last_unlink_trans >= trans->transid)
4200 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4201 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004203 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004204 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004205
Chris Mason39279cc2007-06-12 06:35:45 -04004206 return err;
4207}
4208
Chris Mason28f75a02015-02-04 06:59:29 -08004209static int truncate_space_check(struct btrfs_trans_handle *trans,
4210 struct btrfs_root *root,
4211 u64 bytes_deleted)
4212{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004213 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004214 int ret;
4215
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004216 /*
4217 * This is only used to apply pressure to the enospc system, we don't
4218 * intend to use this reservation at all.
4219 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004220 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004221 bytes_deleted *= fs_info->nodesize;
4222 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004223 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004224 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004226 trans->transid,
4227 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004228 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004229 }
Chris Mason28f75a02015-02-04 06:59:29 -08004230 return ret;
4231
4232}
4233
Filipe Manana0305cd52015-10-16 12:34:25 +01004234static int truncate_inline_extent(struct inode *inode,
4235 struct btrfs_path *path,
4236 struct btrfs_key *found_key,
4237 const u64 item_end,
4238 const u64 new_size)
4239{
4240 struct extent_buffer *leaf = path->nodes[0];
4241 int slot = path->slots[0];
4242 struct btrfs_file_extent_item *fi;
4243 u32 size = (u32)(new_size - found_key->offset);
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4245
4246 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4247
4248 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4249 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004250 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004251
4252 /*
4253 * Zero out the remaining of the last page of our inline extent,
4254 * instead of directly truncating our inline extent here - that
4255 * would be much more complex (decompressing all the data, then
4256 * compressing the truncated data, which might be bigger than
4257 * the size of the inline extent, resize the extent, etc).
4258 * We release the path because to get the page we might need to
4259 * read the extent item from disk (data not in the page cache).
4260 */
4261 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304262 return btrfs_truncate_block(inode, offset, page_end - offset,
4263 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004264 }
4265
4266 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4267 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004268 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004269
4270 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4271 inode_sub_bytes(inode, item_end + 1 - new_size);
4272
4273 return 0;
4274}
4275
Chris Mason323ac952008-10-01 19:05:46 -04004276/*
Chris Mason39279cc2007-06-12 06:35:45 -04004277 * this can truncate away extent items, csum items and directory items.
4278 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004279 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004280 *
4281 * csum items that cross the new i_size are truncated to the new size
4282 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004283 *
4284 * min_type is the minimum key type to truncate down to. If set to 0, this
4285 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004286 */
Yan, Zheng80825102009-11-12 09:35:36 +00004287int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4288 struct btrfs_root *root,
4289 struct inode *inode,
4290 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004291{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004292 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004294 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004296 struct btrfs_key key;
4297 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004298 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004299 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004301 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004302 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004303 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004304 int found_extent;
4305 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004306 int pending_del_nr = 0;
4307 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004308 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004309 int ret;
4310 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004311 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004312 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004313 bool be_nice = 0;
4314 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004315 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004316
4317 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004318
Chris Mason28ed1342014-12-17 09:41:04 -08004319 /*
4320 * for non-free space inodes and ref cows, we want to back off from
4321 * time to time
4322 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004323 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004324 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4325 be_nice = 1;
4326
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004327 path = btrfs_alloc_path();
4328 if (!path)
4329 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004330 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004331
Josef Bacik5dc562c2012-08-17 13:14:17 -04004332 /*
4333 * We want to drop from the next block forward in case this new size is
4334 * not block aligned since we will be keeping the last block of the
4335 * extent just the way it is.
4336 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004337 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004338 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004339 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004340 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004341 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004342
Miao Xie16cdcec2011-04-22 18:12:22 +08004343 /*
4344 * This function is also used to drop the items in the log tree before
4345 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4346 * it is used to drop the loged items. So we shouldn't kill the delayed
4347 * items.
4348 */
4349 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004350 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004351
Li Zefan33345d012011-04-20 10:31:50 +08004352 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004353 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004354 key.type = (u8)-1;
4355
Chris Mason85e21ba2008-01-29 15:11:36 -05004356search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004357 /*
4358 * with a 16K leaf size and 128MB extents, you can actually queue
4359 * up a huge file in a single leaf. Most of the time that
4360 * bytes_deleted is > 0, it will be huge by the time we get here
4361 */
Byongho Leeee221842015-12-15 01:42:10 +09004362 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004363 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004364 err = -EAGAIN;
4365 goto error;
4366 }
4367 }
4368
4369
Chris Masonb9473432009-03-13 11:00:37 -04004370 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004371 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004372 if (ret < 0) {
4373 err = ret;
4374 goto out;
4375 }
Chris Masond3977122009-01-05 21:25:51 -05004376
Chris Mason85e21ba2008-01-29 15:11:36 -05004377 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004378 /* there are no items in the tree for us to truncate, we're
4379 * done
4380 */
Yan, Zheng80825102009-11-12 09:35:36 +00004381 if (path->slots[0] == 0)
4382 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004383 path->slots[0]--;
4384 }
4385
Chris Masond3977122009-01-05 21:25:51 -05004386 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004387 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004388 leaf = path->nodes[0];
4389 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004390 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004391
Li Zefan33345d012011-04-20 10:31:50 +08004392 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004393 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004394
Chris Mason85e21ba2008-01-29 15:11:36 -05004395 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004396 break;
4397
Chris Mason5f39d392007-10-15 16:14:19 -04004398 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004399 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004400 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004401 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004402 extent_type = btrfs_file_extent_type(leaf, fi);
4403 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004404 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004405 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004406 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004407 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004408 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004409 }
Yan008630c2007-11-07 13:31:09 -05004410 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 }
Yan, Zheng80825102009-11-12 09:35:36 +00004412 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004413 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004414 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004415 if (item_end < new_size) {
4416 /*
4417 * With NO_HOLES mode, for the following mapping
4418 *
4419 * [0-4k][hole][8k-12k]
4420 *
4421 * if truncating isize down to 6k, it ends up
4422 * isize being 8k.
4423 */
4424 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4425 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004426 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004427 }
Yan, Zheng80825102009-11-12 09:35:36 +00004428 if (found_key.offset >= new_size)
4429 del_item = 1;
4430 else
4431 del_item = 0;
4432 }
Chris Mason39279cc2007-06-12 06:35:45 -04004433 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004434 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004435 if (found_type != BTRFS_EXTENT_DATA_KEY)
4436 goto delete;
4437
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004438 if (del_item)
4439 last_size = found_key.offset;
4440 else
4441 last_size = new_size;
4442
Chris Mason179e29e2007-11-01 11:28:41 -04004443 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004444 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004445 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004446 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004447 u64 orig_num_bytes =
4448 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004449 extent_num_bytes = ALIGN(new_size -
4450 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004451 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004452 btrfs_set_file_extent_num_bytes(leaf, fi,
4453 extent_num_bytes);
4454 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004455 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004456 if (test_bit(BTRFS_ROOT_REF_COWS,
4457 &root->state) &&
4458 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004459 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004460 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 } else {
Chris Masondb945352007-10-15 16:15:53 -04004462 extent_num_bytes =
4463 btrfs_file_extent_disk_num_bytes(leaf,
4464 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004465 extent_offset = found_key.offset -
4466 btrfs_file_extent_offset(leaf, fi);
4467
Chris Mason39279cc2007-06-12 06:35:45 -04004468 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004469 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004470 if (extent_start != 0) {
4471 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004472 if (test_bit(BTRFS_ROOT_REF_COWS,
4473 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004474 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004475 }
4476 }
Chris Mason90692182008-02-08 13:49:28 -05004477 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004478 /*
4479 * we can't truncate inline items that have had
4480 * special encodings
4481 */
4482 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004483 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4484 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004485
4486 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004487 * Need to release path in order to truncate a
4488 * compressed extent. So delete any accumulated
4489 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004490 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004491 if (btrfs_file_extent_compression(leaf, fi) !=
4492 BTRFS_COMPRESS_NONE && pending_del_nr) {
4493 err = btrfs_del_items(trans, root, path,
4494 pending_del_slot,
4495 pending_del_nr);
4496 if (err) {
4497 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004498 err);
4499 goto error;
4500 }
4501 pending_del_nr = 0;
4502 }
4503
4504 err = truncate_inline_extent(inode, path,
4505 &found_key,
4506 item_end,
4507 new_size);
4508 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004509 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004510 goto error;
4511 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004512 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4513 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004514 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004515 }
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
Chris Mason179e29e2007-11-01 11:28:41 -04004517delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004518 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004519 if (!pending_del_nr) {
4520 /* no pending yet, add ourselves */
4521 pending_del_slot = path->slots[0];
4522 pending_del_nr = 1;
4523 } else if (pending_del_nr &&
4524 path->slots[0] + 1 == pending_del_slot) {
4525 /* hop on the pending chunk */
4526 pending_del_nr++;
4527 pending_del_slot = path->slots[0];
4528 } else {
Chris Masond3977122009-01-05 21:25:51 -05004529 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004530 }
Chris Mason39279cc2007-06-12 06:35:45 -04004531 } else {
4532 break;
4533 }
Chris Mason28f75a02015-02-04 06:59:29 -08004534 should_throttle = 0;
4535
Miao Xie27cdeb72014-04-02 19:51:05 +08004536 if (found_extent &&
4537 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004538 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004539 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004540 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004541 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004542 extent_num_bytes, 0,
4543 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004544 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004546 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4547 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004548 trans->delayed_ref_updates * 2,
4549 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004550 if (be_nice) {
4551 if (truncate_space_check(trans, root,
4552 extent_num_bytes)) {
4553 should_end = 1;
4554 }
4555 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004556 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004557 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004558 }
Chris Mason39279cc2007-06-12 06:35:45 -04004559 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004560
Yan, Zheng80825102009-11-12 09:35:36 +00004561 if (found_type == BTRFS_INODE_ITEM_KEY)
4562 break;
4563
4564 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004565 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004566 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004567 if (pending_del_nr) {
4568 ret = btrfs_del_items(trans, root, path,
4569 pending_del_slot,
4570 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004571 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004572 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004573 goto error;
4574 }
Yan, Zheng80825102009-11-12 09:35:36 +00004575 pending_del_nr = 0;
4576 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004577 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004578 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004579 unsigned long updates = trans->delayed_ref_updates;
4580 if (updates) {
4581 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004582 ret = btrfs_run_delayed_refs(trans,
4583 fs_info,
4584 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004585 if (ret && !err)
4586 err = ret;
4587 }
4588 }
Chris Mason28f75a02015-02-04 06:59:29 -08004589 /*
4590 * if we failed to refill our space rsv, bail out
4591 * and let the transaction restart
4592 */
4593 if (should_end) {
4594 err = -EAGAIN;
4595 goto error;
4596 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004597 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004598 } else {
4599 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004600 }
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
Yan, Zheng80825102009-11-12 09:35:36 +00004602out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004603 if (pending_del_nr) {
4604 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4605 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004606 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004607 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004608 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004609error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004610 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004611 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004612
Chris Mason39279cc2007-06-12 06:35:45 -04004613 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004614
Liu Bo19fd2df2016-12-01 13:01:02 -08004615 if (err == 0) {
4616 /* only inline file may have last_size != new_size */
4617 if (new_size >= fs_info->sectorsize ||
4618 new_size > fs_info->max_inline)
4619 ASSERT(last_size == new_size);
4620 }
4621
Byongho Leeee221842015-12-15 01:42:10 +09004622 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004623 unsigned long updates = trans->delayed_ref_updates;
4624 if (updates) {
4625 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004626 ret = btrfs_run_delayed_refs(trans, fs_info,
4627 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004628 if (ret && !err)
4629 err = ret;
4630 }
4631 }
Yan, Zheng80825102009-11-12 09:35:36 +00004632 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004633}
4634
Chris Masona52d9a82007-08-27 16:49:44 -04004635/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304636 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004637 * @inode - inode that we're zeroing
4638 * @from - the offset to start zeroing
4639 * @len - the length to zero, 0 to zero the entire range respective to the
4640 * offset
4641 * @front - zero up to the offset instead of from the offset on
4642 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304643 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004644 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004645 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304646int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004647 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004648{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004650 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004651 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4652 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004653 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004654 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004655 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004656 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304657 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004658 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004659 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004660 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304661 u64 block_start;
4662 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004663
Josef Bacik2aaa6652012-08-29 14:27:18 -04004664 if ((offset & (blocksize - 1)) == 0 &&
4665 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004666 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304667
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004668 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304669 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004670 if (ret)
4671 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004672
Chris Mason211c17f2008-05-15 09:13:45 -04004673again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004674 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004675 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004676 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304677 round_down(from, blocksize),
4678 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004679 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004680 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004681 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004682
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304683 block_start = round_down(from, blocksize);
4684 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004685
Chris Masona52d9a82007-08-27 16:49:44 -04004686 if (!PageUptodate(page)) {
4687 ret = btrfs_readpage(NULL, page);
4688 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004689 if (page->mapping != mapping) {
4690 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004691 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004692 goto again;
4693 }
Chris Masona52d9a82007-08-27 16:49:44 -04004694 if (!PageUptodate(page)) {
4695 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004696 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004697 }
4698 }
Chris Mason211c17f2008-05-15 09:13:45 -04004699 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004700
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004702 set_page_extent_mapped(page);
4703
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304704 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004705 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004707 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004708 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004709 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004710 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004711 btrfs_put_ordered_extent(ordered);
4712 goto again;
4713 }
4714
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004716 EXTENT_DIRTY | EXTENT_DELALLOC |
4717 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004718 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004719
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004721 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004722 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304723 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004724 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004725 goto out_unlock;
4726 }
4727
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004729 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304730 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004731 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004732 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 memset(kaddr + (block_start - page_offset(page)),
4734 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004735 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 memset(kaddr + (block_start - page_offset(page)) + offset,
4737 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004738 flush_dcache_page(page);
4739 kunmap(page);
4740 }
Chris Mason247e7432008-07-17 12:53:51 -04004741 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004742 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004744 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004745
Chris Mason89642222008-07-24 09:41:53 -04004746out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004747 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304748 btrfs_delalloc_release_space(inode, block_start,
4749 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004750 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004751 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004752out:
4753 return ret;
4754}
4755
Josef Bacik16e75492013-10-22 12:18:51 -04004756static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4757 u64 offset, u64 len)
4758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004760 struct btrfs_trans_handle *trans;
4761 int ret;
4762
4763 /*
4764 * Still need to make sure the inode looks like it's been updated so
4765 * that any holes get logged if we fsync.
4766 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004767 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4768 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004769 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4770 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4771 return 0;
4772 }
4773
4774 /*
4775 * 1 - for the one we're dropping
4776 * 1 - for the one we're adding
4777 * 1 - for updating the inode.
4778 */
4779 trans = btrfs_start_transaction(root, 3);
4780 if (IS_ERR(trans))
4781 return PTR_ERR(trans);
4782
4783 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4784 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004785 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004786 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004787 return ret;
4788 }
4789
David Sterbaf85b7372017-01-20 14:54:07 +01004790 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4791 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004792 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004793 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004794 else
4795 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004796 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004797 return ret;
4798}
4799
Josef Bacik695a0d02011-03-04 15:46:53 -05004800/*
4801 * This function puts in dummy file extents for the area we're creating a hole
4802 * for. So if we are truncating this file to a larger size we need to insert
4803 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4804 * the range between oldsize and size
4805 */
Josef Bacika41ad392011-01-31 15:30:16 -05004806int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004809 struct btrfs_root *root = BTRFS_I(inode)->root;
4810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004811 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004812 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004813 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004814 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4815 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004816 u64 last_byte;
4817 u64 cur_offset;
4818 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004819 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004820
Josef Bacika71754f2013-06-17 17:14:39 -04004821 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 * If our size started in the middle of a block we need to zero out the
4823 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004824 * expose stale data.
4825 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004827 if (err)
4828 return err;
4829
Yan Zheng9036c102008-10-30 14:19:41 -04004830 if (size <= hole_start)
4831 return 0;
4832
Yan Zheng9036c102008-10-30 14:19:41 -04004833 while (1) {
4834 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004835
David Sterbaff13db42015-12-03 14:30:40 +01004836 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004837 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004838 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4839 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004840 if (!ordered)
4841 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4843 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004844 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004845 btrfs_put_ordered_extent(ordered);
4846 }
4847
Yan Zheng9036c102008-10-30 14:19:41 -04004848 cur_offset = hole_start;
4849 while (1) {
4850 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4851 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004852 if (IS_ERR(em)) {
4853 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004854 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004855 break;
4856 }
Yan Zheng9036c102008-10-30 14:19:41 -04004857 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004858 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004859 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004860 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004861 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004862
Josef Bacik16e75492013-10-22 12:18:51 -04004863 err = maybe_insert_hole(root, inode, cur_offset,
4864 hole_size);
4865 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004866 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004867 btrfs_drop_extent_cache(inode, cur_offset,
4868 cur_offset + hole_size - 1, 0);
4869 hole_em = alloc_extent_map();
4870 if (!hole_em) {
4871 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4872 &BTRFS_I(inode)->runtime_flags);
4873 goto next;
4874 }
4875 hole_em->start = cur_offset;
4876 hole_em->len = hole_size;
4877 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004878
Josef Bacik5dc562c2012-08-17 13:14:17 -04004879 hole_em->block_start = EXTENT_MAP_HOLE;
4880 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004881 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004882 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004884 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004886
4887 while (1) {
4888 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004889 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004890 write_unlock(&em_tree->lock);
4891 if (err != -EEXIST)
4892 break;
4893 btrfs_drop_extent_cache(inode, cur_offset,
4894 cur_offset +
4895 hole_size - 1, 0);
4896 }
4897 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004898 }
Josef Bacik16e75492013-10-22 12:18:51 -04004899next:
Yan Zheng9036c102008-10-30 14:19:41 -04004900 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004901 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004902 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004903 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004904 break;
4905 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004906 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004907 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4908 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004909 return err;
4910}
4911
Eric Sandeen3972f262013-01-12 02:57:22 +00004912static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004913{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004914 struct btrfs_root *root = BTRFS_I(inode)->root;
4915 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004916 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004917 loff_t newsize = attr->ia_size;
4918 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004919 int ret;
4920
Eric Sandeen3972f262013-01-12 02:57:22 +00004921 /*
4922 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4923 * special case where we need to update the times despite not having
4924 * these flags set. For all other operations the VFS set these flags
4925 * explicitly if it wants a timestamp update.
4926 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004927 if (newsize != oldsize) {
4928 inode_inc_iversion(inode);
4929 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4930 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004931 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004932 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004933
Josef Bacika41ad392011-01-31 15:30:16 -05004934 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004935 /*
4936 * Don't do an expanding truncate while snapshoting is ongoing.
4937 * This is to ensure the snapshot captures a fully consistent
4938 * state of this file - if the snapshot captures this expanding
4939 * truncation, it must capture all writes that happened before
4940 * this truncation.
4941 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004942 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004943 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004944 if (ret) {
4945 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004946 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004947 }
Yan, Zheng80825102009-11-12 09:35:36 +00004948
Miao Xief4a2f4c2011-12-14 20:12:01 -05004949 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004950 if (IS_ERR(trans)) {
4951 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004952 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004953 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004954
4955 i_size_write(inode, newsize);
4956 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304957 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004958 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004959 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004960 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004961 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004962
Josef Bacika41ad392011-01-31 15:30:16 -05004963 /*
4964 * We're truncating a file that used to have good data down to
4965 * zero. Make sure it gets into the ordered flush list so that
4966 * any new writes get down to disk quickly.
4967 */
4968 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004969 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4970 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004971
Josef Bacikf3fe8202013-01-07 17:03:21 -05004972 /*
4973 * 1 for the orphan item we're going to add
4974 * 1 for the orphan item deletion.
4975 */
4976 trans = btrfs_start_transaction(root, 2);
4977 if (IS_ERR(trans))
4978 return PTR_ERR(trans);
4979
4980 /*
4981 * We need to do this in case we fail at _any_ point during the
4982 * actual truncate. Once we do the truncate_setsize we could
4983 * invalidate pages which forces any outstanding ordered io to
4984 * be instantly completed which will give us extents that need
4985 * to be truncated. If we fail to get an orphan inode down we
4986 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004987 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004988 * will be consistent.
4989 */
4990 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004991 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004992 if (ret)
4993 return ret;
4994
Josef Bacika41ad392011-01-31 15:30:16 -05004995 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4996 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004997
4998 /* Disable nonlocked read DIO to avoid the end less truncate */
4999 btrfs_inode_block_unlocked_dio(inode);
5000 inode_dio_wait(inode);
5001 btrfs_inode_resume_unlocked_dio(inode);
5002
Josef Bacika41ad392011-01-31 15:30:16 -05005003 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005004 if (ret && inode->i_nlink) {
5005 int err;
5006
Liu Bo19fd2df2016-12-01 13:01:02 -08005007 /* To get a stable disk_i_size */
5008 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5009 if (err) {
5010 btrfs_orphan_del(NULL, inode);
5011 return err;
5012 }
5013
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005014 /*
5015 * failed to truncate, disk_i_size is only adjusted down
5016 * as we remove extents, so it should represent the true
5017 * size of the inode, so reset the in memory size and
5018 * delete our orphan entry.
5019 */
5020 trans = btrfs_join_transaction(root);
5021 if (IS_ERR(trans)) {
5022 btrfs_orphan_del(NULL, inode);
5023 return ret;
5024 }
5025 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5026 err = btrfs_orphan_del(trans, inode);
5027 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005028 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005029 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005030 }
Yan, Zheng80825102009-11-12 09:35:36 +00005031 }
5032
Josef Bacika41ad392011-01-31 15:30:16 -05005033 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005034}
5035
Chris Mason39279cc2007-06-12 06:35:45 -04005036static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5037{
David Howells2b0143b2015-03-17 22:25:59 +00005038 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005039 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005040 int err;
5041
Li Zefanb83cc962010-12-20 16:04:08 +08005042 if (btrfs_root_readonly(root))
5043 return -EROFS;
5044
Jan Kara31051c82016-05-26 16:55:18 +02005045 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005046 if (err)
5047 return err;
5048
Chris Mason5a3f23d2009-03-31 13:27:11 -04005049 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005050 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005051 if (err)
5052 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005053 }
Yan Zheng9036c102008-10-30 14:19:41 -04005054
Christoph Hellwig10257742010-06-04 11:30:02 +02005055 if (attr->ia_valid) {
5056 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005057 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005058 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005059
Josef Bacik22c44fe2011-11-30 10:45:38 -05005060 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005061 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005062 }
5063
Chris Mason39279cc2007-06-12 06:35:45 -04005064 return err;
5065}
Chris Mason61295eb2008-01-14 16:24:38 -05005066
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005067/*
5068 * While truncating the inode pages during eviction, we get the VFS calling
5069 * btrfs_invalidatepage() against each page of the inode. This is slow because
5070 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5071 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5072 * extent_state structures over and over, wasting lots of time.
5073 *
5074 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5075 * those expensive operations on a per page basis and do only the ordered io
5076 * finishing, while we release here the extent_map and extent_state structures,
5077 * without the excessive merging and splitting.
5078 */
5079static void evict_inode_truncate_pages(struct inode *inode)
5080{
5081 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5082 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5083 struct rb_node *node;
5084
5085 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005086 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005087
5088 write_lock(&map_tree->lock);
5089 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5090 struct extent_map *em;
5091
5092 node = rb_first(&map_tree->map);
5093 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005094 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5095 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005096 remove_extent_mapping(map_tree, em);
5097 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005098 if (need_resched()) {
5099 write_unlock(&map_tree->lock);
5100 cond_resched();
5101 write_lock(&map_tree->lock);
5102 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005103 }
5104 write_unlock(&map_tree->lock);
5105
Filipe Manana6ca07092015-05-26 00:55:42 +01005106 /*
5107 * Keep looping until we have no more ranges in the io tree.
5108 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005109 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5110 * still in progress (unlocked the pages in the bio but did not yet
5111 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005112 * ranges can still be locked and eviction started because before
5113 * submitting those bios, which are executed by a separate task (work
5114 * queue kthread), inode references (inode->i_count) were not taken
5115 * (which would be dropped in the end io callback of each bio).
5116 * Therefore here we effectively end up waiting for those bios and
5117 * anyone else holding locked ranges without having bumped the inode's
5118 * reference count - if we don't do it, when they access the inode's
5119 * io_tree to unlock a range it may be too late, leading to an
5120 * use-after-free issue.
5121 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005122 spin_lock(&io_tree->lock);
5123 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5124 struct extent_state *state;
5125 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005126 u64 start;
5127 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005128
5129 node = rb_first(&io_tree->state);
5130 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005131 start = state->start;
5132 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005133 spin_unlock(&io_tree->lock);
5134
David Sterbaff13db42015-12-03 14:30:40 +01005135 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005136
5137 /*
5138 * If still has DELALLOC flag, the extent didn't reach disk,
5139 * and its reserved space won't be freed by delayed_ref.
5140 * So we need to free its reserved space here.
5141 * (Refer to comment in btrfs_invalidatepage, case 2)
5142 *
5143 * Note, end is the bytenr of last byte, so we need + 1 here.
5144 */
5145 if (state->state & EXTENT_DELALLOC)
5146 btrfs_qgroup_free_data(inode, start, end - start + 1);
5147
Filipe Manana6ca07092015-05-26 00:55:42 +01005148 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005149 EXTENT_LOCKED | EXTENT_DIRTY |
5150 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5151 EXTENT_DEFRAG, 1, 1,
5152 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005153
Filipe Manana7064dd52014-08-08 02:47:05 +01005154 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005155 spin_lock(&io_tree->lock);
5156 }
5157 spin_unlock(&io_tree->lock);
5158}
5159
Al Virobd555972010-06-07 11:35:40 -04005160void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 struct btrfs_trans_handle *trans;
5164 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005165 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005166 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005167 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005168 int ret;
5169
liubo1abe9b82011-03-24 11:18:59 +00005170 trace_btrfs_inode_evict(inode);
5171
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005172 if (!root) {
5173 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5174 return;
5175 }
5176
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005177 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005178
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 evict_inode_truncate_pages(inode);
5180
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005181 if (inode->i_nlink &&
5182 ((btrfs_root_refs(&root->root_item) != 0 &&
5183 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005184 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005185 goto no_delete;
5186
Chris Mason39279cc2007-06-12 06:35:45 -04005187 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005188 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005189 goto no_delete;
5190 }
Al Virobd555972010-06-07 11:35:40 -04005191 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005192 if (!special_file(inode->i_mode))
5193 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005194
Miao Xief6124962014-09-12 18:44:04 +08005195 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005197 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005198 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005199 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005200 goto no_delete;
5201 }
5202
Yan, Zheng76dda932009-09-21 16:00:26 -04005203 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005204 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5205 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005206 goto no_delete;
5207 }
5208
Nikolay Borisovaa790212017-01-10 20:35:40 +02005209 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005210 if (ret) {
5211 btrfs_orphan_del(NULL, inode);
5212 goto no_delete;
5213 }
5214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005215 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005216 if (!rsv) {
5217 btrfs_orphan_del(NULL, inode);
5218 goto no_delete;
5219 }
Josef Bacik4a338542011-08-29 11:01:31 -04005220 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005221 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005222 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005223
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005224 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005225
Josef Bacik4289a662011-08-05 13:22:24 -04005226 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005227 * This is a bit simpler than btrfs_truncate since we've already
5228 * reserved our space for our orphan item in the unlink, so we just
5229 * need to reserve some slack space in case we add bytes and update
5230 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005231 */
Yan, Zheng80825102009-11-12 09:35:36 +00005232 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005233 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5234 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005235
Josef Bacik726c35f2011-09-26 15:46:06 -04005236 /*
5237 * Try and steal from the global reserve since we will
5238 * likely not use this space anyway, we want to try as
5239 * hard as possible to get this to work.
5240 */
5241 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005242 steal_from_global++;
5243 else
5244 steal_from_global = 0;
5245 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005246
Josef Bacik3bce8762015-02-24 12:35:51 -08005247 /*
5248 * steal_from_global == 0: we reserved stuff, hooray!
5249 * steal_from_global == 1: we didn't reserve stuff, boo!
5250 * steal_from_global == 2: we've committed, still not a lot of
5251 * room but maybe we'll have room in the global reserve this
5252 * time.
5253 * steal_from_global == 3: abandon all hope!
5254 */
5255 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005256 btrfs_warn(fs_info,
5257 "Could not get space for a delete, will truncate on mount %d",
5258 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005259 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005260 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005261 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005262 }
5263
Miao Xie0e8c36a2012-12-19 06:59:51 +00005264 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005265 if (IS_ERR(trans)) {
5266 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005267 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005268 goto no_delete;
5269 }
5270
Josef Bacik3bce8762015-02-24 12:35:51 -08005271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005272 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005273 * sure there is room to do it, if not we need to commit and try
5274 * again.
5275 */
5276 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005277 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005278 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005279 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005280 else
5281 ret = -ENOSPC;
5282 }
5283
5284 /*
5285 * Couldn't steal from the global reserve, we have too much
5286 * pending stuff built up, commit the transaction and try it
5287 * again.
5288 */
5289 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005290 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005291 if (ret) {
5292 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005293 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005294 goto no_delete;
5295 }
5296 continue;
5297 } else {
5298 steal_from_global = 0;
5299 }
5300
Josef Bacik4289a662011-08-05 13:22:24 -04005301 trans->block_rsv = rsv;
5302
Yan, Zhengd68fc572010-05-16 10:49:58 -04005303 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005304 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005305 break;
5306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005308 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005309 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005310 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005311 }
5312
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005313 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005314
Josef Bacik4ef31a42013-08-13 14:10:08 -04005315 /*
5316 * Errors here aren't a big deal, it just means we leave orphan items
5317 * in the tree. They will be cleaned up on the next mount.
5318 */
Yan, Zheng80825102009-11-12 09:35:36 +00005319 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005320 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005321 btrfs_orphan_del(trans, inode);
5322 } else {
5323 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005324 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005325
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005326 trans->block_rsv = &fs_info->trans_block_rsv;
5327 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005328 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005329 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005330
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005331 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005332 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005333no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005334 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005335 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005336}
5337
5338/*
5339 * this returns the key found in the dir entry in the location pointer.
5340 * If no dir entries were found, location->objectid is 0.
5341 */
5342static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5343 struct btrfs_key *location)
5344{
5345 const char *name = dentry->d_name.name;
5346 int namelen = dentry->d_name.len;
5347 struct btrfs_dir_item *di;
5348 struct btrfs_path *path;
5349 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005350 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005351
5352 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005353 if (!path)
5354 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005355
David Sterbaf85b7372017-01-20 14:54:07 +01005356 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5357 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005358 if (IS_ERR(di))
5359 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005360
David Sterbac7040052011-04-19 18:00:01 +02005361 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005362 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005363
Chris Mason5f39d392007-10-15 16:14:19 -04005364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005365out:
Chris Mason39279cc2007-06-12 06:35:45 -04005366 btrfs_free_path(path);
5367 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005368out_err:
5369 location->objectid = 0;
5370 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005371}
5372
5373/*
5374 * when we hit a tree root in a directory, the btrfs part of the inode
5375 * needs to be changed to reflect the root directory of the tree root. This
5376 * is kind of like crossing a mount point.
5377 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005378static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005379 struct inode *dir,
5380 struct dentry *dentry,
5381 struct btrfs_key *location,
5382 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005383{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 struct btrfs_path *path;
5385 struct btrfs_root *new_root;
5386 struct btrfs_root_ref *ref;
5387 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005388 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 int ret;
5390 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005391
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005392 path = btrfs_alloc_path();
5393 if (!path) {
5394 err = -ENOMEM;
5395 goto out;
5396 }
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005399 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5400 key.type = BTRFS_ROOT_REF_KEY;
5401 key.offset = location->objectid;
5402
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005404 if (ret) {
5405 if (ret < 0)
5406 err = ret;
5407 goto out;
5408 }
Chris Mason39279cc2007-06-12 06:35:45 -04005409
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005410 leaf = path->nodes[0];
5411 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005412 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005413 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5414 goto out;
5415
5416 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5417 (unsigned long)(ref + 1),
5418 dentry->d_name.len);
5419 if (ret)
5420 goto out;
5421
David Sterbab3b4aa72011-04-21 01:20:15 +02005422 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005424 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425 if (IS_ERR(new_root)) {
5426 err = PTR_ERR(new_root);
5427 goto out;
5428 }
5429
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 *sub_root = new_root;
5431 location->objectid = btrfs_root_dirid(&new_root->root_item);
5432 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005433 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005434 err = 0;
5435out:
5436 btrfs_free_path(path);
5437 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005438}
5439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440static void inode_tree_add(struct inode *inode)
5441{
5442 struct btrfs_root *root = BTRFS_I(inode)->root;
5443 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005444 struct rb_node **p;
5445 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005446 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005447 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448
Al Viro1d3382cb2010-10-23 15:19:20 -04005449 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005450 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005451 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005452 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005453 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 while (*p) {
5455 parent = *p;
5456 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5457
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005458 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005459 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005460 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005461 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 else {
5463 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005464 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005465 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005466 RB_CLEAR_NODE(parent);
5467 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005468 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005469 }
5470 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005471 rb_link_node(new, parent, p);
5472 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473 spin_unlock(&root->inode_lock);
5474}
5475
5476static void inode_tree_del(struct inode *inode)
5477{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005478 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005479 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005480 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005482 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005486 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005488 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005489
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005490 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005491 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005492 spin_lock(&root->inode_lock);
5493 empty = RB_EMPTY_ROOT(&root->inode_tree);
5494 spin_unlock(&root->inode_lock);
5495 if (empty)
5496 btrfs_add_dead_root(root);
5497 }
5498}
5499
Jeff Mahoney143bede2012-03-01 14:56:26 +01005500void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005502 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005503 struct rb_node *node;
5504 struct rb_node *prev;
5505 struct btrfs_inode *entry;
5506 struct inode *inode;
5507 u64 objectid = 0;
5508
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005509 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005510 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005511
5512 spin_lock(&root->inode_lock);
5513again:
5514 node = root->inode_tree.rb_node;
5515 prev = NULL;
5516 while (node) {
5517 prev = node;
5518 entry = rb_entry(node, struct btrfs_inode, rb_node);
5519
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005520 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005522 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005523 node = node->rb_right;
5524 else
5525 break;
5526 }
5527 if (!node) {
5528 while (prev) {
5529 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005531 node = prev;
5532 break;
5533 }
5534 prev = rb_next(prev);
5535 }
5536 }
5537 while (node) {
5538 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005539 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005540 inode = igrab(&entry->vfs_inode);
5541 if (inode) {
5542 spin_unlock(&root->inode_lock);
5543 if (atomic_read(&inode->i_count) > 1)
5544 d_prune_aliases(inode);
5545 /*
Al Viro45321ac2010-06-07 13:43:19 -04005546 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005547 * the inode cache when its usage count
5548 * hits zero.
5549 */
5550 iput(inode);
5551 cond_resched();
5552 spin_lock(&root->inode_lock);
5553 goto again;
5554 }
5555
5556 if (cond_resched_lock(&root->inode_lock))
5557 goto again;
5558
5559 node = rb_next(node);
5560 }
5561 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005562}
5563
Chris Masone02119d2008-09-05 16:13:11 -04005564static int btrfs_init_locked_inode(struct inode *inode, void *p)
5565{
5566 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005567 inode->i_ino = args->location->objectid;
5568 memcpy(&BTRFS_I(inode)->location, args->location,
5569 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005570 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005571 return 0;
5572}
5573
5574static int btrfs_find_actor(struct inode *inode, void *opaque)
5575{
5576 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005577 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005578 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005579}
5580
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005582 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005584{
5585 struct inode *inode;
5586 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005587 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005588
Chris Mason90d3e592014-01-09 17:28:00 -08005589 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005590 args.root = root;
5591
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005592 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005593 btrfs_init_locked_inode,
5594 (void *)&args);
5595 return inode;
5596}
5597
Balaji Rao1a54ef82008-07-21 02:01:04 +05305598/* Get an inode object given its location and corresponding root.
5599 * Returns in *is_new if the inode was read from disk
5600 */
5601struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005602 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305603{
5604 struct inode *inode;
5605
Chris Mason90d3e592014-01-09 17:28:00 -08005606 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305607 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305609
5610 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005611 int ret;
5612
5613 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005614 if (!is_bad_inode(inode)) {
5615 inode_tree_add(inode);
5616 unlock_new_inode(inode);
5617 if (new)
5618 *new = 1;
5619 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005620 unlock_new_inode(inode);
5621 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005622 ASSERT(ret < 0);
5623 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005624 }
5625 }
5626
Balaji Rao1a54ef82008-07-21 02:01:04 +05305627 return inode;
5628}
5629
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630static struct inode *new_simple_dir(struct super_block *s,
5631 struct btrfs_key *key,
5632 struct btrfs_root *root)
5633{
5634 struct inode *inode = new_inode(s);
5635
5636 if (!inode)
5637 return ERR_PTR(-ENOMEM);
5638
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005639 BTRFS_I(inode)->root = root;
5640 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005641 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642
5643 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005644 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005645 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 inode->i_fop = &simple_dir_operations;
5647 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005648 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305649 inode->i_atime = inode->i_mtime;
5650 inode->i_ctime = inode->i_mtime;
5651 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652
5653 return inode;
5654}
5655
Chris Mason3de45862008-11-17 21:02:50 -05005656struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005657{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005658 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005659 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005660 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005661 struct btrfs_root *sub_root = root;
5662 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005664 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005665
5666 if (dentry->d_name.len > BTRFS_NAME_LEN)
5667 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005668
Jeff Layton39e3c952012-11-28 11:30:53 -05005669 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005670 if (ret < 0)
5671 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005672
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005673 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005674 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005675
5676 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005677 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005679 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005680
5681 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5682
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005683 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005684 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685 &location, &sub_root);
5686 if (ret < 0) {
5687 if (ret != -ENOENT)
5688 inode = ERR_PTR(ret);
5689 else
5690 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5691 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005692 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005693 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005694 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005695
Julia Lawall34d19ba2011-01-24 19:55:19 +00005696 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005697 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005698 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005699 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005700 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005701 if (ret) {
5702 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005703 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005704 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005705 }
5706
Chris Mason3de45862008-11-17 21:02:50 -05005707 return inode;
5708}
5709
Nick Pigginfe15ce42011-01-07 17:49:23 +11005710static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005711{
5712 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005713 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005714
Li Zefan848cce02012-02-21 17:04:28 +08005715 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005716 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005717
Li Zefan848cce02012-02-21 17:04:28 +08005718 if (inode) {
5719 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005720 if (btrfs_root_refs(&root->root_item) == 0)
5721 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005722
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005723 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005724 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005725 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005726 return 0;
5727}
5728
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005729static void btrfs_dentry_release(struct dentry *dentry)
5730{
Daeseok Youn944a4512014-04-14 15:37:02 +09005731 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005732}
5733
Chris Mason3de45862008-11-17 21:02:50 -05005734static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005735 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005736{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005737 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005738
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005739 inode = btrfs_lookup_dentry(dir, dentry);
5740 if (IS_ERR(inode)) {
5741 if (PTR_ERR(inode) == -ENOENT)
5742 inode = NULL;
5743 else
5744 return ERR_CAST(inode);
5745 }
5746
Al Viro41d28bc2014-10-12 22:24:21 -04005747 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005748}
5749
Miao Xie16cdcec2011-04-22 18:12:22 +08005750unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005751 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5752};
5753
Al Viro9cdda8d2013-05-22 16:48:09 -04005754static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005755{
Al Viro9cdda8d2013-05-22 16:48:09 -04005756 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005758 struct btrfs_root *root = BTRFS_I(inode)->root;
5759 struct btrfs_item *item;
5760 struct btrfs_dir_item *di;
5761 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005762 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005763 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005764 struct list_head ins_list;
5765 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005766 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005767 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 unsigned char d_type;
5770 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005771 char tmp_name[32];
5772 char *name_ptr;
5773 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005774 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005775 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005776
Al Viro9cdda8d2013-05-22 16:48:09 -04005777 if (!dir_emit_dots(file, ctx))
5778 return 0;
5779
David Woodhouse49593bf2008-08-17 17:08:36 +01005780 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005781 if (!path)
5782 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005783
David Sterbae4058b52015-11-27 16:31:35 +01005784 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005785
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005786 INIT_LIST_HEAD(&ins_list);
5787 INIT_LIST_HEAD(&del_list);
5788 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005789
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005790 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005791 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005792 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005793
Chris Mason39279cc2007-06-12 06:35:45 -04005794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5795 if (ret < 0)
5796 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005797
5798 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005799 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005800 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005801 if (slot >= btrfs_header_nritems(leaf)) {
5802 ret = btrfs_next_leaf(root, path);
5803 if (ret < 0)
5804 goto err;
5805 else if (ret > 0)
5806 break;
5807 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005808 }
Chris Mason3de45862008-11-17 21:02:50 -05005809
Ross Kirkdd3cc162013-09-16 15:58:09 +01005810 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005811 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5812
5813 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005814 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005815 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005816 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005817 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005818 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005819 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005820 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005821
Al Viro9cdda8d2013-05-22 16:48:09 -04005822 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005823
Chris Mason39279cc2007-06-12 06:35:45 -04005824 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005825 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005826 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005827
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005828 name_len = btrfs_dir_name_len(leaf, di);
5829 if (name_len <= sizeof(tmp_name)) {
5830 name_ptr = tmp_name;
5831 } else {
5832 name_ptr = kmalloc(name_len, GFP_KERNEL);
5833 if (!name_ptr) {
5834 ret = -ENOMEM;
5835 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005836 }
Chris Mason39279cc2007-06-12 06:35:45 -04005837 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005838 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5839 name_len);
5840
5841 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5842 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5843
5844 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5845 d_type);
5846
5847 if (name_ptr != tmp_name)
5848 kfree(name_ptr);
5849
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005850 if (over)
5851 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005852 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005853next:
5854 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005855 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005856
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005857 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005858 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005859 goto nopos;
5860
Zach Browndb62efb2013-07-11 16:19:42 -07005861 /*
5862 * Stop new entries from being returned after we return the last
5863 * entry.
5864 *
5865 * New directory entries are assigned a strictly increasing
5866 * offset. This means that new entries created during readdir
5867 * are *guaranteed* to be seen in the future by that readdir.
5868 * This has broken buggy programs which operate on names as
5869 * they're returned by readdir. Until we re-use freed offsets
5870 * we have this hack to stop new entries from being returned
5871 * under the assumption that they'll never reach this huge
5872 * offset.
5873 *
5874 * This is being careful not to overflow 32bit loff_t unless the
5875 * last entry requires it because doing so has broken 32bit apps
5876 * in the past.
5877 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005878 if (ctx->pos >= INT_MAX)
5879 ctx->pos = LLONG_MAX;
5880 else
5881 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005882nopos:
5883 ret = 0;
5884err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005885 if (put)
5886 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005887 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005888 return ret;
5889}
5890
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005891int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005892{
5893 struct btrfs_root *root = BTRFS_I(inode)->root;
5894 struct btrfs_trans_handle *trans;
5895 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005896 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005897
Josef Bacik72ac3c02012-05-23 14:13:11 -04005898 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005899 return 0;
5900
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005901 if (btrfs_fs_closing(root->fs_info) &&
5902 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005903 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005904
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005905 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005906 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005907 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005908 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005909 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005910 if (IS_ERR(trans))
5911 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005912 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005913 }
5914 return ret;
5915}
5916
5917/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005918 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005919 * inode changes. But, it is most likely to find the inode in cache.
5920 * FIXME, needs more benchmarking...there are no reasons other than performance
5921 * to keep or drop this code.
5922 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005923static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005924{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005926 struct btrfs_root *root = BTRFS_I(inode)->root;
5927 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005928 int ret;
5929
Josef Bacik72ac3c02012-05-23 14:13:11 -04005930 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005931 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005932
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005933 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005934 if (IS_ERR(trans))
5935 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005936
5937 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005938 if (ret && ret == -ENOSPC) {
5939 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005940 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005941 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005942 if (IS_ERR(trans))
5943 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005944
Chris Mason94b60442010-05-26 11:02:00 -04005945 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005946 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005947 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005948 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005949 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005950
5951 return ret;
5952}
5953
5954/*
5955 * This is a copy of file_update_time. We need this so we can return error on
5956 * ENOSPC for updating the inode in the case of file write and mmap writes.
5957 */
Josef Bacike41f9412012-03-26 09:46:47 -04005958static int btrfs_update_time(struct inode *inode, struct timespec *now,
5959 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005960{
Alexander Block2bc5565282012-06-15 09:49:33 +02005961 struct btrfs_root *root = BTRFS_I(inode)->root;
5962
5963 if (btrfs_root_readonly(root))
5964 return -EROFS;
5965
Josef Bacike41f9412012-03-26 09:46:47 -04005966 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005967 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005968 if (flags & S_CTIME)
5969 inode->i_ctime = *now;
5970 if (flags & S_MTIME)
5971 inode->i_mtime = *now;
5972 if (flags & S_ATIME)
5973 inode->i_atime = *now;
5974 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005975}
5976
Chris Masond352ac62008-09-29 15:18:18 -04005977/*
5978 * find the highest existing sequence number in a directory
5979 * and then set the in-memory index_cnt variable to reflect
5980 * free sequence numbers
5981 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005982static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005983{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005984 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005985 struct btrfs_key key, found_key;
5986 struct btrfs_path *path;
5987 struct extent_buffer *leaf;
5988 int ret;
5989
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005990 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005991 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005992 key.offset = (u64)-1;
5993
5994 path = btrfs_alloc_path();
5995 if (!path)
5996 return -ENOMEM;
5997
5998 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5999 if (ret < 0)
6000 goto out;
6001 /* FIXME: we should be able to handle this */
6002 if (ret == 0)
6003 goto out;
6004 ret = 0;
6005
6006 /*
6007 * MAGIC NUMBER EXPLANATION:
6008 * since we search a directory based on f_pos we have to start at 2
6009 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6010 * else has to start at 2
6011 */
6012 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006013 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006014 goto out;
6015 }
6016
6017 path->slots[0]--;
6018
6019 leaf = path->nodes[0];
6020 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6021
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006022 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006023 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006024 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006025 goto out;
6026 }
6027
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006028 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006029out:
6030 btrfs_free_path(path);
6031 return ret;
6032}
6033
Chris Masond352ac62008-09-29 15:18:18 -04006034/*
6035 * helper to find a free sequence number in a given directory. This current
6036 * code is very simple, later versions will do smarter things in the btree
6037 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006038int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006039{
6040 int ret = 0;
6041
Nikolay Borisov877574e2017-02-20 13:50:33 +02006042 if (dir->index_cnt == (u64)-1) {
6043 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006044 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006045 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006046 if (ret)
6047 return ret;
6048 }
Josef Bacikaec74772008-07-24 12:12:38 -04006049 }
6050
Nikolay Borisov877574e2017-02-20 13:50:33 +02006051 *index = dir->index_cnt;
6052 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006053
6054 return ret;
6055}
6056
Chris Masonb0d5d102014-09-08 13:08:51 -07006057static int btrfs_insert_inode_locked(struct inode *inode)
6058{
6059 struct btrfs_iget_args args;
6060 args.location = &BTRFS_I(inode)->location;
6061 args.root = BTRFS_I(inode)->root;
6062
6063 return insert_inode_locked4(inode,
6064 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6065 btrfs_find_actor, &args);
6066}
6067
Chris Mason39279cc2007-06-12 06:35:45 -04006068static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6069 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006070 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006071 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006072 u64 ref_objectid, u64 objectid,
6073 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006075 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006076 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006077 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006078 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006079 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006080 struct btrfs_inode_ref *ref;
6081 struct btrfs_key key[2];
6082 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006083 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006084 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006085 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006086
Chris Mason5f39d392007-10-15 16:14:19 -04006087 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006088 if (!path)
6089 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006090
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006091 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006092 if (!inode) {
6093 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006094 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006095 }
Chris Mason39279cc2007-06-12 06:35:45 -04006096
Li Zefan581bb052011-04-20 10:06:11 +08006097 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006098 * O_TMPFILE, set link count to 0, so that after this point,
6099 * we fill in an inode item with the correct link count.
6100 */
6101 if (!name)
6102 set_nlink(inode, 0);
6103
6104 /*
Li Zefan581bb052011-04-20 10:06:11 +08006105 * we have to initialize this early, so we can reclaim the inode
6106 * number if we fail afterwards in this function.
6107 */
6108 inode->i_ino = objectid;
6109
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006110 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006111 trace_btrfs_inode_request(dir);
6112
Nikolay Borisov877574e2017-02-20 13:50:33 +02006113 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006114 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006115 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006116 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006117 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006118 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006119 } else if (dir) {
6120 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006121 }
6122 /*
6123 * index_cnt is ignored for everything but a dir,
6124 * btrfs_get_inode_index_count has an explanation for the magic
6125 * number
6126 */
6127 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006128 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006129 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006130 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006131 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006132
Josef Bacik5dc562c2012-08-17 13:14:17 -04006133 /*
6134 * We could have gotten an inode number from somebody who was fsynced
6135 * and then removed in this same transaction, so let's just set full
6136 * sync since it will be a full sync anyway and this will blow away the
6137 * old info in the log.
6138 */
6139 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6140
Chris Mason9c583092008-01-29 15:15:18 -05006141 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006142 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006143 key[0].offset = 0;
6144
Chris Mason9c583092008-01-29 15:15:18 -05006145 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006146
6147 if (name) {
6148 /*
6149 * Start new inodes with an inode_ref. This is slightly more
6150 * efficient for small numbers of hard links since they will
6151 * be packed into one item. Extended refs will kick in if we
6152 * add more hard links than can fit in the ref item.
6153 */
6154 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006155 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006156 key[1].offset = ref_objectid;
6157
6158 sizes[1] = name_len + sizeof(*ref);
6159 }
Chris Mason9c583092008-01-29 15:15:18 -05006160
Chris Masonb0d5d102014-09-08 13:08:51 -07006161 location = &BTRFS_I(inode)->location;
6162 location->objectid = objectid;
6163 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006164 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006165
6166 ret = btrfs_insert_inode_locked(inode);
6167 if (ret < 0)
6168 goto fail;
6169
Chris Masonb9473432009-03-13 11:00:37 -04006170 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006171 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006172 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006173 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006174
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006175 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006176 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306177
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006178 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306179 inode->i_atime = inode->i_mtime;
6180 inode->i_ctime = inode->i_mtime;
6181 BTRFS_I(inode)->i_otime = inode->i_mtime;
6182
Chris Mason5f39d392007-10-15 16:14:19 -04006183 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6184 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006185 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006186 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006187 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006188
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006189 if (name) {
6190 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6191 struct btrfs_inode_ref);
6192 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6193 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6194 ptr = (unsigned long)(ref + 1);
6195 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6196 }
Chris Mason9c583092008-01-29 15:15:18 -05006197
Chris Mason5f39d392007-10-15 16:14:19 -04006198 btrfs_mark_buffer_dirty(path->nodes[0]);
6199 btrfs_free_path(path);
6200
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006201 btrfs_inherit_iflags(inode, dir);
6202
Al Viro569254b2011-07-24 17:08:40 -04006203 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006204 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006205 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006206 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006207 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6208 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006209 }
6210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006211 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006212
6213 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006214 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006215
Alexander Block8ea05e32012-07-25 17:35:53 +02006216 btrfs_update_root_times(trans, root);
6217
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006218 ret = btrfs_inode_inherit_props(trans, inode, dir);
6219 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006220 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006221 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006222 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006223
Chris Mason39279cc2007-06-12 06:35:45 -04006224 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006225
6226fail_unlock:
6227 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006228fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006229 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006230 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006231 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006232 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006233 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006234}
6235
6236static inline u8 btrfs_inode_type(struct inode *inode)
6237{
6238 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6239}
6240
Chris Masond352ac62008-09-29 15:18:18 -04006241/*
6242 * utility function to add 'inode' into 'parent_inode' with
6243 * a give name and a given sequence number.
6244 * if 'add_backref' is true, also insert a backref from the
6245 * inode to the parent directory.
6246 */
Chris Masone02119d2008-09-05 16:13:11 -04006247int btrfs_add_link(struct btrfs_trans_handle *trans,
6248 struct inode *parent_inode, struct inode *inode,
6249 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006252 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006253 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006254 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006255 u64 ino = btrfs_ino(BTRFS_I(inode));
6256 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006257
Li Zefan33345d012011-04-20 10:31:50 +08006258 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006259 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6260 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006261 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006262 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006263 key.offset = 0;
6264 }
Chris Mason39279cc2007-06-12 06:35:45 -04006265
Li Zefan33345d012011-04-20 10:31:50 +08006266 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006267 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6268 root->root_key.objectid, parent_ino,
6269 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006270 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006271 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6272 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006273 }
6274
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006275 /* Nothing to clean up yet */
6276 if (ret)
6277 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006278
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006279 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +02006280 BTRFS_I(parent_inode), &key,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006281 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006282 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006283 goto fail_dir_item;
6284 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006285 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006286 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006287 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006288
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006289 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006290 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006291 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006292 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006293 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006294 ret = btrfs_update_inode(trans, root, parent_inode);
6295 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006296 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006297 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006298
6299fail_dir_item:
6300 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6301 u64 local_index;
6302 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6304 root->root_key.objectid, parent_ino,
6305 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006306
6307 } else if (add_backref) {
6308 u64 local_index;
6309 int err;
6310
6311 err = btrfs_del_inode_ref(trans, root, name, name_len,
6312 ino, parent_ino, &local_index);
6313 }
6314 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006315}
6316
6317static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006318 struct inode *dir, struct dentry *dentry,
6319 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006320{
Josef Bacika1b075d2010-11-19 20:36:11 +00006321 int err = btrfs_add_link(trans, dir, inode,
6322 dentry->d_name.name, dentry->d_name.len,
6323 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006324 if (err > 0)
6325 err = -EEXIST;
6326 return err;
6327}
6328
Josef Bacik618e21d2007-07-11 10:18:17 -04006329static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006330 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006331{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006332 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006333 struct btrfs_trans_handle *trans;
6334 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006335 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006336 int err;
6337 int drop_inode = 0;
6338 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006339 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006340
Josef Bacik9ed74f22009-09-11 16:12:44 -04006341 /*
6342 * 2 for inode item and ref
6343 * 2 for dir items
6344 * 1 for xattr if selinux is on
6345 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006346 trans = btrfs_start_transaction(root, 5);
6347 if (IS_ERR(trans))
6348 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006349
Li Zefan581bb052011-04-20 10:06:11 +08006350 err = btrfs_find_free_ino(root, &objectid);
6351 if (err)
6352 goto out_unlock;
6353
Josef Bacikaec74772008-07-24 12:12:38 -04006354 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006355 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6356 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006357 if (IS_ERR(inode)) {
6358 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006359 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006360 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006361
Casey Schauflerad19db72011-12-15 10:09:07 -05006362 /*
6363 * If the active LSM wants to access the inode during
6364 * d_instantiate it needs these. Smack checks to see
6365 * if the filesystem supports xattrs by looking at the
6366 * ops vector.
6367 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006368 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 init_special_inode(inode, inode->i_mode, rdev);
6370
6371 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006372 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006373 goto out_unlock_inode;
6374
6375 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6376 if (err) {
6377 goto out_unlock_inode;
6378 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006379 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006380 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006381 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006382 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006383
Josef Bacik618e21d2007-07-11 10:18:17 -04006384out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006385 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006386 btrfs_balance_delayed_items(fs_info);
6387 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006388 if (drop_inode) {
6389 inode_dec_link_count(inode);
6390 iput(inode);
6391 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006392 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006393
6394out_unlock_inode:
6395 drop_inode = 1;
6396 unlock_new_inode(inode);
6397 goto out_unlock;
6398
Josef Bacik618e21d2007-07-11 10:18:17 -04006399}
6400
Chris Mason39279cc2007-06-12 06:35:45 -04006401static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006402 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006403{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006404 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006405 struct btrfs_trans_handle *trans;
6406 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006407 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006408 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006409 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006410 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006411 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006412
Josef Bacik9ed74f22009-09-11 16:12:44 -04006413 /*
6414 * 2 for inode item and ref
6415 * 2 for dir items
6416 * 1 for xattr if selinux is on
6417 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006418 trans = btrfs_start_transaction(root, 5);
6419 if (IS_ERR(trans))
6420 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006421
Li Zefan581bb052011-04-20 10:06:11 +08006422 err = btrfs_find_free_ino(root, &objectid);
6423 if (err)
6424 goto out_unlock;
6425
Josef Bacikaec74772008-07-24 12:12:38 -04006426 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006427 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6428 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006429 if (IS_ERR(inode)) {
6430 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006431 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006432 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006433 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006434 /*
6435 * If the active LSM wants to access the inode during
6436 * d_instantiate it needs these. Smack checks to see
6437 * if the filesystem supports xattrs by looking at the
6438 * ops vector.
6439 */
6440 inode->i_fop = &btrfs_file_operations;
6441 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006442 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443
6444 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6445 if (err)
6446 goto out_unlock_inode;
6447
6448 err = btrfs_update_inode(trans, root, inode);
6449 if (err)
6450 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006451
Josef Bacika1b075d2010-11-19 20:36:11 +00006452 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006454 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006455
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006457 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006458 d_instantiate(dentry, inode);
6459
Chris Mason39279cc2007-06-12 06:35:45 -04006460out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006461 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006462 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006463 inode_dec_link_count(inode);
6464 iput(inode);
6465 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006466 btrfs_balance_delayed_items(fs_info);
6467 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006468 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006469
6470out_unlock_inode:
6471 unlock_new_inode(inode);
6472 goto out_unlock;
6473
Chris Mason39279cc2007-06-12 06:35:45 -04006474}
6475
6476static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6477 struct dentry *dentry)
6478{
Filipe Manana271dba452016-01-05 16:24:05 +00006479 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006480 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006481 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006483 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 int err;
6485 int drop_inode = 0;
6486
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006487 /* do not allow sys_link's with other subvols of the same device */
6488 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006489 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006490
Mark Fashehf1863732012-08-08 11:32:27 -07006491 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006492 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006493
Nikolay Borisov877574e2017-02-20 13:50:33 +02006494 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006495 if (err)
6496 goto fail;
6497
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006499 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006501 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006502 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006503 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006504 if (IS_ERR(trans)) {
6505 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006506 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006507 goto fail;
6508 }
Chris Mason5f39d392007-10-15 16:14:19 -04006509
Miao Xie67de1172013-12-26 13:07:06 +08006510 /* There are several dir indexes for this inode, clear the cache. */
6511 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006512 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006513 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006514 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006515 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006516 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006517
Josef Bacika1b075d2010-11-19 20:36:11 +00006518 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006519
Yan, Zhenga5719522009-09-24 09:17:31 -04006520 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006521 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006522 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006523 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006524 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006525 if (err)
6526 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006527 if (inode->i_nlink == 1) {
6528 /*
6529 * If new hard link count is 1, it's a file created
6530 * with open(2) O_TMPFILE flag.
6531 */
6532 err = btrfs_orphan_del(trans, inode);
6533 if (err)
6534 goto fail;
6535 }
Al Viro08c422c2011-12-23 07:58:13 -05006536 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006537 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006538 }
Chris Mason39279cc2007-06-12 06:35:45 -04006539
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006540 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006541fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006542 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006543 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006544 if (drop_inode) {
6545 inode_dec_link_count(inode);
6546 iput(inode);
6547 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006548 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 return err;
6550}
6551
Al Viro18bb1db2011-07-26 01:41:39 -04006552static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006553{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006554 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006555 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006556 struct btrfs_trans_handle *trans;
6557 struct btrfs_root *root = BTRFS_I(dir)->root;
6558 int err = 0;
6559 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006560 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006561 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006562
Josef Bacik9ed74f22009-09-11 16:12:44 -04006563 /*
6564 * 2 items for inode and ref
6565 * 2 items for dir items
6566 * 1 for xattr if selinux is on
6567 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006568 trans = btrfs_start_transaction(root, 5);
6569 if (IS_ERR(trans))
6570 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006571
Li Zefan581bb052011-04-20 10:06:11 +08006572 err = btrfs_find_free_ino(root, &objectid);
6573 if (err)
6574 goto out_fail;
6575
Josef Bacikaec74772008-07-24 12:12:38 -04006576 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006577 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6578 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006579 if (IS_ERR(inode)) {
6580 err = PTR_ERR(inode);
6581 goto out_fail;
6582 }
Chris Mason5f39d392007-10-15 16:14:19 -04006583
Chris Mason39279cc2007-06-12 06:35:45 -04006584 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006585 /* these must be set before we unlock the inode */
6586 inode->i_op = &btrfs_dir_inode_operations;
6587 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006588
Eric Paris2a7dba32011-02-01 11:05:39 -05006589 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006590 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006592
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006593 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006594 err = btrfs_update_inode(trans, root, inode);
6595 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006596 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006597
Josef Bacika1b075d2010-11-19 20:36:11 +00006598 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6599 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006600 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006602
Chris Mason39279cc2007-06-12 06:35:45 -04006603 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006604 /*
6605 * mkdir is special. We're unlocking after we call d_instantiate
6606 * to avoid a race with nfsd calling d_instantiate.
6607 */
6608 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006609 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006610
6611out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006612 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006613 if (drop_on_err) {
6614 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006616 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006617 btrfs_balance_delayed_items(fs_info);
6618 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006620
6621out_fail_inode:
6622 unlock_new_inode(inode);
6623 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006624}
6625
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006626/* Find next extent map of a given extent map, caller needs to ensure locks */
6627static struct extent_map *next_extent_map(struct extent_map *em)
6628{
6629 struct rb_node *next;
6630
6631 next = rb_next(&em->rb_node);
6632 if (!next)
6633 return NULL;
6634 return container_of(next, struct extent_map, rb_node);
6635}
6636
6637static struct extent_map *prev_extent_map(struct extent_map *em)
6638{
6639 struct rb_node *prev;
6640
6641 prev = rb_prev(&em->rb_node);
6642 if (!prev)
6643 return NULL;
6644 return container_of(prev, struct extent_map, rb_node);
6645}
6646
Chris Masond352ac62008-09-29 15:18:18 -04006647/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006648 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006649 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006650 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006651 */
Chris Mason3b951512008-04-17 11:29:12 -04006652static int merge_extent_mapping(struct extent_map_tree *em_tree,
6653 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006654 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006655 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006656{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006657 struct extent_map *prev;
6658 struct extent_map *next;
6659 u64 start;
6660 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006661 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006662
Chris Masone6dcd2d2008-07-17 12:53:50 -04006663 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006664
6665 if (existing->start > map_start) {
6666 next = existing;
6667 prev = prev_extent_map(next);
6668 } else {
6669 prev = existing;
6670 next = next_extent_map(prev);
6671 }
6672
6673 start = prev ? extent_map_end(prev) : em->start;
6674 start = max_t(u64, start, em->start);
6675 end = next ? next->start : extent_map_end(em);
6676 end = min_t(u64, end, extent_map_end(em));
6677 start_diff = start - em->start;
6678 em->start = start;
6679 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006680 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6681 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006682 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006683 em->block_len -= start_diff;
6684 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006685 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006686}
6687
Chris Masonc8b97812008-10-29 14:49:59 -04006688static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006689 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006690 size_t pg_offset, u64 extent_offset,
6691 struct btrfs_file_extent_item *item)
6692{
6693 int ret;
6694 struct extent_buffer *leaf = path->nodes[0];
6695 char *tmp;
6696 size_t max_size;
6697 unsigned long inline_size;
6698 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006699 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006700
6701 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006702 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006703 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6704 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006705 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006706 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006707 if (!tmp)
6708 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006709 ptr = btrfs_file_extent_inline_start(item);
6710
6711 read_extent_buffer(leaf, tmp, ptr, inline_size);
6712
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006713 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006714 ret = btrfs_decompress(compress_type, tmp, page,
6715 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006716 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006717 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006718}
6719
Chris Masond352ac62008-09-29 15:18:18 -04006720/*
6721 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006722 * the ugly parts come from merging extents from the disk with the in-ram
6723 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006724 * where the in-ram extents might be locked pending data=ordered completion.
6725 *
6726 * This also copies inline extents directly into the page.
6727 */
Chris Masond3977122009-01-05 21:25:51 -05006728
Chris Masona52d9a82007-08-27 16:49:44 -04006729struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006730 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006731 int create)
6732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006734 int ret;
6735 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006736 u64 extent_start = 0;
6737 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006738 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006739 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006740 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 struct btrfs_root *root = BTRFS_I(inode)->root;
6742 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006743 struct extent_buffer *leaf;
6744 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006745 struct extent_map *em = NULL;
6746 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006747 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006748 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006749 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006750
Chris Masona52d9a82007-08-27 16:49:44 -04006751again:
Chris Mason890871b2009-09-02 16:24:52 -04006752 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006753 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006754 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006755 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006756 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006757
Chris Masona52d9a82007-08-27 16:49:44 -04006758 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006759 if (em->start > start || em->start + em->len <= start)
6760 free_extent_map(em);
6761 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006762 free_extent_map(em);
6763 else
6764 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006765 }
David Sterba172ddd62011-04-21 00:48:27 +02006766 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006767 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006768 err = -ENOMEM;
6769 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006770 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006771 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006772 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006773 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006774 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006775 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006776
6777 if (!path) {
6778 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006779 if (!path) {
6780 err = -ENOMEM;
6781 goto out;
6782 }
6783 /*
6784 * Chances are we'll be called again, so go ahead and do
6785 * readahead
6786 */
David Sterbae4058b52015-11-27 16:31:35 +01006787 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006788 }
6789
Chris Mason179e29e2007-11-01 11:28:41 -04006790 ret = btrfs_lookup_file_extent(trans, root, path,
6791 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006792 if (ret < 0) {
6793 err = ret;
6794 goto out;
6795 }
6796
6797 if (ret != 0) {
6798 if (path->slots[0] == 0)
6799 goto not_found;
6800 path->slots[0]--;
6801 }
6802
Chris Mason5f39d392007-10-15 16:14:19 -04006803 leaf = path->nodes[0];
6804 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006805 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006806 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006807 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006808 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006809 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006810 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006811 /*
6812 * If we backup past the first extent we want to move forward
6813 * and see if there is an extent in front of us, otherwise we'll
6814 * say there is a hole for our whole search range which can
6815 * cause problems.
6816 */
6817 extent_end = start;
6818 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 }
6820
Chris Mason5f39d392007-10-15 16:14:19 -04006821 found_type = btrfs_file_extent_type(leaf, item);
6822 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006823 if (found_type == BTRFS_FILE_EXTENT_REG ||
6824 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006825 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006826 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006827 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6828 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006829 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006830 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006831 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006832 }
Josef Bacik25a50342013-10-14 12:08:38 -04006833next:
Yan Zheng9036c102008-10-30 14:19:41 -04006834 if (start >= extent_end) {
6835 path->slots[0]++;
6836 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6837 ret = btrfs_next_leaf(root, path);
6838 if (ret < 0) {
6839 err = ret;
6840 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006841 }
Yan Zheng9036c102008-10-30 14:19:41 -04006842 if (ret > 0)
6843 goto not_found;
6844 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006845 }
Yan Zheng9036c102008-10-30 14:19:41 -04006846 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6847 if (found_key.objectid != objectid ||
6848 found_key.type != BTRFS_EXTENT_DATA_KEY)
6849 goto not_found;
6850 if (start + len <= found_key.offset)
6851 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006852 if (start > found_key.offset)
6853 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006854 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006855 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006856 em->len = found_key.offset - start;
6857 goto not_found_em;
6858 }
6859
Filipe Manana7ffbb592014-06-09 03:48:05 +01006860 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6861
Yan Zhengd899e052008-10-30 14:25:28 -04006862 if (found_type == BTRFS_FILE_EXTENT_REG ||
6863 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006864 goto insert;
6865 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006866 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006867 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006868 size_t size;
6869 size_t extent_offset;
6870 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006871
Filipe Manana7ffbb592014-06-09 03:48:05 +01006872 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006873 goto out;
Yan689f9342007-10-29 11:41:07 -04006874
Chris Mason514ac8a2014-01-03 21:07:00 -08006875 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006876 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006877 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6878 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006879 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006880 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006881 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006882 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006883 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006884 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006885 if (btrfs_file_extent_compression(leaf, item) !=
6886 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006887 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006888 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006889 if (ret) {
6890 err = ret;
6891 goto out;
6892 }
Chris Masonc8b97812008-10-29 14:49:59 -04006893 } else {
6894 map = kmap(page);
6895 read_extent_buffer(leaf, map + pg_offset, ptr,
6896 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006897 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006898 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006899 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006900 copy_size);
6901 }
Chris Masonc8b97812008-10-29 14:49:59 -04006902 kunmap(page);
6903 }
Chris Mason179e29e2007-11-01 11:28:41 -04006904 flush_dcache_page(page);
6905 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006906 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006907 if (!trans) {
6908 kunmap(page);
6909 free_extent_map(em);
6910 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006911
David Sterbab3b4aa72011-04-21 01:20:15 +02006912 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006913 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006914
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006915 if (IS_ERR(trans))
6916 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006917 goto again;
6918 }
Chris Masonc8b97812008-10-29 14:49:59 -04006919 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006920 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006921 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006922 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006923 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006924 }
Chris Masond1310b22008-01-24 16:13:08 -05006925 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006926 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006927 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006928 }
6929not_found:
6930 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006931 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006932 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006933not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006934 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006935 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006936insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006937 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006938 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006939 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006940 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6941 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006942 err = -EIO;
6943 goto out;
6944 }
Chris Masond1310b22008-01-24 16:13:08 -05006945
6946 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006947 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006948 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006949 /* it is possible that someone inserted the extent into the tree
6950 * while we had the lock dropped. It is also possible that
6951 * an overlapping map exists in the tree
6952 */
Chris Masona52d9a82007-08-27 16:49:44 -04006953 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006954 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006955
6956 ret = 0;
6957
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006958 existing = search_extent_mapping(em_tree, start, len);
6959 /*
6960 * existing will always be non-NULL, since there must be
6961 * extent causing the -EEXIST.
6962 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006963 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006964 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006965 em->block_start == existing->block_start) {
6966 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006967 * The existing extent map already encompasses the
6968 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006969 */
6970 free_extent_map(em);
6971 em = existing;
6972 err = 0;
6973
6974 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006975 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006976 /*
6977 * The existing extent map is the one nearest to
6978 * the [start, start + len) range which overlaps
6979 */
6980 err = merge_extent_mapping(em_tree, existing,
6981 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006982 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006983 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006984 free_extent_map(em);
6985 em = NULL;
6986 }
6987 } else {
6988 free_extent_map(em);
6989 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006990 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Chris Masona52d9a82007-08-27 16:49:44 -04006992 }
Chris Mason890871b2009-09-02 16:24:52 -04006993 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006994out:
liubo1abe9b82011-03-24 11:18:59 +00006995
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006996 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00006997
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006998 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006999 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007000 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007001 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007002 err = ret;
7003 }
Chris Masona52d9a82007-08-27 16:49:44 -04007004 if (err) {
7005 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007006 return ERR_PTR(err);
7007 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007008 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007009 return em;
7010}
7011
Chris Masonec29ed52011-02-23 16:23:20 -05007012struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7013 size_t pg_offset, u64 start, u64 len,
7014 int create)
7015{
7016 struct extent_map *em;
7017 struct extent_map *hole_em = NULL;
7018 u64 range_start = start;
7019 u64 end;
7020 u64 found;
7021 u64 found_end;
7022 int err = 0;
7023
7024 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7025 if (IS_ERR(em))
7026 return em;
7027 if (em) {
7028 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007029 * if our em maps to
7030 * - a hole or
7031 * - a pre-alloc extent,
7032 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007033 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007034 if (em->block_start != EXTENT_MAP_HOLE &&
7035 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007036 return em;
7037 else
7038 hole_em = em;
7039 }
7040
7041 /* check to see if we've wrapped (len == -1 or similar) */
7042 end = start + len;
7043 if (end < start)
7044 end = (u64)-1;
7045 else
7046 end -= 1;
7047
7048 em = NULL;
7049
7050 /* ok, we didn't find anything, lets look for delalloc */
7051 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7052 end, len, EXTENT_DELALLOC, 1);
7053 found_end = range_start + found;
7054 if (found_end < range_start)
7055 found_end = (u64)-1;
7056
7057 /*
7058 * we didn't find anything useful, return
7059 * the original results from get_extent()
7060 */
7061 if (range_start > end || found_end <= start) {
7062 em = hole_em;
7063 hole_em = NULL;
7064 goto out;
7065 }
7066
7067 /* adjust the range_start to make sure it doesn't
7068 * go backwards from the start they passed in
7069 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307070 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007071 found = found_end - range_start;
7072
7073 if (found > 0) {
7074 u64 hole_start = start;
7075 u64 hole_len = len;
7076
David Sterba172ddd62011-04-21 00:48:27 +02007077 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007078 if (!em) {
7079 err = -ENOMEM;
7080 goto out;
7081 }
7082 /*
7083 * when btrfs_get_extent can't find anything it
7084 * returns one huge hole
7085 *
7086 * make sure what it found really fits our range, and
7087 * adjust to make sure it is based on the start from
7088 * the caller
7089 */
7090 if (hole_em) {
7091 u64 calc_end = extent_map_end(hole_em);
7092
7093 if (calc_end <= start || (hole_em->start > end)) {
7094 free_extent_map(hole_em);
7095 hole_em = NULL;
7096 } else {
7097 hole_start = max(hole_em->start, start);
7098 hole_len = calc_end - hole_start;
7099 }
7100 }
7101 em->bdev = NULL;
7102 if (hole_em && range_start > hole_start) {
7103 /* our hole starts before our delalloc, so we
7104 * have to return just the parts of the hole
7105 * that go until the delalloc starts
7106 */
7107 em->len = min(hole_len,
7108 range_start - hole_start);
7109 em->start = hole_start;
7110 em->orig_start = hole_start;
7111 /*
7112 * don't adjust block start at all,
7113 * it is fixed at EXTENT_MAP_HOLE
7114 */
7115 em->block_start = hole_em->block_start;
7116 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007117 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7118 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007119 } else {
7120 em->start = range_start;
7121 em->len = found;
7122 em->orig_start = range_start;
7123 em->block_start = EXTENT_MAP_DELALLOC;
7124 em->block_len = found;
7125 }
7126 } else if (hole_em) {
7127 return hole_em;
7128 }
7129out:
7130
7131 free_extent_map(hole_em);
7132 if (err) {
7133 free_extent_map(em);
7134 return ERR_PTR(err);
7135 }
7136 return em;
7137}
7138
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007139static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7140 const u64 start,
7141 const u64 len,
7142 const u64 orig_start,
7143 const u64 block_start,
7144 const u64 block_len,
7145 const u64 orig_block_len,
7146 const u64 ram_bytes,
7147 const int type)
7148{
7149 struct extent_map *em = NULL;
7150 int ret;
7151
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007152 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007153 em = create_io_em(inode, start, len, orig_start,
7154 block_start, block_len, orig_block_len,
7155 ram_bytes,
7156 BTRFS_COMPRESS_NONE, /* compress_type */
7157 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007158 if (IS_ERR(em))
7159 goto out;
7160 }
7161 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7162 len, block_len, type);
7163 if (ret) {
7164 if (em) {
7165 free_extent_map(em);
7166 btrfs_drop_extent_cache(inode, start,
7167 start + len - 1, 0);
7168 }
7169 em = ERR_PTR(ret);
7170 }
7171 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007172
7173 return em;
7174}
7175
Josef Bacik4b46fce2010-05-23 11:00:55 -04007176static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7177 u64 start, u64 len)
7178{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007181 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007182 struct btrfs_key ins;
7183 u64 alloc_hint;
7184 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007188 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007189 if (ret)
7190 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007192 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7193 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007194 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007195 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007196 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007197 btrfs_free_reserved_extent(fs_info, ins.objectid,
7198 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007199
Josef Bacik4b46fce2010-05-23 11:00:55 -04007200 return em;
7201}
7202
Chris Mason46bfbb52010-05-26 11:04:10 -04007203/*
7204 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7205 * block must be cow'd
7206 */
Josef Bacik00361582013-08-14 14:02:47 -04007207noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007208 u64 *orig_start, u64 *orig_block_len,
7209 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007210{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007211 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007212 struct btrfs_path *path;
7213 int ret;
7214 struct extent_buffer *leaf;
7215 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007216 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007217 struct btrfs_file_extent_item *fi;
7218 struct btrfs_key key;
7219 u64 disk_bytenr;
7220 u64 backref_offset;
7221 u64 extent_end;
7222 u64 num_bytes;
7223 int slot;
7224 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007225 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007226
Chris Mason46bfbb52010-05-26 11:04:10 -04007227 path = btrfs_alloc_path();
7228 if (!path)
7229 return -ENOMEM;
7230
David Sterbaf85b7372017-01-20 14:54:07 +01007231 ret = btrfs_lookup_file_extent(NULL, root, path,
7232 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007233 if (ret < 0)
7234 goto out;
7235
7236 slot = path->slots[0];
7237 if (ret == 1) {
7238 if (slot == 0) {
7239 /* can't find the item, must cow */
7240 ret = 0;
7241 goto out;
7242 }
7243 slot--;
7244 }
7245 ret = 0;
7246 leaf = path->nodes[0];
7247 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007248 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 key.type != BTRFS_EXTENT_DATA_KEY) {
7250 /* not our file or wrong item type, must cow */
7251 goto out;
7252 }
7253
7254 if (key.offset > offset) {
7255 /* Wrong offset, must cow */
7256 goto out;
7257 }
7258
7259 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7260 found_type = btrfs_file_extent_type(leaf, fi);
7261 if (found_type != BTRFS_FILE_EXTENT_REG &&
7262 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7263 /* not a regular extent, must cow */
7264 goto out;
7265 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007266
7267 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7268 goto out;
7269
Miao Xiee77751a2013-12-27 21:11:50 +08007270 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7271 if (extent_end <= offset)
7272 goto out;
7273
Chris Mason46bfbb52010-05-26 11:04:10 -04007274 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007275 if (disk_bytenr == 0)
7276 goto out;
7277
7278 if (btrfs_file_extent_compression(leaf, fi) ||
7279 btrfs_file_extent_encryption(leaf, fi) ||
7280 btrfs_file_extent_other_encoding(leaf, fi))
7281 goto out;
7282
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 backref_offset = btrfs_file_extent_offset(leaf, fi);
7284
Josef Bacik7ee9e442013-06-21 16:37:03 -04007285 if (orig_start) {
7286 *orig_start = key.offset - backref_offset;
7287 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7288 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7289 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007291 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007292 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007293
7294 num_bytes = min(offset + *len, extent_end) - offset;
7295 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7296 u64 range_end;
7297
Jeff Mahoneyda170662016-06-15 09:22:56 -04007298 range_end = round_up(offset + num_bytes,
7299 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007300 ret = test_range_bit(io_tree, offset, range_end,
7301 EXTENT_DELALLOC, 0, NULL);
7302 if (ret) {
7303 ret = -EAGAIN;
7304 goto out;
7305 }
7306 }
7307
Josef Bacik1bda19e2013-10-18 12:10:36 -04007308 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007309
7310 /*
7311 * look for other files referencing this extent, if we
7312 * find any we must cow
7313 */
Josef Bacik00361582013-08-14 14:02:47 -04007314
Liu Boe4c3b2d2017-01-30 12:25:28 -08007315 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007316 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007317 if (ret) {
7318 ret = 0;
7319 goto out;
7320 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007321
7322 /*
7323 * adjust disk_bytenr and num_bytes to cover just the bytes
7324 * in this extent we are about to write. If there
7325 * are any csums in that range we have to cow in order
7326 * to keep the csums correct
7327 */
7328 disk_bytenr += backref_offset;
7329 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007330 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7331 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 /*
7333 * all of the above have passed, it is safe to overwrite this extent
7334 * without cow
7335 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007336 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007337 ret = 1;
7338out:
7339 btrfs_free_path(path);
7340 return ret;
7341}
7342
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007343bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7344{
7345 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7346 int found = false;
7347 void **pagep = NULL;
7348 struct page *page = NULL;
7349 int start_idx;
7350 int end_idx;
7351
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007352 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007353
7354 /*
7355 * end is the last byte in the last page. end == start is legal
7356 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007357 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007358
7359 rcu_read_lock();
7360
7361 /* Most of the code in this while loop is lifted from
7362 * find_get_page. It's been modified to begin searching from a
7363 * page and return just the first page found in that range. If the
7364 * found idx is less than or equal to the end idx then we know that
7365 * a page exists. If no pages are found or if those pages are
7366 * outside of the range then we're fine (yay!) */
7367 while (page == NULL &&
7368 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7369 page = radix_tree_deref_slot(pagep);
7370 if (unlikely(!page))
7371 break;
7372
7373 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007374 if (radix_tree_deref_retry(page)) {
7375 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007376 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007377 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007378 /*
7379 * Otherwise, shmem/tmpfs must be storing a swap entry
7380 * here as an exceptional entry: so return it without
7381 * attempting to raise page count.
7382 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007383 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007384 break; /* TODO: Is this relevant for this use case? */
7385 }
7386
Filipe Manana91405152014-06-05 13:22:24 +01007387 if (!page_cache_get_speculative(page)) {
7388 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007389 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007390 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007391
7392 /*
7393 * Has the page moved?
7394 * This is part of the lockless pagecache protocol. See
7395 * include/linux/pagemap.h for details.
7396 */
7397 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007398 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007399 page = NULL;
7400 }
7401 }
7402
7403 if (page) {
7404 if (page->index <= end_idx)
7405 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007406 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007407 }
7408
7409 rcu_read_unlock();
7410 return found;
7411}
7412
Josef Bacikeb838e72012-07-31 16:28:48 -04007413static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7414 struct extent_state **cached_state, int writing)
7415{
7416 struct btrfs_ordered_extent *ordered;
7417 int ret = 0;
7418
7419 while (1) {
7420 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007421 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007422 /*
7423 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007424 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007425 * extents in this range.
7426 */
7427 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7428 lockend - lockstart + 1);
7429
7430 /*
7431 * We need to make sure there are no buffered pages in this
7432 * range either, we could have raced between the invalidate in
7433 * generic_file_direct_write and locking the extent. The
7434 * invalidate needs to happen so that reads after a write do not
7435 * get stale data.
7436 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007437 if (!ordered &&
7438 (!writing ||
7439 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007440 break;
7441
7442 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7443 cached_state, GFP_NOFS);
7444
7445 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007446 /*
7447 * If we are doing a DIO read and the ordered extent we
7448 * found is for a buffered write, we can not wait for it
7449 * to complete and retry, because if we do so we can
7450 * deadlock with concurrent buffered writes on page
7451 * locks. This happens only if our DIO read covers more
7452 * than one extent map, if at this point has already
7453 * created an ordered extent for a previous extent map
7454 * and locked its range in the inode's io tree, and a
7455 * concurrent write against that previous extent map's
7456 * range and this range started (we unlock the ranges
7457 * in the io tree only when the bios complete and
7458 * buffered writes always lock pages before attempting
7459 * to lock range in the io tree).
7460 */
7461 if (writing ||
7462 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7463 btrfs_start_ordered_extent(inode, ordered, 1);
7464 else
7465 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007466 btrfs_put_ordered_extent(ordered);
7467 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007469 * We could trigger writeback for this range (and wait
7470 * for it to complete) and then invalidate the pages for
7471 * this range (through invalidate_inode_pages2_range()),
7472 * but that can lead us to a deadlock with a concurrent
7473 * call to readpages() (a buffered read or a defrag call
7474 * triggered a readahead) on a page lock due to an
7475 * ordered dio extent we created before but did not have
7476 * yet a corresponding bio submitted (whence it can not
7477 * complete), which makes readpages() wait for that
7478 * ordered extent to complete while holding a lock on
7479 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007480 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007481 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007482 }
7483
Filipe Mananaade77022016-02-18 14:28:55 +00007484 if (ret)
7485 break;
7486
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 cond_resched();
7488 }
7489
7490 return ret;
7491}
7492
Liu Bo6f9994d2017-01-31 07:50:22 -08007493/* The callers of this must take lock_extent() */
7494static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7495 u64 orig_start, u64 block_start,
7496 u64 block_len, u64 orig_block_len,
7497 u64 ram_bytes, int compress_type,
7498 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007499{
7500 struct extent_map_tree *em_tree;
7501 struct extent_map *em;
7502 struct btrfs_root *root = BTRFS_I(inode)->root;
7503 int ret;
7504
Liu Bo6f9994d2017-01-31 07:50:22 -08007505 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7506 type == BTRFS_ORDERED_COMPRESSED ||
7507 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007508 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007509
Josef Bacik69ffb542012-09-11 15:40:07 -04007510 em_tree = &BTRFS_I(inode)->extent_tree;
7511 em = alloc_extent_map();
7512 if (!em)
7513 return ERR_PTR(-ENOMEM);
7514
7515 em->start = start;
7516 em->orig_start = orig_start;
7517 em->len = len;
7518 em->block_len = block_len;
7519 em->block_start = block_start;
7520 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007521 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007522 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007523 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007524 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007525 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007526 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007527 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007528 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7529 em->compress_type = compress_type;
7530 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007531
7532 do {
7533 btrfs_drop_extent_cache(inode, em->start,
7534 em->start + em->len - 1, 0);
7535 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007536 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007537 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007538 /*
7539 * The caller has taken lock_extent(), who could race with us
7540 * to add em?
7541 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007542 } while (ret == -EEXIST);
7543
7544 if (ret) {
7545 free_extent_map(em);
7546 return ERR_PTR(ret);
7547 }
7548
Liu Bo6f9994d2017-01-31 07:50:22 -08007549 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007550 return em;
7551}
7552
Filipe Manana9c9464c2015-11-04 09:52:04 +00007553static void adjust_dio_outstanding_extents(struct inode *inode,
7554 struct btrfs_dio_data *dio_data,
7555 const u64 len)
7556{
David Sterba823bb202017-01-04 11:09:51 +01007557 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007558
Filipe Manana9c9464c2015-11-04 09:52:04 +00007559 /*
7560 * If we have an outstanding_extents count still set then we're
7561 * within our reservation, otherwise we need to adjust our inode
7562 * counter appropriately.
7563 */
Liu Boc2931662016-12-22 17:13:54 -08007564 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007565 dio_data->outstanding_extents -= num_extents;
7566 } else {
Liu Boc2931662016-12-22 17:13:54 -08007567 /*
7568 * If dio write length has been split due to no large enough
7569 * contiguous space, we need to compensate our inode counter
7570 * appropriately.
7571 */
7572 u64 num_needed = num_extents - dio_data->outstanding_extents;
7573
Filipe Manana9c9464c2015-11-04 09:52:04 +00007574 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007575 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007576 spin_unlock(&BTRFS_I(inode)->lock);
7577 }
7578}
7579
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7581 struct buffer_head *bh_result, int create)
7582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307586 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007589 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007591 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007592
Miao Xie172a5042013-02-21 02:48:22 -07007593 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007594 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007595 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007596 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007597
Josef Bacikc3298612012-08-03 16:49:19 -04007598 lockstart = start;
7599 lockend = start + len - 1;
7600
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007601 if (current->journal_info) {
7602 /*
7603 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007604 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007605 * confused.
7606 */
chandan50745b02015-08-28 21:10:13 +05307607 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007608 current->journal_info = NULL;
7609 }
7610
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
7612 * If this errors out it's because we couldn't invalidate pagecache for
7613 * this range and we need to fallback to buffered.
7614 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007615 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7616 create)) {
7617 ret = -ENOTBLK;
7618 goto err;
7619 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007620
Josef Bacik4b46fce2010-05-23 11:00:55 -04007621 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 if (IS_ERR(em)) {
7623 ret = PTR_ERR(em);
7624 goto unlock_err;
7625 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626
7627 /*
7628 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7629 * io. INLINE is special, and we could probably kludge it in here, but
7630 * it's still buffered so for safety lets just fall back to the generic
7631 * buffered path.
7632 *
7633 * For COMPRESSED we _have_ to read the entire extent in so we can
7634 * decompress it, so there will be buffering required no matter what we
7635 * do, so go ahead and fallback to buffered.
7636 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007637 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007638 * to buffered IO. Don't blame me, this is the price we pay for using
7639 * the generic code.
7640 */
7641 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7642 em->block_start == EXTENT_MAP_INLINE) {
7643 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007644 ret = -ENOTBLK;
7645 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007646 }
7647
7648 /* Just a good old fashioned hole, return */
7649 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7650 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7651 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007652 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 }
7654
7655 /*
7656 * We don't allocate a new extent in the following cases
7657 *
7658 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7659 * existing extent.
7660 * 2) The extent is marked as PREALLOC. We're good to go here and can
7661 * just use the extent.
7662 *
7663 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007664 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 len = min(len, em->len - (start - em->start));
7666 lockstart = start + len;
7667 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007668 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669
7670 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7671 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7672 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007673 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007674 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675
7676 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7677 type = BTRFS_ORDERED_PREALLOC;
7678 else
7679 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007680 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007682
Josef Bacik00361582013-08-14 14:02:47 -04007683 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007684 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007685 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007686 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007687
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007688 em2 = btrfs_create_dio_extent(inode, start, len,
7689 orig_start, block_start,
7690 len, orig_block_len,
7691 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007692 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007693 if (type == BTRFS_ORDERED_PREALLOC) {
7694 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007695 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007696 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007697 if (em2 && IS_ERR(em2)) {
7698 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007699 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007700 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007701 /*
7702 * For inode marked NODATACOW or extent marked PREALLOC,
7703 * use the existing or preallocated extent, so does not
7704 * need to adjust btrfs_space_info's bytes_may_use.
7705 */
7706 btrfs_free_reserved_data_space_noquota(inode,
7707 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007708 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 }
Josef Bacik00361582013-08-14 14:02:47 -04007711
Chris Mason46bfbb52010-05-26 11:04:10 -04007712 /*
7713 * this will cow the extent, reset the len in case we changed
7714 * it above
7715 */
7716 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007717 free_extent_map(em);
7718 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 if (IS_ERR(em)) {
7720 ret = PTR_ERR(em);
7721 goto unlock_err;
7722 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 len = min(len, em->len - (start - em->start));
7724unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7726 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007727 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 bh_result->b_bdev = em->bdev;
7729 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007730 if (create) {
7731 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7732 set_buffer_new(bh_result);
7733
7734 /*
7735 * Need to update the i_size under the extent lock so buffered
7736 * readers will get the updated i_size when we unlock.
7737 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007738 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007739 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007740
Filipe Manana9c9464c2015-11-04 09:52:04 +00007741 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307742 WARN_ON(dio_data->reserve < len);
7743 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007744 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307745 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007746 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 /*
7749 * In the case of write we need to clear and unlock the entire range,
7750 * in the case of read we need to unlock only the end area that we
7751 * aren't using if there is any left over space.
7752 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007753 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007754 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7755 lockend, unlock_bits, 1, 0,
7756 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007757 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007758 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007759 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007760
Josef Bacik4b46fce2010-05-23 11:00:55 -04007761 free_extent_map(em);
7762
7763 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007764
7765unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7767 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007768err:
chandan50745b02015-08-28 21:10:13 +05307769 if (dio_data)
7770 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007771 /*
7772 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7773 * write less data then expected, so that we don't underflow our inode's
7774 * outstanding extents counter.
7775 */
7776 if (create && dio_data)
7777 adjust_dio_outstanding_extents(inode, dio_data, len);
7778
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007780}
7781
Miao Xie8b110e32014-09-12 18:44:03 +08007782static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007783 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007784{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007786 int ret;
7787
Mike Christie37226b22016-06-05 14:31:52 -05007788 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007789
7790 bio_get(bio);
7791
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007792 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007793 if (ret)
7794 goto err;
7795
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007796 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007797err:
7798 bio_put(bio);
7799 return ret;
7800}
7801
7802static int btrfs_check_dio_repairable(struct inode *inode,
7803 struct bio *failed_bio,
7804 struct io_failure_record *failrec,
7805 int failed_mirror)
7806{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007808 int num_copies;
7809
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007810 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007811 if (num_copies == 1) {
7812 /*
7813 * we only have a single copy of the data, so don't bother with
7814 * all the retry and error correction code that follows. no
7815 * matter what the error is, it is very likely to persist.
7816 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007817 btrfs_debug(fs_info,
7818 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7819 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007820 return 0;
7821 }
7822
7823 failrec->failed_mirror = failed_mirror;
7824 failrec->this_mirror++;
7825 if (failrec->this_mirror == failed_mirror)
7826 failrec->this_mirror++;
7827
7828 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007829 btrfs_debug(fs_info,
7830 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7831 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007832 return 0;
7833 }
7834
7835 return 1;
7836}
7837
7838static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307839 struct page *page, unsigned int pgoff,
7840 u64 start, u64 end, int failed_mirror,
7841 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007842{
7843 struct io_failure_record *failrec;
7844 struct bio *bio;
7845 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007846 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007847 int ret;
7848
Mike Christie37226b22016-06-05 14:31:52 -05007849 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007850
7851 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7852 if (ret)
7853 return ret;
7854
7855 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7856 failed_mirror);
7857 if (!ret) {
7858 free_io_failure(inode, failrec);
7859 return -EIO;
7860 }
7861
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307862 if ((failed_bio->bi_vcnt > 1)
7863 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007864 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007865 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007866
7867 isector = start - btrfs_io_bio(failed_bio)->logical;
7868 isector >>= inode->i_sb->s_blocksize_bits;
7869 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307870 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007871 if (!bio) {
7872 free_io_failure(inode, failrec);
7873 return -EIO;
7874 }
Mike Christie37226b22016-06-05 14:31:52 -05007875 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007876
7877 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7878 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7879 read_mode, failrec->this_mirror, failrec->in_validation);
7880
Mike Christie81a75f672016-06-05 14:31:54 -05007881 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007882 if (ret) {
7883 free_io_failure(inode, failrec);
7884 bio_put(bio);
7885 }
7886
7887 return ret;
7888}
7889
7890struct btrfs_retry_complete {
7891 struct completion done;
7892 struct inode *inode;
7893 u64 start;
7894 int uptodate;
7895};
7896
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007897static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007898{
7899 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307900 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007901 struct bio_vec *bvec;
7902 int i;
7903
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007904 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007905 goto end;
7906
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307907 ASSERT(bio->bi_vcnt == 1);
7908 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007909 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307910
Miao Xie8b110e32014-09-12 18:44:03 +08007911 done->uptodate = 1;
7912 bio_for_each_segment_all(bvec, bio, i)
7913 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7914end:
7915 complete(&done->done);
7916 bio_put(bio);
7917}
7918
7919static int __btrfs_correct_data_nocsum(struct inode *inode,
7920 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007921{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007923 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007924 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007925 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307926 unsigned int pgoff;
7927 u32 sectorsize;
7928 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007929 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007930 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007931
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007933 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307934
Miao Xiec1dc0892014-09-12 18:43:56 +08007935 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007936 done.inode = inode;
7937
7938 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7940 pgoff = bvec->bv_offset;
7941
7942next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007943 done.uptodate = 0;
7944 done.start = start;
7945 init_completion(&done.done);
7946
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307947 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7948 pgoff, start, start + sectorsize - 1,
7949 io_bio->mirror_num,
7950 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 if (ret)
7952 return ret;
7953
7954 wait_for_completion(&done.done);
7955
7956 if (!done.uptodate) {
7957 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 }
7960
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307961 start += sectorsize;
7962
7963 if (nr_sectors--) {
7964 pgoff += sectorsize;
7965 goto next_block_or_try_again;
7966 }
Miao Xie8b110e32014-09-12 18:44:03 +08007967 }
7968
7969 return 0;
7970}
7971
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007972static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007973{
7974 struct btrfs_retry_complete *done = bio->bi_private;
7975 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007977 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307978 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007979 int uptodate;
7980 int ret;
7981 int i;
7982
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007983 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007984 goto end;
7985
7986 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987
7988 start = done->start;
7989
7990 ASSERT(bio->bi_vcnt == 1);
7991 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007992 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993
Miao Xie8b110e32014-09-12 18:44:03 +08007994 bio_for_each_segment_all(bvec, bio, i) {
7995 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 bvec->bv_page, bvec->bv_offset,
7997 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007998 if (!ret)
7999 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008001 else
8002 uptodate = 0;
8003 }
8004
8005 done->uptodate = uptodate;
8006end:
8007 complete(&done->done);
8008 bio_put(bio);
8009}
8010
8011static int __btrfs_subio_endio_read(struct inode *inode,
8012 struct btrfs_io_bio *io_bio, int err)
8013{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008015 struct bio_vec *bvec;
8016 struct btrfs_retry_complete done;
8017 u64 start;
8018 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 u32 sectorsize;
8020 int nr_sectors;
8021 unsigned int pgoff;
8022 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008023 int i;
8024 int ret;
8025
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008027 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308028
Miao Xie8b110e32014-09-12 18:44:03 +08008029 err = 0;
8030 start = io_bio->logical;
8031 done.inode = inode;
8032
Miao Xiec1dc0892014-09-12 18:43:56 +08008033 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308034 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8035
8036 pgoff = bvec->bv_offset;
8037next_block:
8038 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8039 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8040 bvec->bv_page, pgoff, start,
8041 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008042 if (likely(!ret))
8043 goto next;
8044try_again:
8045 done.uptodate = 0;
8046 done.start = start;
8047 init_completion(&done.done);
8048
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8050 pgoff, start, start + sectorsize - 1,
8051 io_bio->mirror_num,
8052 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008053 if (ret) {
8054 err = ret;
8055 goto next;
8056 }
8057
8058 wait_for_completion(&done.done);
8059
8060 if (!done.uptodate) {
8061 /* We might have another mirror, so try again */
8062 goto try_again;
8063 }
8064next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 offset += sectorsize;
8066 start += sectorsize;
8067
8068 ASSERT(nr_sectors);
8069
8070 if (--nr_sectors) {
8071 pgoff += sectorsize;
8072 goto next_block;
8073 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008074 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008075
8076 return err;
8077}
8078
Miao Xie8b110e32014-09-12 18:44:03 +08008079static int btrfs_subio_endio_read(struct inode *inode,
8080 struct btrfs_io_bio *io_bio, int err)
8081{
8082 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8083
8084 if (skip_csum) {
8085 if (unlikely(err))
8086 return __btrfs_correct_data_nocsum(inode, io_bio);
8087 else
8088 return 0;
8089 } else {
8090 return __btrfs_subio_endio_read(inode, io_bio, err);
8091 }
8092}
8093
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008094static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008095{
8096 struct btrfs_dio_private *dip = bio->bi_private;
8097 struct inode *inode = dip->inode;
8098 struct bio *dio_bio;
8099 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008100 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008101
Miao Xie8b110e32014-09-12 18:44:03 +08008102 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8103 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008104
Josef Bacik4b46fce2010-05-23 11:00:55 -04008105 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008106 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008107 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108
Josef Bacik4b46fce2010-05-23 11:00:55 -04008109 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008110
Filipe Manana1636d1d2016-02-15 16:20:26 +00008111 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008112 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008113
8114 if (io_bio->end_io)
8115 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008116 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008117}
8118
Filipe Manana14543772015-11-24 16:23:54 +00008119static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8120 const u64 offset,
8121 const u64 bytes,
8122 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008125 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008126 u64 ordered_offset = offset;
8127 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128 int ret;
8129
Chris Mason163cf092010-11-28 19:56:33 -05008130again:
8131 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8132 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008133 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008134 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008135 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008136 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008137
Liu Bo9e0af232014-08-15 23:36:53 +08008138 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8139 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008140 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008141out_test:
8142 /*
8143 * our bio might span multiple ordered extents. If we haven't
8144 * completed the accounting for the whole dio, go back and try again
8145 */
Filipe Manana14543772015-11-24 16:23:54 +00008146 if (ordered_offset < offset + bytes) {
8147 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008148 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008149 goto again;
8150 }
Filipe Manana14543772015-11-24 16:23:54 +00008151}
8152
8153static void btrfs_endio_direct_write(struct bio *bio)
8154{
8155 struct btrfs_dio_private *dip = bio->bi_private;
8156 struct bio *dio_bio = dip->dio_bio;
8157
8158 btrfs_endio_direct_write_update_ordered(dip->inode,
8159 dip->logical_offset,
8160 dip->bytes,
8161 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008164
Filipe Manana1636d1d2016-02-15 16:20:26 +00008165 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008166 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008167 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008168}
8169
Mike Christie81a75f672016-06-05 14:31:54 -05008170static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008171 struct bio *bio, int mirror_num,
8172 unsigned long bio_flags, u64 offset)
8173{
8174 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008175 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008176 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008177 return 0;
8178}
8179
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008180static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008181{
8182 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008184
Miao Xie8b110e32014-09-12 18:44:03 +08008185 if (err)
8186 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008187 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008188 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8189 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008190 (unsigned long long)bio->bi_iter.bi_sector,
8191 bio->bi_iter.bi_size, err);
8192
8193 if (dip->subio_endio)
8194 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008195
8196 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008197 dip->errors = 1;
8198
8199 /*
8200 * before atomic variable goto zero, we must make sure
8201 * dip->errors is perceived to be set.
8202 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008203 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008204 }
8205
8206 /* if there are more bios still pending for this dio, just exit */
8207 if (!atomic_dec_and_test(&dip->pending_bios))
8208 goto out;
8209
Chris Mason9be33952013-05-17 18:30:14 -04008210 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008211 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008212 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008213 dip->dio_bio->bi_error = 0;
8214 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008215 }
8216out:
8217 bio_put(bio);
8218}
8219
8220static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8221 u64 first_sector, gfp_t gfp_flags)
8222{
Chris Masonda2f0f72015-07-02 13:57:22 -07008223 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008224 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008225 if (bio)
8226 bio_associate_current(bio);
8227 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008228}
8229
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008230static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008231 struct btrfs_dio_private *dip,
8232 struct bio *bio,
8233 u64 file_offset)
8234{
8235 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8236 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8237 int ret;
8238
8239 /*
8240 * We load all the csum data we need when we submit
8241 * the first bio to reduce the csum tree search and
8242 * contention.
8243 */
8244 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008245 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008246 file_offset);
8247 if (ret)
8248 return ret;
8249 }
8250
8251 if (bio == dip->orig_bio)
8252 return 0;
8253
8254 file_offset -= dip->logical_offset;
8255 file_offset >>= inode->i_sb->s_blocksize_bits;
8256 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8257
8258 return 0;
8259}
8260
Miao Xiee65e1532010-11-22 03:04:43 +00008261static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008262 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008263 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008266 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008267 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008268 int ret;
8269
Josef Bacikb812ce22012-11-16 13:56:32 -05008270 if (async_submit)
8271 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8272
Miao Xiee65e1532010-11-22 03:04:43 +00008273 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008274
8275 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008276 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008277 if (ret)
8278 goto err;
8279 }
Miao Xiee65e1532010-11-22 03:04:43 +00008280
Josef Bacik1ae39932011-04-06 14:41:34 -04008281 if (skip_sum)
8282 goto map;
8283
8284 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008285 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8286 file_offset,
8287 __btrfs_submit_bio_start_direct_io,
8288 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008289 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008290 } else if (write) {
8291 /*
8292 * If we aren't doing async submit, calculate the csum of the
8293 * bio now.
8294 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008295 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008296 if (ret)
8297 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008298 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008299 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008300 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008301 if (ret)
8302 goto err;
8303 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008304map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008305 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008306err:
8307 bio_put(bio);
8308 return ret;
8309}
8310
Mike Christie81a75f672016-06-05 14:31:54 -05008311static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008312 int skip_sum)
8313{
8314 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008316 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008317 struct bio *bio;
8318 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008319 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008320 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008321 u64 file_offset = dip->logical_offset;
8322 u64 submit_len = 0;
8323 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008324 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008325 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308326 int nr_sectors;
8327 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008328 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008329
Kent Overstreet4f024f32013-10-11 15:44:27 -07008330 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008331 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8332 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008333 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008334 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008335
Kent Overstreet4f024f32013-10-11 15:44:27 -07008336 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008337 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008338 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008339 goto submit;
8340 }
8341
David Woodhouse53b381b2013-01-29 18:40:14 -05008342 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008343 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008344 async_submit = 0;
8345 else
8346 async_submit = 1;
8347
Josef Bacik02f57c72011-04-06 14:25:44 -04008348 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8349 if (!bio)
8350 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008351
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008352 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008353 bio->bi_private = dip;
8354 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008355 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008356 atomic_inc(&dip->pending_bios);
8357
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008358 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008359 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308360 i = 0;
8361next_block:
8362 if (unlikely(map_length < submit_len + blocksize ||
8363 bio_add_page(bio, bvec->bv_page, blocksize,
8364 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008365 /*
8366 * inc the count before we submit the bio so
8367 * we know the end IO handler won't happen before
8368 * we inc the count. Otherwise, the dip might get freed
8369 * before we're done setting it up
8370 */
8371 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008372 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008373 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008374 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008375 if (ret) {
8376 bio_put(bio);
8377 atomic_dec(&dip->pending_bios);
8378 goto out_err;
8379 }
8380
Miao Xiee65e1532010-11-22 03:04:43 +00008381 start_sector += submit_len >> 9;
8382 file_offset += submit_len;
8383
8384 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008385
8386 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8387 start_sector, GFP_NOFS);
8388 if (!bio)
8389 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008390 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008391 bio->bi_private = dip;
8392 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008393 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008394
Kent Overstreet4f024f32013-10-11 15:44:27 -07008395 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008396 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008397 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008398 &map_length, NULL, 0);
8399 if (ret) {
8400 bio_put(bio);
8401 goto out_err;
8402 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308403
8404 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008405 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308406 submit_len += blocksize;
8407 if (--nr_sectors) {
8408 i++;
8409 goto next_block;
8410 }
Miao Xiee65e1532010-11-22 03:04:43 +00008411 }
8412 }
8413
Josef Bacik02f57c72011-04-06 14:25:44 -04008414submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008415 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008416 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008417 if (!ret)
8418 return 0;
8419
8420 bio_put(bio);
8421out_err:
8422 dip->errors = 1;
8423 /*
8424 * before atomic variable goto zero, we must
8425 * make sure dip->errors is perceived to be set.
8426 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008427 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008428 if (atomic_dec_and_test(&dip->pending_bios))
8429 bio_io_error(dip->orig_bio);
8430
8431 /* bio_end_io() will handle error, so we needn't return it */
8432 return 0;
8433}
8434
Mike Christie8a4c1e42016-06-05 14:31:50 -05008435static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8436 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008437{
Filipe Manana61de7182015-07-01 12:13:10 +01008438 struct btrfs_dio_private *dip = NULL;
8439 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008440 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008441 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008442 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008443 int ret = 0;
8444
8445 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8446
Chris Mason9be33952013-05-17 18:30:14 -04008447 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008448 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008449 ret = -ENOMEM;
8450 goto free_ordered;
8451 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008452
Miao Xiec1dc0892014-09-12 18:43:56 +08008453 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008454 if (!dip) {
8455 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008456 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008457 }
8458
8459 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008460 dip->inode = inode;
8461 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008462 dip->bytes = dio_bio->bi_iter.bi_size;
8463 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008464 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008465 dip->orig_bio = io_bio;
8466 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008467 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008468 btrfs_bio = btrfs_io_bio(io_bio);
8469 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008470
Miao Xiec1dc0892014-09-12 18:43:56 +08008471 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008472 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008473 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008474 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008475 dip->subio_endio = btrfs_subio_endio_read;
8476 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008477
Filipe Mananaf28a4922015-12-08 19:23:20 +00008478 /*
8479 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8480 * even if we fail to submit a bio, because in such case we do the
8481 * corresponding error handling below and it must not be done a second
8482 * time by btrfs_direct_IO().
8483 */
8484 if (write) {
8485 struct btrfs_dio_data *dio_data = current->journal_info;
8486
8487 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8488 dip->bytes;
8489 dio_data->unsubmitted_oe_range_start =
8490 dio_data->unsubmitted_oe_range_end;
8491 }
8492
Mike Christie81a75f672016-06-05 14:31:54 -05008493 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008494 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008495 return;
Chris Mason9be33952013-05-17 18:30:14 -04008496
Miao Xie23ea8e52014-09-12 18:43:54 +08008497 if (btrfs_bio->end_io)
8498 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008499
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500free_ordered:
8501 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008502 * If we arrived here it means either we failed to submit the dip
8503 * or we either failed to clone the dio_bio or failed to allocate the
8504 * dip. If we cloned the dio_bio and allocated the dip, we can just
8505 * call bio_endio against our io_bio so that we get proper resource
8506 * cleanup if we fail to submit the dip, otherwise, we must do the
8507 * same as btrfs_endio_direct_[write|read] because we can't call these
8508 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 */
Filipe Manana61de7182015-07-01 12:13:10 +01008510 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008511 io_bio->bi_error = -EIO;
8512 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008513 /*
8514 * The end io callbacks free our dip, do the final put on io_bio
8515 * and all the cleanup and final put for dio_bio (through
8516 * dio_end_io()).
8517 */
8518 dip = NULL;
8519 io_bio = NULL;
8520 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008521 if (write)
8522 btrfs_endio_direct_write_update_ordered(inode,
8523 file_offset,
8524 dio_bio->bi_iter.bi_size,
8525 0);
8526 else
Filipe Manana61de7182015-07-01 12:13:10 +01008527 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8528 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008529
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008530 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008531 /*
8532 * Releases and cleans up our dio_bio, no need to bio_put()
8533 * nor bio_endio()/bio_io_error() against dio_bio.
8534 */
8535 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008536 }
Filipe Manana61de7182015-07-01 12:13:10 +01008537 if (io_bio)
8538 bio_put(io_bio);
8539 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008540}
8541
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008542static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8543 struct kiocb *iocb,
8544 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008545{
8546 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008547 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008548 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008549 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008550
8551 if (offset & blocksize_mask)
8552 goto out;
8553
Al Viro28060d52014-03-22 05:15:17 -04008554 if (iov_iter_alignment(iter) & blocksize_mask)
8555 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008556
Al Viro28060d52014-03-22 05:15:17 -04008557 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008558 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008559 return 0;
8560 /*
8561 * Check to make sure we don't have duplicate iov_base's in this
8562 * iovec, if so return EINVAL, otherwise we'll get csum errors
8563 * when reading back.
8564 */
8565 for (seg = 0; seg < iter->nr_segs; seg++) {
8566 for (i = seg + 1; i < iter->nr_segs; i++) {
8567 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008568 goto out;
8569 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008570 }
8571 retval = 0;
8572out:
8573 return retval;
8574}
Josef Bacikeb838e72012-07-31 16:28:48 -04008575
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008576static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008577{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008578 struct file *file = iocb->ki_filp;
8579 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008580 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308581 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008582 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008583 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008584 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008585 bool wakeup = true;
8586 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008587 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008588
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008589 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008590 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008591
Jens Axboefe0f07d2015-04-15 17:05:48 -06008592 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008593 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008594
Josef Bacik0e267c42013-07-02 10:38:02 -04008595 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008596 * The generic stuff only does filemap_write_and_wait_range, which
8597 * isn't enough if we've written compressed pages to this area, so
8598 * we need to flush the dirty pages again to make absolutely sure
8599 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008600 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008601 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008602 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8603 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008604 filemap_fdatawrite_range(inode->i_mapping, offset,
8605 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008606
Omar Sandoval6f673762015-03-16 04:33:52 -07008607 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008608 /*
8609 * If the write DIO is beyond the EOF, we need update
8610 * the isize, but it is protected by i_mutex. So we can
8611 * not unlock the i_mutex at this case.
8612 */
8613 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008614 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008615 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008616 relock = true;
8617 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008618 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008619 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008620 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008621 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008622
8623 /*
8624 * We need to know how many extents we reserved so that we can
8625 * do the accounting properly if we go over the number we
8626 * originally calculated. Abuse current->journal_info for this.
8627 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008628 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008629 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008630 dio_data.unsubmitted_oe_range_start = (u64)offset;
8631 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308632 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308633 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008634 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8635 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008636 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008637 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8638 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008639 }
8640
Omar Sandoval17f8c842015-03-16 04:33:50 -07008641 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008642 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008643 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008644 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008645 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308646 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008647 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008648 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308649 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008650 btrfs_delalloc_release_space(inode, offset,
8651 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008652 /*
8653 * On error we might have left some ordered extents
8654 * without submitting corresponding bios for them, so
8655 * cleanup them up to avoid other tasks getting them
8656 * and waiting for them to complete forever.
8657 */
8658 if (dio_data.unsubmitted_oe_range_start <
8659 dio_data.unsubmitted_oe_range_end)
8660 btrfs_endio_direct_write_update_ordered(inode,
8661 dio_data.unsubmitted_oe_range_start,
8662 dio_data.unsubmitted_oe_range_end -
8663 dio_data.unsubmitted_oe_range_start,
8664 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008665 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008666 btrfs_delalloc_release_space(inode, offset,
8667 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008668 }
Miao Xie38851cc2013-02-08 07:04:11 +00008669out:
Miao Xie2e60a512013-02-08 07:01:08 +00008670 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008671 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008672 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008673 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008674
8675 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008676}
8677
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008678#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8679
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008680static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8681 __u64 start, __u64 len)
8682{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008683 int ret;
8684
8685 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8686 if (ret)
8687 return ret;
8688
Chris Masonec29ed52011-02-23 16:23:20 -05008689 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008690}
8691
Chris Mason9ebefb182007-06-15 13:50:00 -04008692int btrfs_readpage(struct file *file, struct page *page)
8693{
Chris Masond1310b22008-01-24 16:13:08 -05008694 struct extent_io_tree *tree;
8695 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008696 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008697}
Chris Mason1832a6d2007-12-21 16:27:21 -05008698
Chris Mason39279cc2007-06-12 06:35:45 -04008699static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8700{
Chris Masond1310b22008-01-24 16:13:08 -05008701 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008702 struct inode *inode = page->mapping->host;
8703 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008704
8705 if (current->flags & PF_MEMALLOC) {
8706 redirty_page_for_writepage(wbc, page);
8707 unlock_page(page);
8708 return 0;
8709 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008710
8711 /*
8712 * If we are under memory pressure we will call this directly from the
8713 * VM, we need to make sure we have the inode referenced for the ordered
8714 * extent. If not just return like we didn't do anything.
8715 */
8716 if (!igrab(inode)) {
8717 redirty_page_for_writepage(wbc, page);
8718 return AOP_WRITEPAGE_ACTIVATE;
8719 }
Chris Masond1310b22008-01-24 16:13:08 -05008720 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008721 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8722 btrfs_add_delayed_iput(inode);
8723 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008724}
Chris Mason39279cc2007-06-12 06:35:45 -04008725
Eric Sandeen48a3b632013-04-25 20:41:01 +00008726static int btrfs_writepages(struct address_space *mapping,
8727 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008728{
Chris Masond1310b22008-01-24 16:13:08 -05008729 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008730
Chris Masond1310b22008-01-24 16:13:08 -05008731 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008732 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8733}
8734
Chris Mason3ab2fb52007-11-08 10:59:22 -05008735static int
8736btrfs_readpages(struct file *file, struct address_space *mapping,
8737 struct list_head *pages, unsigned nr_pages)
8738{
Chris Masond1310b22008-01-24 16:13:08 -05008739 struct extent_io_tree *tree;
8740 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008741 return extent_readpages(tree, mapping, pages, nr_pages,
8742 btrfs_get_extent);
8743}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008744static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008745{
Chris Masond1310b22008-01-24 16:13:08 -05008746 struct extent_io_tree *tree;
8747 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008748 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008749
Chris Masond1310b22008-01-24 16:13:08 -05008750 tree = &BTRFS_I(page->mapping->host)->io_tree;
8751 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008752 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008753 if (ret == 1) {
8754 ClearPagePrivate(page);
8755 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008756 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008757 }
8758 return ret;
8759}
Chris Mason39279cc2007-06-12 06:35:45 -04008760
Chris Masone6dcd2d2008-07-17 12:53:50 -04008761static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8762{
Chris Mason98509cf2008-09-11 15:51:43 -04008763 if (PageWriteback(page) || PageDirty(page))
8764 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008765 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766}
8767
Lukas Czernerd47992f2013-05-21 23:17:23 -04008768static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8769 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008770{
Josef Bacik5fd02042012-05-02 14:00:54 -04008771 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008772 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008774 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008776 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308777 u64 start;
8778 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008779 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008780
Chris Mason8b62b722009-09-02 16:53:46 -04008781 /*
8782 * we have the page locked, so new writeback can't start,
8783 * and the dirty bit won't be cleared while we are here.
8784 *
8785 * Wait for IO on this page so that we can safely clear
8786 * the PagePrivate2 bit and do ordered accounting
8787 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008788 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008789
Josef Bacik5fd02042012-05-02 14:00:54 -04008790 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008791 if (offset) {
8792 btrfs_releasepage(page, GFP_NOFS);
8793 return;
8794 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008795
8796 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008797 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308798again:
8799 start = page_start;
8800 ordered = btrfs_lookup_ordered_range(inode, start,
8801 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308803 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008804 /*
8805 * IO on this page will never be started, so we need
8806 * to account for any ordered extents now
8807 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008808 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308809 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008810 EXTENT_DIRTY | EXTENT_DELALLOC |
8811 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8812 EXTENT_DEFRAG, 1, 0, &cached_state,
8813 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008814 /*
8815 * whoever cleared the private bit is responsible
8816 * for the finish_ordered_io
8817 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008818 if (TestClearPagePrivate2(page)) {
8819 struct btrfs_ordered_inode_tree *tree;
8820 u64 new_len;
8821
8822 tree = &BTRFS_I(inode)->ordered_tree;
8823
8824 spin_lock_irq(&tree->lock);
8825 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308826 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008827 if (new_len < ordered->truncated_len)
8828 ordered->truncated_len = new_len;
8829 spin_unlock_irq(&tree->lock);
8830
8831 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308832 start,
8833 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008834 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008835 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008837 if (!inode_evicting) {
8838 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308839 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008840 &cached_state);
8841 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842
8843 start = end + 1;
8844 if (start < page_end)
8845 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008847
Qu Wenruob9d0b382015-09-29 10:35:16 +08008848 /*
8849 * Qgroup reserved space handler
8850 * Page here will be either
8851 * 1) Already written to disk
8852 * In this case, its reserved space is released from data rsv map
8853 * and will be freed by delayed_ref handler finally.
8854 * So even we call qgroup_free_data(), it won't decrease reserved
8855 * space.
8856 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008857 * This means the reserved space should be freed here. However,
8858 * if a truncate invalidates the page (by clearing PageDirty)
8859 * and the page is accounted for while allocating extent
8860 * in btrfs_check_data_free_space() we let delayed_ref to
8861 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008862 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008863 if (PageDirty(page))
8864 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008865 if (!inode_evicting) {
8866 clear_extent_bit(tree, page_start, page_end,
8867 EXTENT_LOCKED | EXTENT_DIRTY |
8868 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8869 EXTENT_DEFRAG, 1, 1,
8870 &cached_state, GFP_NOFS);
8871
8872 __btrfs_releasepage(page, GFP_NOFS);
8873 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008874
Chris Mason4a096752008-07-21 10:29:44 -04008875 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008876 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008877 ClearPagePrivate(page);
8878 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008879 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008880 }
Chris Mason39279cc2007-06-12 06:35:45 -04008881}
8882
Chris Mason9ebefb182007-06-15 13:50:00 -04008883/*
8884 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8885 * called from a page fault handler when a page is first dirtied. Hence we must
8886 * be careful to check for EOF conditions here. We set the page up correctly
8887 * for a written page which means we get ENOSPC checking when writing into
8888 * holes and correct delalloc and unwritten extent mapping on filesystems that
8889 * support these features.
8890 *
8891 * We are not allowed to take the i_mutex here so we have to play games to
8892 * protect against truncate races as the page could now be beyond EOF. Because
8893 * vmtruncate() writes the inode size before removing pages, once we have the
8894 * page lock we can determine safely if the page is beyond EOF. If it is not
8895 * beyond EOF, then the page is guaranteed safe against truncation until we
8896 * unlock the page.
8897 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008898int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008899{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008900 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008901 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008903 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8904 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008905 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 char *kaddr;
8907 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008908 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008909 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008910 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308911 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008912 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308914 u64 end;
8915
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008916 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008917
Jan Karab2b5ef52012-06-12 16:20:45 +02008918 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008919 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008920 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308921 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008922
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308923 /*
8924 * Reserving delalloc space after obtaining the page lock can lead to
8925 * deadlock. For example, if a dirty page is locked by this function
8926 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8927 * dirty page write out, then the btrfs_writepage() function could
8928 * end up waiting indefinitely to get a lock on the page currently
8929 * being processed by btrfs_page_mkwrite() function.
8930 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008931 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308932 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008933 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008934 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008935 reserved = 1;
8936 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008937 if (ret) {
8938 if (ret == -ENOMEM)
8939 ret = VM_FAULT_OOM;
8940 else /* -ENOSPC, -EIO, etc */
8941 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008942 if (reserved)
8943 goto out;
8944 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008945 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008946
Nick Piggin56a76f82009-03-31 15:23:23 -07008947 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008951
Chris Mason9ebefb182007-06-15 13:50:00 -04008952 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008954 /* page got truncated out from underneath us */
8955 goto out_unlock;
8956 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 wait_on_page_writeback(page);
8958
David Sterbaff13db42015-12-03 14:30:40 +01008959 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008960 set_page_extent_mapped(page);
8961
Chris Masoneb84ae02008-07-17 13:53:27 -04008962 /*
8963 * we can't set the delalloc bits if there are pending ordered
8964 * extents. Drop our locks and wait for them to finish
8965 */
Liu Bo9a9239a2017-01-24 15:58:51 -08008966 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008967 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008968 unlock_extent_cached(io_tree, page_start, page_end,
8969 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008971 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 btrfs_put_ordered_extent(ordered);
8973 goto again;
8974 }
8975
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008976 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008977 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008978 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308980 end = page_start + reserved_space - 1;
8981 spin_lock(&BTRFS_I(inode)->lock);
8982 BTRFS_I(inode)->outstanding_extents++;
8983 spin_unlock(&BTRFS_I(inode)->lock);
8984 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 }
8987 }
8988
Josef Bacikfbf19082009-10-01 17:10:23 -04008989 /*
Liu Bo54160342016-12-13 12:15:19 -08008990 * page_mkwrite gets called when the page is firstly dirtied after it's
8991 * faulted in, but write(2) could also dirty a page and set delalloc
8992 * bits, thus in this case for space account reason, we still need to
8993 * clear any delalloc bits within this page range since we have to
8994 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008995 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008997 EXTENT_DIRTY | EXTENT_DELALLOC |
8998 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008999 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009000
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309001 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009002 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009003 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009004 unlock_extent_cached(io_tree, page_start, page_end,
9005 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009006 ret = VM_FAULT_SIGBUS;
9007 goto out_unlock;
9008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009010
9011 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009012 if (page_start + PAGE_SIZE > size)
9013 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009014 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009015 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009016
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009017 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 flush_dcache_page(page);
9021 kunmap(page);
9022 }
Chris Mason247e7432008-07-17 12:53:51 -04009023 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009025 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009026
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009027 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009028 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009029 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009030
Josef Bacik2ac55d42010-02-03 19:33:23 +00009031 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009032
9033out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009034 if (!ret) {
9035 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009036 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009037 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009038 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009039out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309040 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009041out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009042 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009043 return ret;
9044}
9045
Josef Bacika41ad392011-01-31 15:30:16 -05009046static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009047{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009048 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009049 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009050 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009051 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009052 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009053 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009054 u64 mask = fs_info->sectorsize - 1;
9055 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009056
Josef Bacik0ef8b722013-10-25 16:13:35 -04009057 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9058 (u64)-1);
9059 if (ret)
9060 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009061
Josef Bacikfcb80c22011-05-03 10:40:22 -04009062 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009063 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 * 3 things going on here
9065 *
9066 * 1) We need to reserve space for our orphan item and the space to
9067 * delete our orphan item. Lord knows we don't want to have a dangling
9068 * orphan item because we didn't reserve space to remove it.
9069 *
9070 * 2) We need to reserve space to update our inode.
9071 *
9072 * 3) We need to have something to cache all the space that is going to
9073 * be free'd up by the truncate operation, but also have some slack
9074 * space reserved in case it uses space during the truncate (thank you
9075 * very much snapshotting).
9076 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009077 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009079 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009080 * doesn't end up using space reserved for updating the inode or
9081 * removing the orphan item. We also need to be able to stop the
9082 * transaction and start a new one, which means we need to be able to
9083 * update the inode several times, and we have no idea of knowing how
9084 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009085 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009086 * Then there is the orphan item, which does indeed need to be held on
9087 * to for the whole operation, and we need nobody to touch this reserved
9088 * space except the orphan code.
9089 *
9090 * So that leaves us with
9091 *
9092 * 1) root->orphan_block_rsv - for the orphan deletion.
9093 * 2) rsv - for the truncate reservation, which we will steal from the
9094 * transaction reservation.
9095 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9096 * updating the inode.
9097 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009098 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009099 if (!rsv)
9100 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009101 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009102 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009103
Josef Bacik907cbce2011-08-08 13:46:15 -04009104 /*
Josef Bacik07127182011-08-19 10:29:59 -04009105 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009106 * 1 for updating the inode.
9107 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009108 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009109 if (IS_ERR(trans)) {
9110 err = PTR_ERR(trans);
9111 goto out;
9112 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009113
Josef Bacik907cbce2011-08-08 13:46:15 -04009114 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009115 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009116 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009117 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009118
Chris Mason5a3f23d2009-03-31 13:27:11 -04009119 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009120 * So if we truncate and then write and fsync we normally would just
9121 * write the extents that changed, which is a problem if we need to
9122 * first truncate that entire inode. So set this flag so we write out
9123 * all of the extents in the inode to the sync log so we're completely
9124 * safe.
9125 */
9126 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009127 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009128
Yan, Zheng80825102009-11-12 09:35:36 +00009129 while (1) {
9130 ret = btrfs_truncate_inode_items(trans, root, inode,
9131 inode->i_size,
9132 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009133 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009134 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009135 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009136 }
Chris Mason39279cc2007-06-12 06:35:45 -04009137
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009138 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009139 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009140 if (ret) {
9141 err = ret;
9142 break;
9143 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009144
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009145 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009146 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009147
9148 trans = btrfs_start_transaction(root, 2);
9149 if (IS_ERR(trans)) {
9150 ret = err = PTR_ERR(trans);
9151 trans = NULL;
9152 break;
9153 }
9154
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009155 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009156 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009157 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009158 BUG_ON(ret); /* shouldn't happen */
9159 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009160 }
9161
9162 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009164 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009165 if (ret)
9166 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009167 }
9168
Chris Mason917c16b2011-11-08 14:49:59 -05009169 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009170 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009171 ret = btrfs_update_inode(trans, root, inode);
9172 if (ret && !err)
9173 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009174
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009175 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009176 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009177 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009179 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009180
Josef Bacik3893e332011-01-31 16:03:11 -05009181 if (ret && !err)
9182 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009183
Josef Bacik3893e332011-01-31 16:03:11 -05009184 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009185}
9186
Sven Wegener3b963622008-06-09 21:57:42 -04009187/*
Chris Masond352ac62008-09-29 15:18:18 -04009188 * create a new subvolume directory/inode (helper for the ioctl).
9189 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009190int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009191 struct btrfs_root *new_root,
9192 struct btrfs_root *parent_root,
9193 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009194{
Chris Mason39279cc2007-06-12 06:35:45 -04009195 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009196 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009197 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009198
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009199 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9200 new_dirid, new_dirid,
9201 S_IFDIR | (~current_umask() & S_IRWXUGO),
9202 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009203 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009204 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009205 inode->i_op = &btrfs_dir_inode_operations;
9206 inode->i_fop = &btrfs_dir_file_operations;
9207
Miklos Szeredibfe86842011-10-28 14:13:29 +02009208 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009209 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009210 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009211
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009212 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9213 if (err)
9214 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009215 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009216 new_root->root_key.objectid, err);
9217
Yan, Zheng76dda932009-09-21 16:00:26 -04009218 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009219
Yan, Zheng76dda932009-09-21 16:00:26 -04009220 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009221 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009222}
9223
Chris Mason39279cc2007-06-12 06:35:45 -04009224struct inode *btrfs_alloc_inode(struct super_block *sb)
9225{
9226 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009227 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009228
9229 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9230 if (!ei)
9231 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009232
9233 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009234 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009235 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009236 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009237 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009239 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 ei->disk_i_size = 0;
9241 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009242 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009244 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009245 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009246 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009247 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009248
Josef Bacik9e0baf62011-07-15 15:16:44 +00009249 spin_lock_init(&ei->lock);
9250 ei->outstanding_extents = 0;
9251 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009252
Josef Bacik72ac3c02012-05-23 14:13:11 -04009253 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009254 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009255
Miao Xie16cdcec2011-04-22 18:12:22 +08009256 ei->delayed_node = NULL;
9257
chandan r9cc97d62012-07-04 12:48:07 +05309258 ei->i_otime.tv_sec = 0;
9259 ei->i_otime.tv_nsec = 0;
9260
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009261 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009262 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009263 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9264 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009265 ei->io_tree.track_uptodate = 1;
9266 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009267 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009268 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009269 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009270 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009271 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009272 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009274 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275
9276 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009277}
9278
Josef Bacikaaedb552013-10-11 14:44:09 -04009279#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9280void btrfs_test_destroy_inode(struct inode *inode)
9281{
9282 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9283 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9284}
9285#endif
9286
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009287static void btrfs_i_callback(struct rcu_head *head)
9288{
9289 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009290 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9291}
9292
Chris Mason39279cc2007-06-12 06:35:45 -04009293void btrfs_destroy_inode(struct inode *inode)
9294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009296 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009297 struct btrfs_root *root = BTRFS_I(inode)->root;
9298
Al Virob3d9b7a2012-06-09 13:51:19 -04009299 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009300 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009301 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9302 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009303 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9304 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009305 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009306
Chris Mason5a3f23d2009-03-31 13:27:11 -04009307 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009308 * This can happen where we create an inode, but somebody else also
9309 * created the same inode and we need to destroy the one we already
9310 * created.
9311 */
9312 if (!root)
9313 goto free;
9314
Josef Bacik8a35d952012-05-23 14:26:42 -04009315 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9316 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009317 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009318 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009319 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009320 }
Josef Bacik7b128762008-07-24 12:17:14 -04009321
Chris Masond3977122009-01-05 21:25:51 -05009322 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009323 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9324 if (!ordered)
9325 break;
9326 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009327 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009328 "found ordered extent %llu %llu on inode cleanup",
9329 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009330 btrfs_remove_ordered_extent(inode, ordered);
9331 btrfs_put_ordered_extent(ordered);
9332 btrfs_put_ordered_extent(ordered);
9333 }
9334 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009335 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009336 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009337 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009338free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009339 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009340}
9341
Al Viro45321ac2010-06-07 13:43:19 -04009342int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009343{
9344 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009345
Naohiro Aota6379ef92013-06-06 09:56:34 +00009346 if (root == NULL)
9347 return 1;
9348
Liu Bofa6ac872013-02-20 14:10:23 +00009349 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009350 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009351 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009352 else
Al Viro45321ac2010-06-07 13:43:19 -04009353 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009354}
9355
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009356static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009357{
9358 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9359
9360 inode_init_once(&ei->vfs_inode);
9361}
9362
9363void btrfs_destroy_cachep(void)
9364{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009365 /*
9366 * Make sure all delayed rcu free inodes are flushed before we
9367 * destroy cache.
9368 */
9369 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009370 kmem_cache_destroy(btrfs_inode_cachep);
9371 kmem_cache_destroy(btrfs_trans_handle_cachep);
9372 kmem_cache_destroy(btrfs_transaction_cachep);
9373 kmem_cache_destroy(btrfs_path_cachep);
9374 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009375}
9376
9377int btrfs_init_cachep(void)
9378{
David Sterba837e1972012-09-07 03:00:48 -06009379 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009380 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009381 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9382 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009383 if (!btrfs_inode_cachep)
9384 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009385
David Sterba837e1972012-09-07 03:00:48 -06009386 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009387 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009388 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009389 if (!btrfs_trans_handle_cachep)
9390 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009391
David Sterba837e1972012-09-07 03:00:48 -06009392 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009393 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009394 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 if (!btrfs_transaction_cachep)
9396 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009400 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 if (!btrfs_path_cachep)
9402 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403
David Sterba837e1972012-09-07 03:00:48 -06009404 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009405 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009406 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009407 if (!btrfs_free_space_cachep)
9408 goto fail;
9409
Chris Mason39279cc2007-06-12 06:35:45 -04009410 return 0;
9411fail:
9412 btrfs_destroy_cachep();
9413 return -ENOMEM;
9414}
9415
9416static int btrfs_getattr(struct vfsmount *mnt,
9417 struct dentry *dentry, struct kstat *stat)
9418{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009419 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009420 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009421 u32 blocksize = inode->i_sb->s_blocksize;
9422
Chris Mason39279cc2007-06-12 06:35:45 -04009423 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009424 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009425
9426 spin_lock(&BTRFS_I(inode)->lock);
9427 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9428 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009429 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009430 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009431 return 0;
9432}
9433
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009434static int btrfs_rename_exchange(struct inode *old_dir,
9435 struct dentry *old_dentry,
9436 struct inode *new_dir,
9437 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009439 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 struct btrfs_trans_handle *trans;
9441 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009442 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 struct inode *new_inode = new_dentry->d_inode;
9444 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009445 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009447 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9448 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009449 u64 old_idx = 0;
9450 u64 new_idx = 0;
9451 u64 root_objectid;
9452 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009453 bool root_log_pinned = false;
9454 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455
9456 /* we only allow rename subvolume link between subvolumes */
9457 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9458 return -EXDEV;
9459
9460 /* close the race window with snapshot create/destroy ioctl */
9461 if (old_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 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009464 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009465
9466 /*
9467 * We want to reserve the absolute worst case amount of items. So if
9468 * both inodes are subvols and we need to unlink them then that would
9469 * require 4 item modifications, but if they are both normal inodes it
9470 * would require 5 item modifications, so we'll assume their normal
9471 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9472 * should cover the worst case number of items we'll modify.
9473 */
9474 trans = btrfs_start_transaction(root, 12);
9475 if (IS_ERR(trans)) {
9476 ret = PTR_ERR(trans);
9477 goto out_notrans;
9478 }
9479
9480 /*
9481 * We need to find a free sequence number both in the source and
9482 * in the destination directory for the exchange.
9483 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009484 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 if (ret)
9486 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009487 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488 if (ret)
9489 goto out_fail;
9490
9491 BTRFS_I(old_inode)->dir_index = 0ULL;
9492 BTRFS_I(new_inode)->dir_index = 0ULL;
9493
9494 /* Reference for the source. */
9495 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9496 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009499 btrfs_pin_log_trans(root);
9500 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 ret = btrfs_insert_inode_ref(trans, dest,
9502 new_dentry->d_name.name,
9503 new_dentry->d_name.len,
9504 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009505 btrfs_ino(BTRFS_I(new_dir)),
9506 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 if (ret)
9508 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 }
9510
9511 /* And now for the dest. */
9512 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9513 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009514 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009516 btrfs_pin_log_trans(dest);
9517 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 ret = btrfs_insert_inode_ref(trans, root,
9519 old_dentry->d_name.name,
9520 old_dentry->d_name.len,
9521 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009522 btrfs_ino(BTRFS_I(old_dir)),
9523 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 if (ret)
9525 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 }
9527
9528 /* Update inode version and ctime/mtime. */
9529 inode_inc_iversion(old_dir);
9530 inode_inc_iversion(new_dir);
9531 inode_inc_iversion(old_inode);
9532 inode_inc_iversion(new_inode);
9533 old_dir->i_ctime = old_dir->i_mtime = ctime;
9534 new_dir->i_ctime = new_dir->i_mtime = ctime;
9535 old_inode->i_ctime = ctime;
9536 new_inode->i_ctime = ctime;
9537
9538 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009539 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9540 BTRFS_I(old_inode), 1);
9541 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9542 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 }
9544
9545 /* src is a subvolume */
9546 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9547 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9548 ret = btrfs_unlink_subvol(trans, root, old_dir,
9549 root_objectid,
9550 old_dentry->d_name.name,
9551 old_dentry->d_name.len);
9552 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009553 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9554 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 old_dentry->d_name.name,
9556 old_dentry->d_name.len);
9557 if (!ret)
9558 ret = btrfs_update_inode(trans, root, old_inode);
9559 }
9560 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009561 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 goto out_fail;
9563 }
9564
9565 /* dest is a subvolume */
9566 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9567 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9568 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9569 root_objectid,
9570 new_dentry->d_name.name,
9571 new_dentry->d_name.len);
9572 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009573 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9574 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 new_dentry->d_name.name,
9576 new_dentry->d_name.len);
9577 if (!ret)
9578 ret = btrfs_update_inode(trans, dest, new_inode);
9579 }
9580 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009581 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 goto out_fail;
9583 }
9584
9585 ret = btrfs_add_link(trans, new_dir, old_inode,
9586 new_dentry->d_name.name,
9587 new_dentry->d_name.len, 0, old_idx);
9588 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009589 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 goto out_fail;
9591 }
9592
9593 ret = btrfs_add_link(trans, old_dir, new_inode,
9594 old_dentry->d_name.name,
9595 old_dentry->d_name.len, 0, new_idx);
9596 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009597 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 goto out_fail;
9599 }
9600
9601 if (old_inode->i_nlink == 1)
9602 BTRFS_I(old_inode)->dir_index = old_idx;
9603 if (new_inode->i_nlink == 1)
9604 BTRFS_I(new_inode)->dir_index = new_idx;
9605
Filipe Manana86e8aa02016-05-05 02:02:27 +01009606 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009608 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9609 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009610 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009611 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009613 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009615 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9616 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009618 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 }
9620out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009621 /*
9622 * If we have pinned a log and an error happened, we unpin tasks
9623 * trying to sync the log and force them to fallback to a transaction
9624 * commit if the log currently contains any of the inodes involved in
9625 * this rename operation (to ensure we do not persist a log with an
9626 * inconsistent state for any of these inodes or leading to any
9627 * inconsistencies when replayed). If the transaction was aborted, the
9628 * abortion reason is propagated to userspace when attempting to commit
9629 * the transaction. If the log does not contain any of these inodes, we
9630 * allow the tasks to sync it.
9631 */
9632 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009633 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9634 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9635 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009636 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009637 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009638 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009639
9640 if (root_log_pinned) {
9641 btrfs_end_log_trans(root);
9642 root_log_pinned = false;
9643 }
9644 if (dest_log_pinned) {
9645 btrfs_end_log_trans(dest);
9646 dest_log_pinned = false;
9647 }
9648 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009649 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650out_notrans:
9651 if (new_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 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009654 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655
9656 return ret;
9657}
9658
9659static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9660 struct btrfs_root *root,
9661 struct inode *dir,
9662 struct dentry *dentry)
9663{
9664 int ret;
9665 struct inode *inode;
9666 u64 objectid;
9667 u64 index;
9668
9669 ret = btrfs_find_free_ino(root, &objectid);
9670 if (ret)
9671 return ret;
9672
9673 inode = btrfs_new_inode(trans, root, dir,
9674 dentry->d_name.name,
9675 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009676 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009677 objectid,
9678 S_IFCHR | WHITEOUT_MODE,
9679 &index);
9680
9681 if (IS_ERR(inode)) {
9682 ret = PTR_ERR(inode);
9683 return ret;
9684 }
9685
9686 inode->i_op = &btrfs_special_inode_operations;
9687 init_special_inode(inode, inode->i_mode,
9688 WHITEOUT_DEV);
9689
9690 ret = btrfs_init_inode_security(trans, inode, dir,
9691 &dentry->d_name);
9692 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009693 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694
9695 ret = btrfs_add_nondir(trans, dir, dentry,
9696 inode, 0, index);
9697 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009698 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699
9700 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009701out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009703 if (ret)
9704 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705 iput(inode);
9706
Filipe Mananac9901612016-05-05 01:41:57 +01009707 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708}
9709
Chris Mason39279cc2007-06-12 06:35:45 -04009710static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 struct inode *new_dir, struct dentry *new_dentry,
9712 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009714 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009715 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009716 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009717 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9718 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009719 struct inode *new_inode = d_inode(new_dentry);
9720 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009721 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009722 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009723 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009724 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009725 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009726
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009727 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009728 return -EPERM;
9729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009730 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009731 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009732 return -EXDEV;
9733
Li Zefan33345d012011-04-20 10:31:50 +08009734 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009735 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 return -ENOTEMPTY;
9737
Chris Mason39279cc2007-06-12 06:35:45 -04009738 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009739 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009740 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009741
9742
9743 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009744 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009745 new_dentry->d_name.name,
9746 new_dentry->d_name.len);
9747
9748 if (ret) {
9749 if (ret == -EEXIST) {
9750 /* we shouldn't get
9751 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309752 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009753 return ret;
9754 }
9755 } else {
9756 /* maybe -EOVERFLOW */
9757 return ret;
9758 }
9759 }
9760 ret = 0;
9761
Chris Mason5a3f23d2009-03-31 13:27:11 -04009762 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009763 * we're using rename to replace one file with another. Start IO on it
9764 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009765 */
Chris Mason8d875f92014-08-12 10:47:42 -07009766 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009767 filemap_flush(old_inode->i_mapping);
9768
Yan, Zheng76dda932009-09-21 16:00:26 -04009769 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009770 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009771 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009772 /*
9773 * We want to reserve the absolute worst case amount of items. So if
9774 * both inodes are subvols and we need to unlink them then that would
9775 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009777 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9778 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009779 * If our rename has the whiteout flag, we need more 5 units for the
9780 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9781 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009782 */
Filipe Manana5062af32016-05-05 10:26:26 +01009783 trans_num_items = 11;
9784 if (flags & RENAME_WHITEOUT)
9785 trans_num_items += 5;
9786 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009787 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009788 ret = PTR_ERR(trans);
9789 goto out_notrans;
9790 }
Chris Mason5f39d392007-10-15 16:14:19 -04009791
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009792 if (dest != root)
9793 btrfs_record_root_in_trans(trans, dest);
9794
Nikolay Borisov877574e2017-02-20 13:50:33 +02009795 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009796 if (ret)
9797 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009798
Miao Xie67de1172013-12-26 13:07:06 +08009799 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009800 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009802 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009803 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009804 btrfs_pin_log_trans(root);
9805 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009806 ret = btrfs_insert_inode_ref(trans, dest,
9807 new_dentry->d_name.name,
9808 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009809 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009810 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009811 if (ret)
9812 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009813 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009814
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009815 inode_inc_iversion(old_dir);
9816 inode_inc_iversion(new_dir);
9817 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009818 old_dir->i_ctime = old_dir->i_mtime =
9819 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009820 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009821
Chris Mason12fcfd22009-03-24 10:24:20 -04009822 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9824 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009825
Li Zefan33345d012011-04-20 10:31:50 +08009826 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9828 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9829 old_dentry->d_name.name,
9830 old_dentry->d_name.len);
9831 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009832 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9833 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009834 old_dentry->d_name.name,
9835 old_dentry->d_name.len);
9836 if (!ret)
9837 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009839 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009840 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009841 goto out_fail;
9842 }
Chris Mason39279cc2007-06-12 06:35:45 -04009843
9844 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009845 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009846 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009847 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009848 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9849 root_objectid = BTRFS_I(new_inode)->location.objectid;
9850 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9851 root_objectid,
9852 new_dentry->d_name.name,
9853 new_dentry->d_name.len);
9854 BUG_ON(new_inode->i_nlink == 0);
9855 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009856 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9857 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009858 new_dentry->d_name.name,
9859 new_dentry->d_name.len);
9860 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009861 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009862 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009863 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009865 goto out_fail;
9866 }
Chris Mason39279cc2007-06-12 06:35:45 -04009867 }
Josef Bacikaec74772008-07-24 12:12:38 -04009868
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 ret = btrfs_add_link(trans, new_dir, old_inode,
9870 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009871 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009872 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009873 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009874 goto out_fail;
9875 }
Chris Mason39279cc2007-06-12 06:35:45 -04009876
Miao Xie67de1172013-12-26 13:07:06 +08009877 if (old_inode->i_nlink == 1)
9878 BTRFS_I(old_inode)->dir_index = index;
9879
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009880 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009881 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009882
David Sterbaf85b7372017-01-20 14:54:07 +01009883 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9884 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009886 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888
9889 if (flags & RENAME_WHITEOUT) {
9890 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9891 old_dentry);
9892
9893 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009894 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009895 goto out_fail;
9896 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009897 }
Chris Mason39279cc2007-06-12 06:35:45 -04009898out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009899 /*
9900 * If we have pinned the log and an error happened, we unpin tasks
9901 * trying to sync the log and force them to fallback to a transaction
9902 * commit if the log currently contains any of the inodes involved in
9903 * this rename operation (to ensure we do not persist a log with an
9904 * inconsistent state for any of these inodes or leading to any
9905 * inconsistencies when replayed). If the transaction was aborted, the
9906 * abortion reason is propagated to userspace when attempting to commit
9907 * the transaction. If the log does not contain any of these inodes, we
9908 * allow the tasks to sync it.
9909 */
9910 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009911 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9912 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9913 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009914 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009915 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009916 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009917
9918 btrfs_end_log_trans(root);
9919 log_pinned = false;
9920 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009921 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009922out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009923 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009924 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009925
Chris Mason39279cc2007-06-12 06:35:45 -04009926 return ret;
9927}
9928
Miklos Szeredi80ace852014-07-23 15:15:32 +02009929static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9930 struct inode *new_dir, struct dentry *new_dentry,
9931 unsigned int flags)
9932{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009933 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009934 return -EINVAL;
9935
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009936 if (flags & RENAME_EXCHANGE)
9937 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9938 new_dentry);
9939
9940 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009941}
9942
Miao Xie8ccf6f192012-10-25 09:28:04 +00009943static void btrfs_run_delalloc_work(struct btrfs_work *work)
9944{
9945 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009946 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009947
9948 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9949 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009950 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009951 filemap_flush(inode->i_mapping);
9952 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9953 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009954 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009955
9956 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009957 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009958 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009959 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009960 complete(&delalloc_work->completion);
9961}
9962
9963struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009964 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009965{
9966 struct btrfs_delalloc_work *work;
9967
David Sterba100d5702015-12-08 14:39:32 +01009968 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009969 if (!work)
9970 return NULL;
9971
9972 init_completion(&work->completion);
9973 INIT_LIST_HEAD(&work->list);
9974 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009975 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009976 WARN_ON_ONCE(!inode);
9977 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9978 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009979
9980 return work;
9981}
9982
9983void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9984{
9985 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009986 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009987}
9988
Chris Masond352ac62008-09-29 15:18:18 -04009989/*
9990 * some fairly slow code that needs optimization. This walks the list
9991 * of all the inodes with pending delalloc and forces them to disk.
9992 */
Miao Xie6c255e62014-03-06 13:55:01 +08009993static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9994 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009995{
Chris Masonea8c2812008-08-04 23:17:27 -04009996 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009997 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009998 struct btrfs_delalloc_work *work, *next;
9999 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010000 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010002
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010004 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010005
Miao Xie573bfb72014-03-06 13:55:03 +080010006 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010007 spin_lock(&root->delalloc_lock);
10008 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010009 while (!list_empty(&splice)) {
10010 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010011 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010012
Miao Xieeb73c1b2013-05-15 07:48:22 +000010013 list_move_tail(&binode->delalloc_inodes,
10014 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010015 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010016 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010017 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010018 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010019 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010021
David Sterba651d4942015-11-27 19:24:16 +010010022 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010023 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010024 if (delay_iput)
10025 btrfs_add_delayed_iput(inode);
10026 else
10027 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010028 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010029 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010030 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010031 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010032 btrfs_queue_work(root->fs_info->flush_workers,
10033 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010034 ret++;
10035 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010036 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010037 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010038 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010039 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010041
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010042out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010043 list_for_each_entry_safe(work, next, &works, list) {
10044 list_del_init(&work->list);
10045 btrfs_wait_and_free_delalloc_work(work);
10046 }
10047
Miao Xieeb73c1b2013-05-15 07:48:22 +000010048 if (!list_empty_careful(&splice)) {
10049 spin_lock(&root->delalloc_lock);
10050 list_splice_tail(&splice, &root->delalloc_inodes);
10051 spin_unlock(&root->delalloc_lock);
10052 }
Miao Xie573bfb72014-03-06 13:55:03 +080010053 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 return ret;
10055}
10056
10057int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10058{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010059 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010060 int ret;
10061
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010062 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010063 return -EROFS;
10064
Miao Xie6c255e62014-03-06 13:55:01 +080010065 ret = __start_delalloc_inodes(root, delay_iput, -1);
10066 if (ret > 0)
10067 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 /*
10069 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010070 * we have to make sure the IO is actually started and that
10071 * ordered extents get created before we return
10072 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010073 atomic_inc(&fs_info->async_submit_draining);
10074 while (atomic_read(&fs_info->nr_async_submits) ||
10075 atomic_read(&fs_info->async_delalloc_pages)) {
10076 wait_event(fs_info->async_submit_wait,
10077 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10078 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010079 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010080 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 return ret;
10082}
10083
Miao Xie6c255e62014-03-06 13:55:01 +080010084int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10085 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010086{
10087 struct btrfs_root *root;
10088 struct list_head splice;
10089 int ret;
10090
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010091 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092 return -EROFS;
10093
10094 INIT_LIST_HEAD(&splice);
10095
Miao Xie573bfb72014-03-06 13:55:03 +080010096 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 spin_lock(&fs_info->delalloc_root_lock);
10098 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010099 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010100 root = list_first_entry(&splice, struct btrfs_root,
10101 delalloc_root);
10102 root = btrfs_grab_fs_root(root);
10103 BUG_ON(!root);
10104 list_move_tail(&root->delalloc_root,
10105 &fs_info->delalloc_roots);
10106 spin_unlock(&fs_info->delalloc_root_lock);
10107
Miao Xie6c255e62014-03-06 13:55:01 +080010108 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010109 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010110 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 goto out;
10112
Miao Xie6c255e62014-03-06 13:55:01 +080010113 if (nr != -1) {
10114 nr -= ret;
10115 WARN_ON(nr < 0);
10116 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 spin_lock(&fs_info->delalloc_root_lock);
10118 }
10119 spin_unlock(&fs_info->delalloc_root_lock);
10120
Miao Xie6c255e62014-03-06 13:55:01 +080010121 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010122 atomic_inc(&fs_info->async_submit_draining);
10123 while (atomic_read(&fs_info->nr_async_submits) ||
10124 atomic_read(&fs_info->async_delalloc_pages)) {
10125 wait_event(fs_info->async_submit_wait,
10126 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10127 atomic_read(&fs_info->async_delalloc_pages) == 0));
10128 }
10129 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010130out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010131 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010132 spin_lock(&fs_info->delalloc_root_lock);
10133 list_splice_tail(&splice, &fs_info->delalloc_roots);
10134 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010135 }
Miao Xie573bfb72014-03-06 13:55:03 +080010136 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010137 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010138}
10139
Chris Mason39279cc2007-06-12 06:35:45 -040010140static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10141 const char *symname)
10142{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010143 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010144 struct btrfs_trans_handle *trans;
10145 struct btrfs_root *root = BTRFS_I(dir)->root;
10146 struct btrfs_path *path;
10147 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010148 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010149 int err;
10150 int drop_inode = 0;
10151 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010152 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010153 int name_len;
10154 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010156 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010157 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010158
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010159 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010161 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010162
Josef Bacik9ed74f22009-09-11 16:12:44 -040010163 /*
10164 * 2 items for inode item and ref
10165 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010166 * 1 item for updating parent inode item
10167 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010168 * 1 item for xattr if selinux is on
10169 */
Filipe Manana9269d122015-12-31 18:16:29 +000010170 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010171 if (IS_ERR(trans))
10172 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010173
Li Zefan581bb052011-04-20 10:06:11 +080010174 err = btrfs_find_free_ino(root, &objectid);
10175 if (err)
10176 goto out_unlock;
10177
Josef Bacikaec74772008-07-24 12:12:38 -040010178 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010179 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10180 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010181 if (IS_ERR(inode)) {
10182 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010183 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010184 }
Chris Mason39279cc2007-06-12 06:35:45 -040010185
Casey Schauflerad19db72011-12-15 10:09:07 -050010186 /*
10187 * If the active LSM wants to access the inode during
10188 * d_instantiate it needs these. Smack checks to see
10189 * if the filesystem supports xattrs by looking at the
10190 * ops vector.
10191 */
10192 inode->i_fop = &btrfs_file_operations;
10193 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010194 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10196
10197 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10198 if (err)
10199 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010200
Chris Mason39279cc2007-06-12 06:35:45 -040010201 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010202 if (!path) {
10203 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010204 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010205 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010206 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010207 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010208 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010209 datasize = btrfs_file_extent_calc_inline_size(name_len);
10210 err = btrfs_insert_empty_item(trans, root, path, &key,
10211 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010212 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010213 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010214 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010215 }
Chris Mason5f39d392007-10-15 16:14:19 -040010216 leaf = path->nodes[0];
10217 ei = btrfs_item_ptr(leaf, path->slots[0],
10218 struct btrfs_file_extent_item);
10219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10220 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010221 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010222 btrfs_set_file_extent_encryption(leaf, ei, 0);
10223 btrfs_set_file_extent_compression(leaf, ei, 0);
10224 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10225 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10226
Chris Mason39279cc2007-06-12 06:35:45 -040010227 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010228 write_extent_buffer(leaf, symname, ptr, name_len);
10229 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010230 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010231
Chris Mason39279cc2007-06-12 06:35:45 -040010232 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010233 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010234 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010235 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010236 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010237 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010238 /*
10239 * Last step, add directory indexes for our symlink inode. This is the
10240 * last step to avoid extra cleanup of these indexes if an error happens
10241 * elsewhere above.
10242 */
10243 if (!err)
10244 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010245 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010246 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010247 goto out_unlock_inode;
10248 }
10249
10250 unlock_new_inode(inode);
10251 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010252
10253out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010254 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010255 if (drop_inode) {
10256 inode_dec_link_count(inode);
10257 iput(inode);
10258 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010259 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010260 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010261
10262out_unlock_inode:
10263 drop_inode = 1;
10264 unlock_new_inode(inode);
10265 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010266}
Chris Mason16432982008-04-10 10:23:21 -040010267
Josef Bacik0af3d002010-06-21 14:48:16 -040010268static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10269 u64 start, u64 num_bytes, u64 min_size,
10270 loff_t actual_len, u64 *alloc_hint,
10271 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010273 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010274 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10275 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010276 struct btrfs_root *root = BTRFS_I(inode)->root;
10277 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010279 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010280 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010281 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010282 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010283 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010284 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010285
Josef Bacik0af3d002010-06-21 14:48:16 -040010286 if (trans)
10287 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010288 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010289 if (own_trans) {
10290 trans = btrfs_start_transaction(root, 3);
10291 if (IS_ERR(trans)) {
10292 ret = PTR_ERR(trans);
10293 break;
10294 }
Yan Zhengd899e052008-10-30 14:25:28 -040010295 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010296
Byongho Leeee221842015-12-15 01:42:10 +090010297 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010298 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010299 /*
10300 * If we are severely fragmented we could end up with really
10301 * small allocations, so if the allocator is returning small
10302 * chunks lets make its job easier by only searching for those
10303 * sized chunks.
10304 */
10305 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010306 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10307 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010308 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010309 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010310 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010311 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010312 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010313 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010314
Josef Bacik0b670dc2015-09-23 17:11:16 -040010315 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010316 ret = insert_reserved_file_extent(trans, inode,
10317 cur_offset, ins.objectid,
10318 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010319 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010320 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010321 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010322 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010323 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010324 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010325 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010326 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 break;
10328 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010329
Chris Masona1ed8352009-09-11 12:27:37 -040010330 btrfs_drop_extent_cache(inode, cur_offset,
10331 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010332
Josef Bacik5dc562c2012-08-17 13:14:17 -040010333 em = alloc_extent_map();
10334 if (!em) {
10335 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10336 &BTRFS_I(inode)->runtime_flags);
10337 goto next;
10338 }
10339
10340 em->start = cur_offset;
10341 em->orig_start = cur_offset;
10342 em->len = ins.offset;
10343 em->block_start = ins.objectid;
10344 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010345 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010346 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010347 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010348 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10349 em->generation = trans->transid;
10350
10351 while (1) {
10352 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010353 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010354 write_unlock(&em_tree->lock);
10355 if (ret != -EEXIST)
10356 break;
10357 btrfs_drop_extent_cache(inode, cur_offset,
10358 cur_offset + ins.offset - 1,
10359 0);
10360 }
10361 free_extent_map(em);
10362next:
Yan Zhengd899e052008-10-30 14:25:28 -040010363 num_bytes -= ins.offset;
10364 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010365 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010366
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010367 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010368 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010369 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010370 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010371 (actual_len > inode->i_size) &&
10372 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010373 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010374 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010375 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010376 i_size = cur_offset;
10377 i_size_write(inode, i_size);
10378 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010379 }
10380
Yan Zhengd899e052008-10-30 14:25:28 -040010381 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010382
10383 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010384 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010385 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010386 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010387 break;
10388 }
Yan Zhengd899e052008-10-30 14:25:28 -040010389
Josef Bacik0af3d002010-06-21 14:48:16 -040010390 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010391 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010392 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010393 if (cur_offset < end)
10394 btrfs_free_reserved_data_space(inode, cur_offset,
10395 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010396 return ret;
10397}
10398
Josef Bacik0af3d002010-06-21 14:48:16 -040010399int btrfs_prealloc_file_range(struct inode *inode, int mode,
10400 u64 start, u64 num_bytes, u64 min_size,
10401 loff_t actual_len, u64 *alloc_hint)
10402{
10403 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10404 min_size, actual_len, alloc_hint,
10405 NULL);
10406}
10407
10408int btrfs_prealloc_file_range_trans(struct inode *inode,
10409 struct btrfs_trans_handle *trans, int mode,
10410 u64 start, u64 num_bytes, u64 min_size,
10411 loff_t actual_len, u64 *alloc_hint)
10412{
10413 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10414 min_size, actual_len, alloc_hint, trans);
10415}
10416
Chris Masone6dcd2d2008-07-17 12:53:50 -040010417static int btrfs_set_page_dirty(struct page *page)
10418{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010419 return __set_page_dirty_nobuffers(page);
10420}
10421
Al Viro10556cb2011-06-20 19:28:19 -040010422static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010423{
Li Zefanb83cc962010-12-20 16:04:08 +080010424 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010425 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010426
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010427 if (mask & MAY_WRITE &&
10428 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10429 if (btrfs_root_readonly(root))
10430 return -EROFS;
10431 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10432 return -EACCES;
10433 }
Al Viro2830ba72011-06-20 19:16:29 -040010434 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010435}
Chris Mason39279cc2007-06-12 06:35:45 -040010436
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010437static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10438{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010439 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010440 struct btrfs_trans_handle *trans;
10441 struct btrfs_root *root = BTRFS_I(dir)->root;
10442 struct inode *inode = NULL;
10443 u64 objectid;
10444 u64 index;
10445 int ret = 0;
10446
10447 /*
10448 * 5 units required for adding orphan entry
10449 */
10450 trans = btrfs_start_transaction(root, 5);
10451 if (IS_ERR(trans))
10452 return PTR_ERR(trans);
10453
10454 ret = btrfs_find_free_ino(root, &objectid);
10455 if (ret)
10456 goto out;
10457
10458 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010459 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010460 if (IS_ERR(inode)) {
10461 ret = PTR_ERR(inode);
10462 inode = NULL;
10463 goto out;
10464 }
10465
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 inode->i_fop = &btrfs_file_operations;
10467 inode->i_op = &btrfs_file_inode_operations;
10468
10469 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010470 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10471
Chris Masonb0d5d102014-09-08 13:08:51 -070010472 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10473 if (ret)
10474 goto out_inode;
10475
10476 ret = btrfs_update_inode(trans, root, inode);
10477 if (ret)
10478 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010479 ret = btrfs_orphan_add(trans, inode);
10480 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010481 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482
Filipe Manana5762b5c2014-08-01 00:10:32 +010010483 /*
10484 * We set number of links to 0 in btrfs_new_inode(), and here we set
10485 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10486 * through:
10487 *
10488 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10489 */
10490 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010491 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010492 d_tmpfile(dentry, inode);
10493 mark_inode_dirty(inode);
10494
10495out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010496 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010497 if (ret)
10498 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010499 btrfs_balance_delayed_items(fs_info);
10500 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010501 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010502
10503out_inode:
10504 unlock_new_inode(inode);
10505 goto out;
10506
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010507}
10508
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010509static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010510 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010511 .lookup = btrfs_lookup,
10512 .create = btrfs_create,
10513 .unlink = btrfs_unlink,
10514 .link = btrfs_link,
10515 .mkdir = btrfs_mkdir,
10516 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010517 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010518 .symlink = btrfs_symlink,
10519 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010520 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010521 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010522 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010523 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010524 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010525 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010526 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010527};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010528static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010529 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010530 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010531 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010532};
Yan, Zheng76dda932009-09-21 16:00:26 -040010533
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010534static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010535 .llseek = generic_file_llseek,
10536 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010537 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010538 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010539#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010540 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010541#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010542 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010543 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010544};
10545
David Sterba20e55062015-11-19 11:42:28 +010010546static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010547 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010548 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010549 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010550 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010551 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010552 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010553 .set_bit_hook = btrfs_set_bit_hook,
10554 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010555 .merge_extent_hook = btrfs_merge_extent_hook,
10556 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010557};
10558
Chris Mason35054392009-01-21 13:11:13 -050010559/*
10560 * btrfs doesn't support the bmap operation because swapfiles
10561 * use bmap to make a mapping of extents in the file. They assume
10562 * these extents won't change over the life of the file and they
10563 * use the bmap result to do IO directly to the drive.
10564 *
10565 * the btrfs bmap call would return logical addresses that aren't
10566 * suitable for IO and they also will change frequently as COW
10567 * operations happen. So, swapfile + btrfs == corruption.
10568 *
10569 * For now we're avoiding this by dropping bmap.
10570 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010571static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010572 .readpage = btrfs_readpage,
10573 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010574 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010575 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010576 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010577 .invalidatepage = btrfs_invalidatepage,
10578 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010579 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010580 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010581};
10582
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010583static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010584 .readpage = btrfs_readpage,
10585 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010586 .invalidatepage = btrfs_invalidatepage,
10587 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010588};
10589
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010590static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010591 .getattr = btrfs_getattr,
10592 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010593 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010594 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010595 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010596 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010597 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010598 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010599};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010600static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010601 .getattr = btrfs_getattr,
10602 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010603 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010604 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010605 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010606 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010607 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010608};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010609static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010610 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010611 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010612 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010613 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010614 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010615 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010616};
Yan, Zheng76dda932009-09-21 16:00:26 -040010617
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010618const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010619 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010620 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010621};