blob: 8a14b47b4dc07eea7b50853507bbc1b61f4efd61 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400319 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400320 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400321out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800322 /*
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
327 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300328 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000329 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400330 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400331 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400332}
333
Chris Mason771ed682008-11-06 22:02:51 -0500334struct async_extent {
335 u64 start;
336 u64 ram_size;
337 u64 compressed_size;
338 struct page **pages;
339 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800340 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500341 struct list_head list;
342};
343
344struct async_cow {
345 struct inode *inode;
346 struct btrfs_root *root;
347 struct page *locked_page;
348 u64 start;
349 u64 end;
350 struct list_head extents;
351 struct btrfs_work work;
352};
353
354static noinline int add_async_extent(struct async_cow *cow,
355 u64 start, u64 ram_size,
356 u64 compressed_size,
357 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800358 unsigned long nr_pages,
359 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500360{
361 struct async_extent *async_extent;
362
363 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100364 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500365 async_extent->start = start;
366 async_extent->ram_size = ram_size;
367 async_extent->compressed_size = compressed_size;
368 async_extent->pages = pages;
369 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 list_add_tail(&async_extent->list, &cow->extents);
372 return 0;
373}
374
Wang Shilongf79707b2014-07-17 11:44:09 +0800375static inline int inode_need_compress(struct inode *inode)
376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800378
379 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400380 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800381 return 1;
382 /* bad compression ratios */
383 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
384 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400385 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800386 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 BTRFS_I(inode)->force_compress)
388 return 1;
389 return 0;
390}
391
Anand Jain26d30f82016-12-19 19:09:06 +0800392static inline void inode_should_defrag(struct inode *inode,
393 u64 start, u64 end, u64 num_bytes, u64 small_write)
394{
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes < small_write &&
397 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 btrfs_add_inode_defrag(NULL, inode);
399}
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401/*
Chris Mason771ed682008-11-06 22:02:51 -0500402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400405 *
Chris Mason771ed682008-11-06 22:02:51 -0500406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 *
Chris Mason771ed682008-11-06 22:02:51 -0500412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300415 * are written in the same order that the flusher thread sent them
416 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400417 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100418static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page,
420 u64 start, u64 end,
421 struct async_cow *async_cow,
422 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400426 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400431 struct page **pages = NULL;
432 unsigned long nr_pages;
433 unsigned long nr_pages_ret = 0;
434 unsigned long total_compressed = 0;
435 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900436 unsigned long max_compressed = SZ_128K;
437 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400438 int i;
439 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400440 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400441 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400442
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 Masonb888db22007-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 Masonb888db22007-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)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001740 btrfs_delalloc_release_metadata(inode, len);
1741
Josef Bacik6a3891c2015-03-16 17:38:52 -04001742 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001744 return;
1745
Josef Bacik0cb59c92010-07-02 12:14:14 -04001746 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001747 && do_list && !(state->state & EXTENT_NORESERVE)
1748 && (*bits & (EXTENT_DO_ACCOUNTING |
1749 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001750 btrfs_free_reserved_data_space_noquota(inode,
1751 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1754 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001755 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001757 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001758 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 &BTRFS_I(inode)->runtime_flags))
1760 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001762 }
Chris Mason291d6732008-01-29 15:55:23 -05001763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1767 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001768 *
1769 * return 1 if page cannot be merged to bio
1770 * return 0 if page can be merged to bio
1771 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001772 */
Mike Christie81a75f672016-06-05 14:31:54 -05001773int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001774 size_t size, struct bio *bio,
1775 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 struct inode *inode = page->mapping->host;
1778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001780 u64 length = 0;
1781 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001782 int ret;
1783
Chris Mason771ed682008-11-06 22:02:51 -05001784 if (bio_flags & EXTENT_BIO_COMPRESSED)
1785 return 0;
1786
Kent Overstreet4f024f32013-10-11 15:44:27 -07001787 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001788 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1790 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001791 if (ret < 0)
1792 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001793 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001794 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001795 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001796}
1797
Chris Masond352ac62008-09-29 15:18:18 -04001798/*
1799 * in order to insert checksums into the metadata in large chunks,
1800 * we wait until bio submission time. All the pages in the bio are
1801 * checksummed and sums are attached onto the ordered extent record.
1802 *
1803 * At IO completion time the cums attached on the ordered extent record
1804 * are inserted into the btree
1805 */
Mike Christie81a75f672016-06-05 14:31:54 -05001806static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1807 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001808 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001809{
Chris Mason065631f2008-02-20 12:07:25 -05001810 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001812 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001814 return 0;
1815}
Chris Masone0156402008-04-16 11:15:20 -04001816
Chris Mason4a69a412008-11-06 22:03:00 -05001817/*
1818 * in order to insert checksums into the metadata in large chunks,
1819 * we wait until bio submission time. All the pages in the bio are
1820 * checksummed and sums are attached onto the ordered extent record.
1821 *
1822 * At IO completion time the cums attached on the ordered extent record
1823 * are inserted into the btree
1824 */
Mike Christie81a75f672016-06-05 14:31:54 -05001825static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001826 int mirror_num, unsigned long bio_flags,
1827 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001828{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001830 int ret;
1831
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001832 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001833 if (ret) {
1834 bio->bi_error = ret;
1835 bio_endio(bio);
1836 }
Stefan Behrens61891922012-11-05 18:51:52 +01001837 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001838}
1839
Chris Masond352ac62008-09-29 15:18:18 -04001840/*
Chris Masoncad321a2008-12-17 14:51:42 -05001841 * extent_io.c submission hook. This does the right thing for csum calculation
1842 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001843 */
Mike Christie81a75f672016-06-05 14:31:54 -05001844static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001845 int mirror_num, unsigned long bio_flags,
1846 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001847{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001849 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301850 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001851 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001852 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001853 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001854
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001855 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001856
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)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002918 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002919 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (ret || truncated) {
2922 u64 start, end;
2923
2924 if (truncated)
2925 start = ordered_extent->file_offset + logical_len;
2926 else
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 /*
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002943 btrfs_free_reserved_extent(fs_info,
2944 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002945 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002946 }
2947
2948
Josef Bacik5fd02042012-05-02 14:00:54 -04002949 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002950 * This needs to be done to make sure anybody waiting knows we are done
2951 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002952 */
2953 btrfs_remove_ordered_extent(inode, ordered_extent);
2954
Liu Bo38c227d2013-01-29 03:18:40 +00002955 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002956 if (new) {
2957 if (ret) {
2958 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002959 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002960 } else {
2961 relink_file_extents(new);
2962 }
2963 }
Liu Bo38c227d2013-01-29 03:18:40 +00002964
Chris Masone6dcd2d2008-07-17 12:53:50 -04002965 /* once for us */
2966 btrfs_put_ordered_extent(ordered_extent);
2967 /* once for the tree */
2968 btrfs_put_ordered_extent(ordered_extent);
2969
Josef Bacik5fd02042012-05-02 14:00:54 -04002970 return ret;
2971}
2972
2973static void finish_ordered_fn(struct btrfs_work *work)
2974{
2975 struct btrfs_ordered_extent *ordered_extent;
2976 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2977 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978}
2979
Christoph Hellwigb2950862008-12-02 09:54:17 -05002980static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002981 struct extent_state *state, int uptodate)
2982{
Josef Bacik5fd02042012-05-02 14:00:54 -04002983 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002984 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002985 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002986 struct btrfs_workqueue *wq;
2987 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002988
liubo1abe9b82011-03-24 11:18:59 +00002989 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2990
Chris Mason8b62b722009-09-02 16:53:46 -04002991 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002992 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2993 end - start + 1, uptodate))
2994 return 0;
2995
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002996 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002998 func = btrfs_freespace_write_helper;
2999 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003000 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003001 func = btrfs_endio_write_helper;
3002 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003003
Liu Bo9e0af232014-08-15 23:36:53 +08003004 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3005 NULL);
3006 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003007
3008 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003009}
3010
Miao Xiedc380ae2014-09-12 18:43:55 +08003011static int __readpage_endio_check(struct inode *inode,
3012 struct btrfs_io_bio *io_bio,
3013 int icsum, struct page *page,
3014 int pgoff, u64 start, size_t len)
3015{
3016 char *kaddr;
3017 u32 csum_expected;
3018 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003019
3020 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3021
3022 kaddr = kmap_atomic(page);
3023 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003024 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003025 if (csum != csum_expected)
3026 goto zeroit;
3027
3028 kunmap_atomic(kaddr);
3029 return 0;
3030zeroit:
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003031 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3032 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003033 memset(kaddr + pgoff, 1, len);
3034 flush_dcache_page(page);
3035 kunmap_atomic(kaddr);
3036 if (csum_expected == 0)
3037 return 0;
3038 return -EIO;
3039}
3040
Chris Masond352ac62008-09-29 15:18:18 -04003041/*
Chris Masond352ac62008-09-29 15:18:18 -04003042 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003043 * if there's a match, we allow the bio to finish. If not, the code in
3044 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003045 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003046static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3047 u64 phy_offset, struct page *page,
3048 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003049{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003050 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003051 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003052 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003053 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003054
Chris Masond20f7042008-12-08 16:58:54 -05003055 if (PageChecked(page)) {
3056 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003057 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003058 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003059
3060 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003061 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003062
Yan Zheng17d217f2008-12-12 10:03:38 -05003063 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003064 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003065 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003066 return 0;
3067 }
3068
Miao Xiefacc8a222013-07-25 19:22:34 +08003069 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003070 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3071 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003072}
Chris Masonb888db22007-08-27 16:49:44 -04003073
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003074void btrfs_add_delayed_iput(struct inode *inode)
3075{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003077 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003078
3079 if (atomic_add_unless(&inode->i_count, -1, 1))
3080 return;
3081
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003082 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003083 if (binode->delayed_iput_count == 0) {
3084 ASSERT(list_empty(&binode->delayed_iput));
3085 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3086 } else {
3087 binode->delayed_iput_count++;
3088 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003089 spin_unlock(&fs_info->delayed_iput_lock);
3090}
3091
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003092void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003093{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003096 while (!list_empty(&fs_info->delayed_iputs)) {
3097 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003098
David Sterba8089fe62015-11-19 14:15:51 +01003099 inode = list_first_entry(&fs_info->delayed_iputs,
3100 struct btrfs_inode, delayed_iput);
3101 if (inode->delayed_iput_count) {
3102 inode->delayed_iput_count--;
3103 list_move_tail(&inode->delayed_iput,
3104 &fs_info->delayed_iputs);
3105 } else {
3106 list_del_init(&inode->delayed_iput);
3107 }
3108 spin_unlock(&fs_info->delayed_iput_lock);
3109 iput(&inode->vfs_inode);
3110 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003111 }
David Sterba8089fe62015-11-19 14:15:51 +01003112 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003113}
3114
Yan, Zhengd68fc572010-05-16 10:49:58 -04003115/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003116 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003117 * files in the subvolume, it removes orphan item and frees block_rsv
3118 * structure.
3119 */
3120void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root)
3122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003123 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003124 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003125 int ret;
3126
Josef Bacik8a35d952012-05-23 14:26:42 -04003127 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003128 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3129 return;
3130
Josef Bacik90290e12011-12-02 15:44:12 -05003131 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003132 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003133 spin_unlock(&root->orphan_lock);
3134 return;
3135 }
3136
3137 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3138 spin_unlock(&root->orphan_lock);
3139 return;
3140 }
3141
3142 block_rsv = root->orphan_block_rsv;
3143 root->orphan_block_rsv = NULL;
3144 spin_unlock(&root->orphan_lock);
3145
Miao Xie27cdeb72014-04-02 19:51:05 +08003146 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003147 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003150 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003151 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003152 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003153 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3154 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003155 }
3156
Josef Bacik90290e12011-12-02 15:44:12 -05003157 if (block_rsv) {
3158 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003159 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 }
3161}
3162
3163/*
Josef Bacik7b128762008-07-24 12:17:14 -04003164 * This creates an orphan entry for the given inode in case something goes
3165 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003166 *
3167 * NOTE: caller of this function should reserve 5 units of metadata for
3168 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003169 */
3170int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003173 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 struct btrfs_block_rsv *block_rsv = NULL;
3175 int reserve = 0;
3176 int insert = 0;
3177 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003178
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003180 block_rsv = btrfs_alloc_block_rsv(fs_info,
3181 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003182 if (!block_rsv)
3183 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003184 }
3185
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 spin_lock(&root->orphan_lock);
3187 if (!root->orphan_block_rsv) {
3188 root->orphan_block_rsv = block_rsv;
3189 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003190 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 block_rsv = NULL;
3192 }
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik8a35d952012-05-23 14:26:42 -04003194 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3195 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196#if 0
3197 /*
3198 * For proper ENOSPC handling, we should do orphan
3199 * cleanup when mounting. But this introduces backward
3200 * compatibility issue.
3201 */
3202 if (!xchg(&root->orphan_item_inserted, 1))
3203 insert = 2;
3204 else
3205 insert = 1;
3206#endif
3207 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003208 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 }
Josef Bacik7b128762008-07-24 12:17:14 -04003210
Josef Bacik72ac3c02012-05-23 14:13:11 -04003211 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3212 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003215
Yan, Zhengd68fc572010-05-16 10:49:58 -04003216 /* grab metadata reservation from transaction handle */
3217 if (reserve) {
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02003218 ret = btrfs_orphan_reserve_metadata(trans, BTRFS_I(inode));
Josef Bacik3b6571c2016-05-27 13:03:04 -04003219 ASSERT(!ret);
3220 if (ret) {
3221 atomic_dec(&root->orphan_inodes);
3222 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3223 &BTRFS_I(inode)->runtime_flags);
3224 if (insert)
3225 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
3227 return ret;
3228 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 }
3230
3231 /* insert an orphan item to track this unlinked/truncated file */
3232 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003233 ret = btrfs_insert_orphan_item(trans, root,
3234 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003236 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 if (reserve) {
3238 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3239 &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov703b3912017-02-20 13:50:40 +02003240 btrfs_orphan_release_metadata(BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003241 }
3242 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003243 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3244 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003245 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 return ret;
3247 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003248 }
3249 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 }
3251
3252 /* insert an orphan item to track subvolume contains orphan files */
3253 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003254 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003256 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003257 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003258 return ret;
3259 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 }
3261 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003262}
3263
3264/*
3265 * We have done the truncate/delete so we can go ahead and remove the orphan
3266 * item for this particular inode.
3267 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003268static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3269 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003270{
3271 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 int delete_item = 0;
3273 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003274 int ret = 0;
3275
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003277 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3278 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003280
Josef Bacik72ac3c02012-05-23 14:13:11 -04003281 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 spin_unlock(&root->orphan_lock);
3285
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003286 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003287 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288 if (trans)
3289 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003290 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003291 }
Josef Bacik7b128762008-07-24 12:17:14 -04003292
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003293 if (release_rsv)
Nikolay Borisov703b3912017-02-20 13:50:40 +02003294 btrfs_orphan_release_metadata(BTRFS_I(inode));
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295
Josef Bacik4ef31a42013-08-13 14:10:08 -04003296 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003297}
3298
3299/*
3300 * this cleans up any orphans that may be left on the list from the last use
3301 * of this root.
3302 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003303int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003304{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003305 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003306 struct btrfs_path *path;
3307 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003308 struct btrfs_key key, found_key;
3309 struct btrfs_trans_handle *trans;
3310 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003311 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3313
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003315 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003316
3317 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318 if (!path) {
3319 ret = -ENOMEM;
3320 goto out;
3321 }
David Sterbae4058b52015-11-27 16:31:35 +01003322 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003323
3324 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003325 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003326 key.offset = (u64)-1;
3327
Josef Bacik7b128762008-07-24 12:17:14 -04003328 while (1) {
3329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330 if (ret < 0)
3331 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003332
3333 /*
3334 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003335 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003336 * find the key and see if we have stuff that matches
3337 */
3338 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003339 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003340 if (path->slots[0] == 0)
3341 break;
3342 path->slots[0]--;
3343 }
3344
3345 /* pull out the item */
3346 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3348
3349 /* make sure the item matches what we want */
3350 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3351 break;
David Sterba962a2982014-06-04 18:41:45 +02003352 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003353 break;
3354
3355 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003356 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003357
3358 /*
3359 * this is where we are basically btrfs_lookup, without the
3360 * crossing root thing. we store the inode number in the
3361 * offset of the orphan item.
3362 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003363
3364 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003365 btrfs_err(fs_info,
3366 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003367 ret = -EINVAL;
3368 goto out;
3369 }
3370
3371 last_objectid = found_key.offset;
3372
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373 found_key.objectid = found_key.offset;
3374 found_key.type = BTRFS_INODE_ITEM_KEY;
3375 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003376 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303377 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003378 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003379 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003382 struct btrfs_root *dead_root;
3383 struct btrfs_fs_info *fs_info = root->fs_info;
3384 int is_dead_root = 0;
3385
3386 /*
3387 * this is an orphan in the tree root. Currently these
3388 * could come from 2 sources:
3389 * a) a snapshot deletion in progress
3390 * b) a free space cache inode
3391 * We need to distinguish those two, as the snapshot
3392 * orphan must not get deleted.
3393 * find_dead_roots already ran before us, so if this
3394 * is a snapshot deletion, we should find the root
3395 * in the dead_roots list
3396 */
3397 spin_lock(&fs_info->trans_lock);
3398 list_for_each_entry(dead_root, &fs_info->dead_roots,
3399 root_list) {
3400 if (dead_root->root_key.objectid ==
3401 found_key.objectid) {
3402 is_dead_root = 1;
3403 break;
3404 }
3405 }
3406 spin_unlock(&fs_info->trans_lock);
3407 if (is_dead_root) {
3408 /* prevent this orphan from being found again */
3409 key.offset = found_key.objectid - 1;
3410 continue;
3411 }
3412 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003413 /*
3414 * Inode is already gone but the orphan item is still there,
3415 * kill the orphan item.
3416 */
Filipe Manana67710892016-06-06 11:51:25 +01003417 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003418 trans = btrfs_start_transaction(root, 1);
3419 if (IS_ERR(trans)) {
3420 ret = PTR_ERR(trans);
3421 goto out;
3422 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 btrfs_debug(fs_info, "auto deleting %Lu",
3424 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003425 ret = btrfs_del_orphan_item(trans, root,
3426 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003427 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 if (ret)
3429 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003430 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003431 }
Josef Bacik7b128762008-07-24 12:17:14 -04003432
Josef Bacik7b128762008-07-24 12:17:14 -04003433 /*
3434 * add this inode to the orphan list so btrfs_orphan_del does
3435 * the proper thing when we hit it
3436 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003437 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3438 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003439 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003440
Josef Bacik7b128762008-07-24 12:17:14 -04003441 /* if we have links, this was a truncate, lets do that */
3442 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303443 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003444 iput(inode);
3445 continue;
3446 }
Josef Bacik7b128762008-07-24 12:17:14 -04003447 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003448
3449 /* 1 for the orphan item deletion. */
3450 trans = btrfs_start_transaction(root, 1);
3451 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003452 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003453 ret = PTR_ERR(trans);
3454 goto out;
3455 }
3456 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003457 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 if (ret) {
3459 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003460 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003461 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003462
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003464 if (ret)
3465 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003466 } else {
3467 nr_unlink++;
3468 }
3469
3470 /* this will do delete_inode and everything for us */
3471 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (ret)
3473 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003474 }
Miao Xie3254c872011-11-10 20:45:05 -05003475 /* release the path since we're done with it */
3476 btrfs_release_path(path);
3477
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3479
3480 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003481 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003482 (u64)-1);
3483
Miao Xie27cdeb72014-04-02 19:51:05 +08003484 if (root->orphan_block_rsv ||
3485 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003486 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003488 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003489 }
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003492 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003493 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495
3496out:
3497 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 btrfs_free_path(path);
3500 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003504 * very simple check to peek ahead in the leaf looking for xattrs. If we
3505 * don't find any xattrs, we know there can't be any acls.
3506 *
3507 * slot is the slot the inode is in, objectid is the objectid of the inode
3508 */
3509static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003510 int slot, u64 objectid,
3511 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003512{
3513 u32 nritems = btrfs_header_nritems(leaf);
3514 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003515 static u64 xattr_access = 0;
3516 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003517 int scanned = 0;
3518
Josef Bacikf23b5a52013-06-19 10:16:26 -04003519 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003520 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3521 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3522 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3523 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 }
3525
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003527 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 while (slot < nritems) {
3529 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3530
3531 /* we found a different objectid, there must not be acls */
3532 if (found_key.objectid != objectid)
3533 return 0;
3534
3535 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003536 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003537 if (*first_xattr_slot == -1)
3538 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003539 if (found_key.offset == xattr_access ||
3540 found_key.offset == xattr_default)
3541 return 1;
3542 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003543
3544 /*
3545 * we found a key greater than an xattr key, there can't
3546 * be any acls later on
3547 */
3548 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3549 return 0;
3550
3551 slot++;
3552 scanned++;
3553
3554 /*
3555 * it goes inode, inode backrefs, xattrs, extents,
3556 * so if there are a ton of hard links to an inode there can
3557 * be a lot of backrefs. Don't waste time searching too hard,
3558 * this is just an optimization
3559 */
3560 if (scanned >= 8)
3561 break;
3562 }
3563 /* we hit the end of the leaf before we found an xattr or
3564 * something larger than an xattr. We have to assume the inode
3565 * has acls
3566 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003567 if (*first_xattr_slot == -1)
3568 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003569 return 1;
3570}
3571
3572/*
Chris Masond352ac62008-09-29 15:18:18 -04003573 * read an inode from the btree into the in-memory inode
3574 */
Filipe Manana67710892016-06-06 11:51:25 +01003575static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003576{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003577 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003578 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003579 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003580 struct btrfs_inode_item *inode_item;
3581 struct btrfs_root *root = BTRFS_I(inode)->root;
3582 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003583 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003585 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003587 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003588 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003589
3590 ret = btrfs_fill_inode(inode, &rdev);
3591 if (!ret)
3592 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003593
3594 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003595 if (!path) {
3596 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003597 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003598 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003599
Chris Mason39279cc2007-06-12 06:35:45 -04003600 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003601
Chris Mason39279cc2007-06-12 06:35:45 -04003602 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003603 if (ret) {
3604 if (ret > 0)
3605 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003606 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003607 }
Chris Mason39279cc2007-06-12 06:35:45 -04003608
Chris Mason5f39d392007-10-15 16:14:19 -04003609 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003610
3611 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003612 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003613
Chris Mason5f39d392007-10-15 16:14:19 -04003614 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3615 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003616 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003617 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003618 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3619 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003620 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003621
David Sterbaa937b972014-12-12 17:39:12 +01003622 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3623 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003624
David Sterbaa937b972014-12-12 17:39:12 +01003625 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3626 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
David Sterbaa937b972014-12-12 17:39:12 +01003628 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3629 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
chandan r9cc97d62012-07-04 12:48:07 +05303631 BTRFS_I(inode)->i_otime.tv_sec =
3632 btrfs_timespec_sec(leaf, &inode_item->otime);
3633 BTRFS_I(inode)->i_otime.tv_nsec =
3634 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003636 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003637 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003638 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3639
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003640 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003641 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003642 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003643 rdev = btrfs_inode_rdev(leaf, inode_item);
3644
Josef Bacikaec74772008-07-24 12:12:38 -04003645 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003646 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003647
3648cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003649 /*
3650 * If we were modified in the current generation and evicted from memory
3651 * and then re-read we need to do a full sync since we don't have any
3652 * idea about which extents were modified before we were evicted from
3653 * cache.
3654 *
3655 * This is required for both inode re-read from disk and delayed inode
3656 * in delayed_nodes_tree.
3657 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003658 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003659 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3660 &BTRFS_I(inode)->runtime_flags);
3661
Filipe Mananabde6c242015-07-24 00:00:19 +01003662 /*
3663 * We don't persist the id of the transaction where an unlink operation
3664 * against the inode was last made. So here we assume the inode might
3665 * have been evicted, and therefore the exact value of last_unlink_trans
3666 * lost, and set it to last_trans to avoid metadata inconsistencies
3667 * between the inode and its parent if the inode is fsync'ed and the log
3668 * replayed. For example, in the scenario:
3669 *
3670 * touch mydir/foo
3671 * ln mydir/foo mydir/bar
3672 * sync
3673 * unlink mydir/bar
3674 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3675 * xfs_io -c fsync mydir/foo
3676 * <power failure>
3677 * mount fs, triggers fsync log replay
3678 *
3679 * We must make sure that when we fsync our inode foo we also log its
3680 * parent inode, otherwise after log replay the parent still has the
3681 * dentry with the "bar" name but our inode foo has a link count of 1
3682 * and doesn't have an inode ref with the name "bar" anymore.
3683 *
3684 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003685 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003686 * transaction commits on fsync if our inode is a directory, or if our
3687 * inode is not a directory, logging its parent unnecessarily.
3688 */
3689 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3690
Miao Xie67de1172013-12-26 13:07:06 +08003691 path->slots[0]++;
3692 if (inode->i_nlink != 1 ||
3693 path->slots[0] >= btrfs_header_nritems(leaf))
3694 goto cache_acl;
3695
3696 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003697 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003698 goto cache_acl;
3699
3700 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3701 if (location.type == BTRFS_INODE_REF_KEY) {
3702 struct btrfs_inode_ref *ref;
3703
3704 ref = (struct btrfs_inode_ref *)ptr;
3705 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3706 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3707 struct btrfs_inode_extref *extref;
3708
3709 extref = (struct btrfs_inode_extref *)ptr;
3710 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3711 extref);
3712 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003713cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003714 /*
3715 * try to precache a NULL acl entry for files that don't have
3716 * any xattrs or acls
3717 */
Li Zefan33345d012011-04-20 10:31:50 +08003718 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003719 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003720 if (first_xattr_slot != -1) {
3721 path->slots[0] = first_xattr_slot;
3722 ret = btrfs_load_inode_props(inode, path);
3723 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003724 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003725 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003726 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003727 root->root_key.objectid, ret);
3728 }
3729 btrfs_free_path(path);
3730
Al Viro72c04902009-06-24 16:58:48 -04003731 if (!maybe_acls)
3732 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003733
Chris Mason39279cc2007-06-12 06:35:45 -04003734 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003735 case S_IFREG:
3736 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003737 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 inode->i_fop = &btrfs_file_operations;
3739 inode->i_op = &btrfs_file_inode_operations;
3740 break;
3741 case S_IFDIR:
3742 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003743 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003744 break;
3745 case S_IFLNK:
3746 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003747 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003748 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3749 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003750 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003751 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003752 init_special_inode(inode, inode->i_mode, rdev);
3753 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003755
3756 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003757 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003758
3759make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003760 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003762 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003763}
3764
Chris Masond352ac62008-09-29 15:18:18 -04003765/*
3766 * given a leaf and an inode, copy the inode fields into the leaf
3767 */
Chris Masone02119d2008-09-05 16:13:11 -04003768static void fill_inode_item(struct btrfs_trans_handle *trans,
3769 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003770 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003771 struct inode *inode)
3772{
Liu Bo51fab692012-12-27 09:01:21 +00003773 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003774
Liu Bo51fab692012-12-27 09:01:21 +00003775 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
Liu Bo51fab692012-12-27 09:01:21 +00003777 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3778 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3779 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3780 &token);
3781 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3782 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
David Sterbaa937b972014-12-12 17:39:12 +01003784 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003785 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003786 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003787 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_mtime.tv_nsec, &token);
3793
David Sterbaa937b972014-12-12 17:39:12 +01003794 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003795 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_ctime.tv_nsec, &token);
3798
chandan r9cc97d62012-07-04 12:48:07 +05303799 btrfs_set_token_timespec_sec(leaf, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_sec, &token);
3801 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3803
Liu Bo51fab692012-12-27 09:01:21 +00003804 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3805 &token);
3806 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3807 &token);
3808 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3809 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3810 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3811 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3812 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003813}
3814
Chris Masond352ac62008-09-29 15:18:18 -04003815/*
3816 * copy everything in the in-memory inode into the btree.
3817 */
Chris Mason21151332011-11-10 20:39:08 -05003818static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003819 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003820{
3821 struct btrfs_inode_item *inode_item;
3822 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003824 int ret;
3825
3826 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 if (!path)
3828 return -ENOMEM;
3829
Chris Masonb9473432009-03-13 11:00:37 -04003830 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003831 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3832 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003833 if (ret) {
3834 if (ret > 0)
3835 ret = -ENOENT;
3836 goto failed;
3837 }
3838
Chris Mason5f39d392007-10-15 16:14:19 -04003839 leaf = path->nodes[0];
3840 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003841 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003842
Chris Masone02119d2008-09-05 16:13:11 -04003843 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003844 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003845 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003846 ret = 0;
3847failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003848 btrfs_free_path(path);
3849 return ret;
3850}
3851
Chris Masond352ac62008-09-29 15:18:18 -04003852/*
Chris Mason21151332011-11-10 20:39:08 -05003853 * copy everything in the in-memory inode into the btree.
3854 */
3855noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root, struct inode *inode)
3857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003858 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003859 int ret;
3860
3861 /*
3862 * If the inode is a free space inode, we can deadlock during commit
3863 * if we put it into the delayed code.
3864 *
3865 * The data relocation inode should also be directly updated
3866 * without delay
3867 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003868 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003869 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003870 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003871 btrfs_update_root_times(trans, root);
3872
Chris Mason21151332011-11-10 20:39:08 -05003873 ret = btrfs_delayed_update_inode(trans, root, inode);
3874 if (!ret)
3875 btrfs_set_inode_last_trans(trans, inode);
3876 return ret;
3877 }
3878
3879 return btrfs_update_inode_item(trans, root, inode);
3880}
3881
Josef Bacikbe6aef62012-10-22 15:43:12 -04003882noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root,
3884 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003885{
3886 int ret;
3887
3888 ret = btrfs_update_inode(trans, root, inode);
3889 if (ret == -ENOSPC)
3890 return btrfs_update_inode_item(trans, root, inode);
3891 return ret;
3892}
3893
3894/*
Chris Masond352ac62008-09-29 15:18:18 -04003895 * unlink helper that gets used here in inode.c and in the tree logging
3896 * recovery code. It remove a link in a directory with a given name, and
3897 * also drops the back refs in the inode to the directory
3898 */
Al Viro92986792011-03-04 17:14:37 +00003899static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3900 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003901 struct btrfs_inode *dir,
3902 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003903 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003904{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003905 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003908 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003911 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003912 u64 ino = btrfs_ino(inode);
3913 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003914
3915 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003916 if (!path) {
3917 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003918 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003919 }
3920
Chris Masonb9473432009-03-13 11:00:37 -04003921 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003922 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003923 name, name_len, -1);
3924 if (IS_ERR(di)) {
3925 ret = PTR_ERR(di);
3926 goto err;
3927 }
3928 if (!di) {
3929 ret = -ENOENT;
3930 goto err;
3931 }
Chris Mason5f39d392007-10-15 16:14:19 -04003932 leaf = path->nodes[0];
3933 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003934 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003935 if (ret)
3936 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003937 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003938
Miao Xie67de1172013-12-26 13:07:06 +08003939 /*
3940 * If we don't have dir index, we have to get it by looking up
3941 * the inode ref, since we get the inode ref, remove it directly,
3942 * it is unnecessary to do delayed deletion.
3943 *
3944 * But if we have dir index, needn't search inode ref to get it.
3945 * Since the inode ref is close to the inode item, it is better
3946 * that we delay to delete it, and just do this deletion when
3947 * we update the inode item.
3948 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003949 if (inode->dir_index) {
3950 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003951 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003952 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003953 goto skip_backref;
3954 }
3955 }
3956
Li Zefan33345d012011-04-20 10:31:50 +08003957 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3958 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003959 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003960 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003961 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003962 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003963 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003964 goto err;
3965 }
Miao Xie67de1172013-12-26 13:07:06 +08003966skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003967 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003969 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 }
Chris Mason39279cc2007-06-12 06:35:45 -04003972
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003973 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3974 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003976 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 goto err;
3978 }
Chris Masone02119d2008-09-05 16:13:11 -04003979
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003980 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3981 index);
Chris Mason6418c962010-10-30 07:34:24 -04003982 if (ret == -ENOENT)
3983 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003984 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003985 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003986err:
3987 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003988 if (ret)
3989 goto out;
3990
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003991 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003992 inode_inc_iversion(&inode->vfs_inode);
3993 inode_inc_iversion(&dir->vfs_inode);
3994 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3995 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3996 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003997out:
Chris Mason39279cc2007-06-12 06:35:45 -04003998 return ret;
3999}
4000
Al Viro92986792011-03-04 17:14:37 +00004001int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004003 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004004 const char *name, int name_len)
4005{
4006 int ret;
4007 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4008 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 drop_nlink(&inode->vfs_inode);
4010 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004011 }
4012 return ret;
4013}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014
4015/*
4016 * helper to start transaction for unlink and rmdir.
4017 *
Josef Bacikd52be812013-05-29 14:54:47 -04004018 * unlink and rmdir are special in btrfs, they do not always free space, so
4019 * if we cannot make our reservations the normal way try and see if there is
4020 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4021 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022 */
Josef Bacikd52be812013-05-29 14:54:47 -04004023static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026
Josef Bacike70bea52011-10-11 14:18:24 -04004027 /*
4028 * 1 for the possible orphan item
4029 * 1 for the dir item
4030 * 1 for the dir index
4031 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004032 * 1 for the inode
4033 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004034 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035}
4036
Chris Mason39279cc2007-06-12 06:35:45 -04004037static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4038{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004039 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004041 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004042 int ret;
4043
Josef Bacikd52be812013-05-29 14:54:47 -04004044 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004046 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004047
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004048 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4049 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004050
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4052 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4053 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004054 if (ret)
4055 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004056
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004058 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004059 if (ret)
4060 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004061 }
Josef Bacik7b128762008-07-24 12:17:14 -04004062
Tsutomu Itohb5324022011-07-19 07:27:20 +00004063out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004064 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004065 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004066 return ret;
4067}
4068
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 struct inode *dir, u64 objectid,
4072 const char *name, int name_len)
4073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004074 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075 struct btrfs_path *path;
4076 struct extent_buffer *leaf;
4077 struct btrfs_dir_item *di;
4078 struct btrfs_key key;
4079 u64 index;
4080 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004081 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082
4083 path = btrfs_alloc_path();
4084 if (!path)
4085 return -ENOMEM;
4086
Li Zefan33345d012011-04-20 10:31:50 +08004087 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004089 if (IS_ERR_OR_NULL(di)) {
4090 if (!di)
4091 ret = -ENOENT;
4092 else
4093 ret = PTR_ERR(di);
4094 goto out;
4095 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096
4097 leaf = path->nodes[0];
4098 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4099 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4100 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004102 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 goto out;
4104 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004105 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004107 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4108 root->root_key.objectid, dir_ino,
4109 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004112 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 goto out;
4114 }
Li Zefan33345d012011-04-20 10:31:50 +08004115 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (IS_ERR_OR_NULL(di)) {
4118 if (!di)
4119 ret = -ENOENT;
4120 else
4121 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004122 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 goto out;
4124 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125
4126 leaf = path->nodes[0];
4127 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004128 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129 index = key.offset;
4130 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004133 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004135 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 goto out;
4137 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004138
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004139 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004140 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004141 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004142 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004143 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004144 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004146 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004147 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148}
4149
Chris Mason39279cc2007-06-12 06:35:45 -04004150static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4151{
David Howells2b0143b2015-03-17 22:25:59 +00004152 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004153 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004156 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004157
David Sterbab3ae2442012-09-13 16:04:34 -06004158 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004159 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004160 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004161 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004162
Josef Bacikd52be812013-05-29 14:54:47 -04004163 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004165 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004166
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004167 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004168 err = btrfs_unlink_subvol(trans, root, dir,
4169 BTRFS_I(inode)->location.objectid,
4170 dentry->d_name.name,
4171 dentry->d_name.len);
4172 goto out;
4173 }
4174
Josef Bacik7b128762008-07-24 12:17:14 -04004175 err = btrfs_orphan_add(trans, inode);
4176 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004177 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004178
Filipe Manana44f714d2016-06-06 16:11:13 +01004179 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4180
Chris Mason39279cc2007-06-12 06:35:45 -04004181 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004182 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4183 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4184 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004185 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004186 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004187 /*
4188 * Propagate the last_unlink_trans value of the deleted dir to
4189 * its parent directory. This is to prevent an unrecoverable
4190 * log tree in the case we do something like this:
4191 * 1) create dir foo
4192 * 2) create snapshot under dir foo
4193 * 3) delete the snapshot
4194 * 4) rmdir foo
4195 * 5) mkdir foo
4196 * 6) fsync foo or some file inside foo
4197 */
4198 if (last_unlink_trans >= trans->transid)
4199 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4200 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004202 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004203 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004204
Chris Mason39279cc2007-06-12 06:35:45 -04004205 return err;
4206}
4207
Chris Mason28f75a02015-02-04 06:59:29 -08004208static int truncate_space_check(struct btrfs_trans_handle *trans,
4209 struct btrfs_root *root,
4210 u64 bytes_deleted)
4211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004212 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004213 int ret;
4214
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004215 /*
4216 * This is only used to apply pressure to the enospc system, we don't
4217 * intend to use this reservation at all.
4218 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004219 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004220 bytes_deleted *= fs_info->nodesize;
4221 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004222 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004223 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004224 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004225 trans->transid,
4226 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004227 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004228 }
Chris Mason28f75a02015-02-04 06:59:29 -08004229 return ret;
4230
4231}
4232
Filipe Manana0305cd52015-10-16 12:34:25 +01004233static int truncate_inline_extent(struct inode *inode,
4234 struct btrfs_path *path,
4235 struct btrfs_key *found_key,
4236 const u64 item_end,
4237 const u64 new_size)
4238{
4239 struct extent_buffer *leaf = path->nodes[0];
4240 int slot = path->slots[0];
4241 struct btrfs_file_extent_item *fi;
4242 u32 size = (u32)(new_size - found_key->offset);
4243 struct btrfs_root *root = BTRFS_I(inode)->root;
4244
4245 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4246
4247 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4248 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004249 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004250
4251 /*
4252 * Zero out the remaining of the last page of our inline extent,
4253 * instead of directly truncating our inline extent here - that
4254 * would be much more complex (decompressing all the data, then
4255 * compressing the truncated data, which might be bigger than
4256 * the size of the inline extent, resize the extent, etc).
4257 * We release the path because to get the page we might need to
4258 * read the extent item from disk (data not in the page cache).
4259 */
4260 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304261 return btrfs_truncate_block(inode, offset, page_end - offset,
4262 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004263 }
4264
4265 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4266 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004267 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004268
4269 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4270 inode_sub_bytes(inode, item_end + 1 - new_size);
4271
4272 return 0;
4273}
4274
Chris Mason323ac952008-10-01 19:05:46 -04004275/*
Chris Mason39279cc2007-06-12 06:35:45 -04004276 * this can truncate away extent items, csum items and directory items.
4277 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004278 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004279 *
4280 * csum items that cross the new i_size are truncated to the new size
4281 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004282 *
4283 * min_type is the minimum key type to truncate down to. If set to 0, this
4284 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004285 */
Yan, Zheng80825102009-11-12 09:35:36 +00004286int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4287 struct btrfs_root *root,
4288 struct inode *inode,
4289 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004291 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004293 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004294 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004295 struct btrfs_key key;
4296 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004298 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004300 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004301 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004302 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004303 int found_extent;
4304 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004305 int pending_del_nr = 0;
4306 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004307 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004308 int ret;
4309 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004310 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004311 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004312 bool be_nice = 0;
4313 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004314 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004315
4316 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004317
Chris Mason28ed1342014-12-17 09:41:04 -08004318 /*
4319 * for non-free space inodes and ref cows, we want to back off from
4320 * time to time
4321 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004322 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004323 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4324 be_nice = 1;
4325
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004326 path = btrfs_alloc_path();
4327 if (!path)
4328 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004329 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004330
Josef Bacik5dc562c2012-08-17 13:14:17 -04004331 /*
4332 * We want to drop from the next block forward in case this new size is
4333 * not block aligned since we will be keeping the last block of the
4334 * extent just the way it is.
4335 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004336 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004338 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004339 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004340 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004341
Miao Xie16cdcec2011-04-22 18:12:22 +08004342 /*
4343 * This function is also used to drop the items in the log tree before
4344 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4345 * it is used to drop the loged items. So we shouldn't kill the delayed
4346 * items.
4347 */
4348 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004349 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004350
Li Zefan33345d012011-04-20 10:31:50 +08004351 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004352 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004353 key.type = (u8)-1;
4354
Chris Mason85e21ba2008-01-29 15:11:36 -05004355search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004356 /*
4357 * with a 16K leaf size and 128MB extents, you can actually queue
4358 * up a huge file in a single leaf. Most of the time that
4359 * bytes_deleted is > 0, it will be huge by the time we get here
4360 */
Byongho Leeee221842015-12-15 01:42:10 +09004361 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004362 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004363 err = -EAGAIN;
4364 goto error;
4365 }
4366 }
4367
4368
Chris Masonb9473432009-03-13 11:00:37 -04004369 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004370 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004371 if (ret < 0) {
4372 err = ret;
4373 goto out;
4374 }
Chris Masond3977122009-01-05 21:25:51 -05004375
Chris Mason85e21ba2008-01-29 15:11:36 -05004376 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004377 /* there are no items in the tree for us to truncate, we're
4378 * done
4379 */
Yan, Zheng80825102009-11-12 09:35:36 +00004380 if (path->slots[0] == 0)
4381 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 path->slots[0]--;
4383 }
4384
Chris Masond3977122009-01-05 21:25:51 -05004385 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004386 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004387 leaf = path->nodes[0];
4388 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004389 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004390
Li Zefan33345d012011-04-20 10:31:50 +08004391 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004392 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004393
Chris Mason85e21ba2008-01-29 15:11:36 -05004394 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004395 break;
4396
Chris Mason5f39d392007-10-15 16:14:19 -04004397 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004399 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004400 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004401 extent_type = btrfs_file_extent_type(leaf, fi);
4402 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004403 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004404 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004405 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004406 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004407 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004408 }
Yan008630c2007-11-07 13:31:09 -05004409 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 }
Yan, Zheng80825102009-11-12 09:35:36 +00004411 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004412 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004413 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004414 if (item_end < new_size) {
4415 /*
4416 * With NO_HOLES mode, for the following mapping
4417 *
4418 * [0-4k][hole][8k-12k]
4419 *
4420 * if truncating isize down to 6k, it ends up
4421 * isize being 8k.
4422 */
4423 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4424 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004425 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004426 }
Yan, Zheng80825102009-11-12 09:35:36 +00004427 if (found_key.offset >= new_size)
4428 del_item = 1;
4429 else
4430 del_item = 0;
4431 }
Chris Mason39279cc2007-06-12 06:35:45 -04004432 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004434 if (found_type != BTRFS_EXTENT_DATA_KEY)
4435 goto delete;
4436
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004437 if (del_item)
4438 last_size = found_key.offset;
4439 else
4440 last_size = new_size;
4441
Chris Mason179e29e2007-11-01 11:28:41 -04004442 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004443 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004444 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004445 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004446 u64 orig_num_bytes =
4447 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004448 extent_num_bytes = ALIGN(new_size -
4449 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004450 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004451 btrfs_set_file_extent_num_bytes(leaf, fi,
4452 extent_num_bytes);
4453 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004454 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004455 if (test_bit(BTRFS_ROOT_REF_COWS,
4456 &root->state) &&
4457 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004458 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004459 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004460 } else {
Chris Masondb945352007-10-15 16:15:53 -04004461 extent_num_bytes =
4462 btrfs_file_extent_disk_num_bytes(leaf,
4463 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004464 extent_offset = found_key.offset -
4465 btrfs_file_extent_offset(leaf, fi);
4466
Chris Mason39279cc2007-06-12 06:35:45 -04004467 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004468 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004469 if (extent_start != 0) {
4470 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004471 if (test_bit(BTRFS_ROOT_REF_COWS,
4472 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004473 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004474 }
4475 }
Chris Mason90692182008-02-08 13:49:28 -05004476 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004477 /*
4478 * we can't truncate inline items that have had
4479 * special encodings
4480 */
4481 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004482 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4483 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004484
4485 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004486 * Need to release path in order to truncate a
4487 * compressed extent. So delete any accumulated
4488 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004489 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004490 if (btrfs_file_extent_compression(leaf, fi) !=
4491 BTRFS_COMPRESS_NONE && pending_del_nr) {
4492 err = btrfs_del_items(trans, root, path,
4493 pending_del_slot,
4494 pending_del_nr);
4495 if (err) {
4496 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004497 err);
4498 goto error;
4499 }
4500 pending_del_nr = 0;
4501 }
4502
4503 err = truncate_inline_extent(inode, path,
4504 &found_key,
4505 item_end,
4506 new_size);
4507 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004508 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004509 goto error;
4510 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004511 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4512 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004513 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004514 }
Chris Mason39279cc2007-06-12 06:35:45 -04004515 }
Chris Mason179e29e2007-11-01 11:28:41 -04004516delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004517 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 if (!pending_del_nr) {
4519 /* no pending yet, add ourselves */
4520 pending_del_slot = path->slots[0];
4521 pending_del_nr = 1;
4522 } else if (pending_del_nr &&
4523 path->slots[0] + 1 == pending_del_slot) {
4524 /* hop on the pending chunk */
4525 pending_del_nr++;
4526 pending_del_slot = path->slots[0];
4527 } else {
Chris Masond3977122009-01-05 21:25:51 -05004528 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004529 }
Chris Mason39279cc2007-06-12 06:35:45 -04004530 } else {
4531 break;
4532 }
Chris Mason28f75a02015-02-04 06:59:29 -08004533 should_throttle = 0;
4534
Miao Xie27cdeb72014-04-02 19:51:05 +08004535 if (found_extent &&
4536 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004538 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004539 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004540 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004541 extent_num_bytes, 0,
4542 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004543 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004544 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004545 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4546 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004547 trans->delayed_ref_updates * 2,
4548 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004549 if (be_nice) {
4550 if (truncate_space_check(trans, root,
4551 extent_num_bytes)) {
4552 should_end = 1;
4553 }
4554 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004555 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004556 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004559
Yan, Zheng80825102009-11-12 09:35:36 +00004560 if (found_type == BTRFS_INODE_ITEM_KEY)
4561 break;
4562
4563 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004564 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004565 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004566 if (pending_del_nr) {
4567 ret = btrfs_del_items(trans, root, path,
4568 pending_del_slot,
4569 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004570 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004571 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004572 goto error;
4573 }
Yan, Zheng80825102009-11-12 09:35:36 +00004574 pending_del_nr = 0;
4575 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004576 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004577 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004578 unsigned long updates = trans->delayed_ref_updates;
4579 if (updates) {
4580 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004581 ret = btrfs_run_delayed_refs(trans,
4582 fs_info,
4583 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004584 if (ret && !err)
4585 err = ret;
4586 }
4587 }
Chris Mason28f75a02015-02-04 06:59:29 -08004588 /*
4589 * if we failed to refill our space rsv, bail out
4590 * and let the transaction restart
4591 */
4592 if (should_end) {
4593 err = -EAGAIN;
4594 goto error;
4595 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004596 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004597 } else {
4598 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Yan, Zheng80825102009-11-12 09:35:36 +00004601out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004602 if (pending_del_nr) {
4603 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4604 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004605 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004606 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004607 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004608error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004609 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004610 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004613
Liu Bo19fd2df2016-12-01 13:01:02 -08004614 if (err == 0) {
4615 /* only inline file may have last_size != new_size */
4616 if (new_size >= fs_info->sectorsize ||
4617 new_size > fs_info->max_inline)
4618 ASSERT(last_size == new_size);
4619 }
4620
Byongho Leeee221842015-12-15 01:42:10 +09004621 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004622 unsigned long updates = trans->delayed_ref_updates;
4623 if (updates) {
4624 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004625 ret = btrfs_run_delayed_refs(trans, fs_info,
4626 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004627 if (ret && !err)
4628 err = ret;
4629 }
4630 }
Yan, Zheng80825102009-11-12 09:35:36 +00004631 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004632}
4633
Chris Masona52d9a82007-08-27 16:49:44 -04004634/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304635 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004636 * @inode - inode that we're zeroing
4637 * @from - the offset to start zeroing
4638 * @len - the length to zero, 0 to zero the entire range respective to the
4639 * offset
4640 * @front - zero up to the offset instead of from the offset on
4641 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304642 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004643 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004644 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304645int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004646 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004647{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004649 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004650 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4651 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004652 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004653 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004655 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304656 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004657 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004658 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004659 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304660 u64 block_start;
4661 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004662
Josef Bacik2aaa6652012-08-29 14:27:18 -04004663 if ((offset & (blocksize - 1)) == 0 &&
4664 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004665 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304666
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004667 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304668 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004669 if (ret)
4670 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004671
Chris Mason211c17f2008-05-15 09:13:45 -04004672again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004673 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004674 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004675 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304676 round_down(from, blocksize),
4677 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004678 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004679 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004680 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004681
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682 block_start = round_down(from, blocksize);
4683 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004684
Chris Masona52d9a82007-08-27 16:49:44 -04004685 if (!PageUptodate(page)) {
4686 ret = btrfs_readpage(NULL, page);
4687 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004688 if (page->mapping != mapping) {
4689 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004690 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004691 goto again;
4692 }
Chris Masona52d9a82007-08-27 16:49:44 -04004693 if (!PageUptodate(page)) {
4694 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004695 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004696 }
4697 }
Chris Mason211c17f2008-05-15 09:13:45 -04004698 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004699
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304700 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701 set_page_extent_mapped(page);
4702
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304703 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004704 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004706 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004707 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004708 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004709 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 btrfs_put_ordered_extent(ordered);
4711 goto again;
4712 }
4713
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004715 EXTENT_DIRTY | EXTENT_DELALLOC |
4716 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004717 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004718
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004720 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004721 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004723 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004724 goto out_unlock;
4725 }
4726
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004728 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304729 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004731 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 memset(kaddr + (block_start - page_offset(page)),
4733 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 memset(kaddr + (block_start - page_offset(page)) + offset,
4736 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 flush_dcache_page(page);
4738 kunmap(page);
4739 }
Chris Mason247e7432008-07-17 12:53:51 -04004740 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004743 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004744
Chris Mason89642222008-07-24 09:41:53 -04004745out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004746 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 btrfs_delalloc_release_space(inode, block_start,
4748 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004749 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004750 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004751out:
4752 return ret;
4753}
4754
Josef Bacik16e75492013-10-22 12:18:51 -04004755static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4756 u64 offset, u64 len)
4757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004759 struct btrfs_trans_handle *trans;
4760 int ret;
4761
4762 /*
4763 * Still need to make sure the inode looks like it's been updated so
4764 * that any holes get logged if we fsync.
4765 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4767 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004768 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4769 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4770 return 0;
4771 }
4772
4773 /*
4774 * 1 - for the one we're dropping
4775 * 1 - for the one we're adding
4776 * 1 - for updating the inode.
4777 */
4778 trans = btrfs_start_transaction(root, 3);
4779 if (IS_ERR(trans))
4780 return PTR_ERR(trans);
4781
4782 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4783 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004784 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004785 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004786 return ret;
4787 }
4788
David Sterbaf85b7372017-01-20 14:54:07 +01004789 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4790 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004791 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004792 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004793 else
4794 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004795 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004796 return ret;
4797}
4798
Josef Bacik695a0d02011-03-04 15:46:53 -05004799/*
4800 * This function puts in dummy file extents for the area we're creating a hole
4801 * for. So if we are truncating this file to a larger size we need to insert
4802 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4803 * the range between oldsize and size
4804 */
Josef Bacika41ad392011-01-31 15:30:16 -05004805int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004808 struct btrfs_root *root = BTRFS_I(inode)->root;
4809 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004810 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004811 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004812 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4814 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004815 u64 last_byte;
4816 u64 cur_offset;
4817 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004818 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004819
Josef Bacika71754f2013-06-17 17:14:39 -04004820 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 * If our size started in the middle of a block we need to zero out the
4822 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004823 * expose stale data.
4824 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004826 if (err)
4827 return err;
4828
Yan Zheng9036c102008-10-30 14:19:41 -04004829 if (size <= hole_start)
4830 return 0;
4831
Yan Zheng9036c102008-10-30 14:19:41 -04004832 while (1) {
4833 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004834
David Sterbaff13db42015-12-03 14:30:40 +01004835 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004836 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004837 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4838 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004839 if (!ordered)
4840 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004841 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4842 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004843 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004844 btrfs_put_ordered_extent(ordered);
4845 }
4846
Yan Zheng9036c102008-10-30 14:19:41 -04004847 cur_offset = hole_start;
4848 while (1) {
4849 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4850 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004851 if (IS_ERR(em)) {
4852 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004853 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004854 break;
4855 }
Yan Zheng9036c102008-10-30 14:19:41 -04004856 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004858 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004860 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004861
Josef Bacik16e75492013-10-22 12:18:51 -04004862 err = maybe_insert_hole(root, inode, cur_offset,
4863 hole_size);
4864 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004865 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004866 btrfs_drop_extent_cache(inode, cur_offset,
4867 cur_offset + hole_size - 1, 0);
4868 hole_em = alloc_extent_map();
4869 if (!hole_em) {
4870 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4871 &BTRFS_I(inode)->runtime_flags);
4872 goto next;
4873 }
4874 hole_em->start = cur_offset;
4875 hole_em->len = hole_size;
4876 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004877
Josef Bacik5dc562c2012-08-17 13:14:17 -04004878 hole_em->block_start = EXTENT_MAP_HOLE;
4879 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004880 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004881 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004882 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004883 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004885
4886 while (1) {
4887 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004888 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004889 write_unlock(&em_tree->lock);
4890 if (err != -EEXIST)
4891 break;
4892 btrfs_drop_extent_cache(inode, cur_offset,
4893 cur_offset +
4894 hole_size - 1, 0);
4895 }
4896 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004897 }
Josef Bacik16e75492013-10-22 12:18:51 -04004898next:
Yan Zheng9036c102008-10-30 14:19:41 -04004899 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004900 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004901 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004902 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004903 break;
4904 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004905 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004906 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4907 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004908 return err;
4909}
4910
Eric Sandeen3972f262013-01-12 02:57:22 +00004911static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004912{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004913 struct btrfs_root *root = BTRFS_I(inode)->root;
4914 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004915 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004916 loff_t newsize = attr->ia_size;
4917 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004918 int ret;
4919
Eric Sandeen3972f262013-01-12 02:57:22 +00004920 /*
4921 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4922 * special case where we need to update the times despite not having
4923 * these flags set. For all other operations the VFS set these flags
4924 * explicitly if it wants a timestamp update.
4925 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004926 if (newsize != oldsize) {
4927 inode_inc_iversion(inode);
4928 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4929 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004930 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004931 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004932
Josef Bacika41ad392011-01-31 15:30:16 -05004933 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004934 /*
4935 * Don't do an expanding truncate while snapshoting is ongoing.
4936 * This is to ensure the snapshot captures a fully consistent
4937 * state of this file - if the snapshot captures this expanding
4938 * truncation, it must capture all writes that happened before
4939 * this truncation.
4940 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004941 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004942 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 if (ret) {
4944 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004945 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 }
Yan, Zheng80825102009-11-12 09:35:36 +00004947
Miao Xief4a2f4c2011-12-14 20:12:01 -05004948 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 if (IS_ERR(trans)) {
4950 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004951 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004953
4954 i_size_write(inode, newsize);
4955 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304956 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004958 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004959 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004960 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004961
Josef Bacika41ad392011-01-31 15:30:16 -05004962 /*
4963 * We're truncating a file that used to have good data down to
4964 * zero. Make sure it gets into the ordered flush list so that
4965 * any new writes get down to disk quickly.
4966 */
4967 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004968 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4969 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004970
Josef Bacikf3fe8202013-01-07 17:03:21 -05004971 /*
4972 * 1 for the orphan item we're going to add
4973 * 1 for the orphan item deletion.
4974 */
4975 trans = btrfs_start_transaction(root, 2);
4976 if (IS_ERR(trans))
4977 return PTR_ERR(trans);
4978
4979 /*
4980 * We need to do this in case we fail at _any_ point during the
4981 * actual truncate. Once we do the truncate_setsize we could
4982 * invalidate pages which forces any outstanding ordered io to
4983 * be instantly completed which will give us extents that need
4984 * to be truncated. If we fail to get an orphan inode down we
4985 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004986 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004987 * will be consistent.
4988 */
4989 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004990 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004991 if (ret)
4992 return ret;
4993
Josef Bacika41ad392011-01-31 15:30:16 -05004994 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4995 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004996
4997 /* Disable nonlocked read DIO to avoid the end less truncate */
4998 btrfs_inode_block_unlocked_dio(inode);
4999 inode_dio_wait(inode);
5000 btrfs_inode_resume_unlocked_dio(inode);
5001
Josef Bacika41ad392011-01-31 15:30:16 -05005002 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005003 if (ret && inode->i_nlink) {
5004 int err;
5005
Liu Bo19fd2df2016-12-01 13:01:02 -08005006 /* To get a stable disk_i_size */
5007 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5008 if (err) {
5009 btrfs_orphan_del(NULL, inode);
5010 return err;
5011 }
5012
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005013 /*
5014 * failed to truncate, disk_i_size is only adjusted down
5015 * as we remove extents, so it should represent the true
5016 * size of the inode, so reset the in memory size and
5017 * delete our orphan entry.
5018 */
5019 trans = btrfs_join_transaction(root);
5020 if (IS_ERR(trans)) {
5021 btrfs_orphan_del(NULL, inode);
5022 return ret;
5023 }
5024 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5025 err = btrfs_orphan_del(trans, inode);
5026 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005027 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005028 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005029 }
Yan, Zheng80825102009-11-12 09:35:36 +00005030 }
5031
Josef Bacika41ad392011-01-31 15:30:16 -05005032 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005033}
5034
Chris Mason39279cc2007-06-12 06:35:45 -04005035static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5036{
David Howells2b0143b2015-03-17 22:25:59 +00005037 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005038 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005039 int err;
5040
Li Zefanb83cc962010-12-20 16:04:08 +08005041 if (btrfs_root_readonly(root))
5042 return -EROFS;
5043
Jan Kara31051c82016-05-26 16:55:18 +02005044 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005045 if (err)
5046 return err;
5047
Chris Mason5a3f23d2009-03-31 13:27:11 -04005048 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005049 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005050 if (err)
5051 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005052 }
Yan Zheng9036c102008-10-30 14:19:41 -04005053
Christoph Hellwig10257742010-06-04 11:30:02 +02005054 if (attr->ia_valid) {
5055 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005056 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005057 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005058
Josef Bacik22c44fe2011-11-30 10:45:38 -05005059 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005060 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005061 }
5062
Chris Mason39279cc2007-06-12 06:35:45 -04005063 return err;
5064}
Chris Mason61295eb2008-01-14 16:24:38 -05005065
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005066/*
5067 * While truncating the inode pages during eviction, we get the VFS calling
5068 * btrfs_invalidatepage() against each page of the inode. This is slow because
5069 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5070 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5071 * extent_state structures over and over, wasting lots of time.
5072 *
5073 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5074 * those expensive operations on a per page basis and do only the ordered io
5075 * finishing, while we release here the extent_map and extent_state structures,
5076 * without the excessive merging and splitting.
5077 */
5078static void evict_inode_truncate_pages(struct inode *inode)
5079{
5080 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5081 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5082 struct rb_node *node;
5083
5084 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005085 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005086
5087 write_lock(&map_tree->lock);
5088 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5089 struct extent_map *em;
5090
5091 node = rb_first(&map_tree->map);
5092 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005093 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5094 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005095 remove_extent_mapping(map_tree, em);
5096 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005097 if (need_resched()) {
5098 write_unlock(&map_tree->lock);
5099 cond_resched();
5100 write_lock(&map_tree->lock);
5101 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005102 }
5103 write_unlock(&map_tree->lock);
5104
Filipe Manana6ca07092015-05-26 00:55:42 +01005105 /*
5106 * Keep looping until we have no more ranges in the io tree.
5107 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005108 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5109 * still in progress (unlocked the pages in the bio but did not yet
5110 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005111 * ranges can still be locked and eviction started because before
5112 * submitting those bios, which are executed by a separate task (work
5113 * queue kthread), inode references (inode->i_count) were not taken
5114 * (which would be dropped in the end io callback of each bio).
5115 * Therefore here we effectively end up waiting for those bios and
5116 * anyone else holding locked ranges without having bumped the inode's
5117 * reference count - if we don't do it, when they access the inode's
5118 * io_tree to unlock a range it may be too late, leading to an
5119 * use-after-free issue.
5120 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005121 spin_lock(&io_tree->lock);
5122 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5123 struct extent_state *state;
5124 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005125 u64 start;
5126 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005127
5128 node = rb_first(&io_tree->state);
5129 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005130 start = state->start;
5131 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005132 spin_unlock(&io_tree->lock);
5133
David Sterbaff13db42015-12-03 14:30:40 +01005134 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005135
5136 /*
5137 * If still has DELALLOC flag, the extent didn't reach disk,
5138 * and its reserved space won't be freed by delayed_ref.
5139 * So we need to free its reserved space here.
5140 * (Refer to comment in btrfs_invalidatepage, case 2)
5141 *
5142 * Note, end is the bytenr of last byte, so we need + 1 here.
5143 */
5144 if (state->state & EXTENT_DELALLOC)
5145 btrfs_qgroup_free_data(inode, start, end - start + 1);
5146
Filipe Manana6ca07092015-05-26 00:55:42 +01005147 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005148 EXTENT_LOCKED | EXTENT_DIRTY |
5149 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5150 EXTENT_DEFRAG, 1, 1,
5151 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005152
Filipe Manana7064dd52014-08-08 02:47:05 +01005153 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005154 spin_lock(&io_tree->lock);
5155 }
5156 spin_unlock(&io_tree->lock);
5157}
5158
Al Virobd555972010-06-07 11:35:40 -04005159void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005161 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005162 struct btrfs_trans_handle *trans;
5163 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005164 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005165 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005166 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005167 int ret;
5168
liubo1abe9b82011-03-24 11:18:59 +00005169 trace_btrfs_inode_evict(inode);
5170
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005171 if (!root) {
5172 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5173 return;
5174 }
5175
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005176 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005177
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005178 evict_inode_truncate_pages(inode);
5179
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005180 if (inode->i_nlink &&
5181 ((btrfs_root_refs(&root->root_item) != 0 &&
5182 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005183 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005184 goto no_delete;
5185
Chris Mason39279cc2007-06-12 06:35:45 -04005186 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005187 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005188 goto no_delete;
5189 }
Al Virobd555972010-06-07 11:35:40 -04005190 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005191 if (!special_file(inode->i_mode))
5192 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005193
Miao Xief6124962014-09-12 18:44:04 +08005194 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5195
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005196 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005197 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005198 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005199 goto no_delete;
5200 }
5201
Yan, Zheng76dda932009-09-21 16:00:26 -04005202 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005203 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5204 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005205 goto no_delete;
5206 }
5207
Nikolay Borisovaa790212017-01-10 20:35:40 +02005208 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005209 if (ret) {
5210 btrfs_orphan_del(NULL, inode);
5211 goto no_delete;
5212 }
5213
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005214 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005215 if (!rsv) {
5216 btrfs_orphan_del(NULL, inode);
5217 goto no_delete;
5218 }
Josef Bacik4a338542011-08-29 11:01:31 -04005219 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005220 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005221 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005222
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005223 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005224
Josef Bacik4289a662011-08-05 13:22:24 -04005225 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005226 * This is a bit simpler than btrfs_truncate since we've already
5227 * reserved our space for our orphan item in the unlink, so we just
5228 * need to reserve some slack space in case we add bytes and update
5229 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005230 */
Yan, Zheng80825102009-11-12 09:35:36 +00005231 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005232 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5233 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005234
Josef Bacik726c35f2011-09-26 15:46:06 -04005235 /*
5236 * Try and steal from the global reserve since we will
5237 * likely not use this space anyway, we want to try as
5238 * hard as possible to get this to work.
5239 */
5240 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005241 steal_from_global++;
5242 else
5243 steal_from_global = 0;
5244 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005245
Josef Bacik3bce8762015-02-24 12:35:51 -08005246 /*
5247 * steal_from_global == 0: we reserved stuff, hooray!
5248 * steal_from_global == 1: we didn't reserve stuff, boo!
5249 * steal_from_global == 2: we've committed, still not a lot of
5250 * room but maybe we'll have room in the global reserve this
5251 * time.
5252 * steal_from_global == 3: abandon all hope!
5253 */
5254 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 btrfs_warn(fs_info,
5256 "Could not get space for a delete, will truncate on mount %d",
5257 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005258 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005259 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005260 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005261 }
5262
Miao Xie0e8c36a2012-12-19 06:59:51 +00005263 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005264 if (IS_ERR(trans)) {
5265 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005266 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005267 goto no_delete;
5268 }
5269
Josef Bacik3bce8762015-02-24 12:35:51 -08005270 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005271 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005272 * sure there is room to do it, if not we need to commit and try
5273 * again.
5274 */
5275 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005276 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005277 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005278 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005279 else
5280 ret = -ENOSPC;
5281 }
5282
5283 /*
5284 * Couldn't steal from the global reserve, we have too much
5285 * pending stuff built up, commit the transaction and try it
5286 * again.
5287 */
5288 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005289 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005290 if (ret) {
5291 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005292 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 goto no_delete;
5294 }
5295 continue;
5296 } else {
5297 steal_from_global = 0;
5298 }
5299
Josef Bacik4289a662011-08-05 13:22:24 -04005300 trans->block_rsv = rsv;
5301
Yan, Zhengd68fc572010-05-16 10:49:58 -04005302 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005303 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005304 break;
5305
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005307 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005308 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005309 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005310 }
5311
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005312 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005313
Josef Bacik4ef31a42013-08-13 14:10:08 -04005314 /*
5315 * Errors here aren't a big deal, it just means we leave orphan items
5316 * in the tree. They will be cleaned up on the next mount.
5317 */
Yan, Zheng80825102009-11-12 09:35:36 +00005318 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005319 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005320 btrfs_orphan_del(trans, inode);
5321 } else {
5322 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005323 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005324
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 trans->block_rsv = &fs_info->trans_block_rsv;
5326 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005327 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005328 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005329
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005330 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005331 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005332no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005333 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005334 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005335}
5336
5337/*
5338 * this returns the key found in the dir entry in the location pointer.
5339 * If no dir entries were found, location->objectid is 0.
5340 */
5341static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5342 struct btrfs_key *location)
5343{
5344 const char *name = dentry->d_name.name;
5345 int namelen = dentry->d_name.len;
5346 struct btrfs_dir_item *di;
5347 struct btrfs_path *path;
5348 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005349 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005350
5351 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005352 if (!path)
5353 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005354
David Sterbaf85b7372017-01-20 14:54:07 +01005355 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5356 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005357 if (IS_ERR(di))
5358 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005359
David Sterbac7040052011-04-19 18:00:01 +02005360 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005361 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005362
Chris Mason5f39d392007-10-15 16:14:19 -04005363 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005364out:
Chris Mason39279cc2007-06-12 06:35:45 -04005365 btrfs_free_path(path);
5366 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005367out_err:
5368 location->objectid = 0;
5369 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005370}
5371
5372/*
5373 * when we hit a tree root in a directory, the btrfs part of the inode
5374 * needs to be changed to reflect the root directory of the tree root. This
5375 * is kind of like crossing a mount point.
5376 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005377static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005378 struct inode *dir,
5379 struct dentry *dentry,
5380 struct btrfs_key *location,
5381 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005382{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 struct btrfs_path *path;
5384 struct btrfs_root *new_root;
5385 struct btrfs_root_ref *ref;
5386 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005387 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005388 int ret;
5389 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005390
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391 path = btrfs_alloc_path();
5392 if (!path) {
5393 err = -ENOMEM;
5394 goto out;
5395 }
Chris Mason39279cc2007-06-12 06:35:45 -04005396
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005397 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005398 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5399 key.type = BTRFS_ROOT_REF_KEY;
5400 key.offset = location->objectid;
5401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403 if (ret) {
5404 if (ret < 0)
5405 err = ret;
5406 goto out;
5407 }
Chris Mason39279cc2007-06-12 06:35:45 -04005408
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 leaf = path->nodes[0];
5410 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005411 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005412 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5413 goto out;
5414
5415 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5416 (unsigned long)(ref + 1),
5417 dentry->d_name.len);
5418 if (ret)
5419 goto out;
5420
David Sterbab3b4aa72011-04-21 01:20:15 +02005421 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005423 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424 if (IS_ERR(new_root)) {
5425 err = PTR_ERR(new_root);
5426 goto out;
5427 }
5428
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005429 *sub_root = new_root;
5430 location->objectid = btrfs_root_dirid(&new_root->root_item);
5431 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005432 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005433 err = 0;
5434out:
5435 btrfs_free_path(path);
5436 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005437}
5438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005439static void inode_tree_add(struct inode *inode)
5440{
5441 struct btrfs_root *root = BTRFS_I(inode)->root;
5442 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005443 struct rb_node **p;
5444 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005445 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005446 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447
Al Viro1d3382cb2010-10-23 15:19:20 -04005448 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005449 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005450 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005452 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453 while (*p) {
5454 parent = *p;
5455 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5456
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005457 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005458 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005459 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005460 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 else {
5462 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005463 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005464 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005465 RB_CLEAR_NODE(parent);
5466 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005467 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 }
5469 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005470 rb_link_node(new, parent, p);
5471 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472 spin_unlock(&root->inode_lock);
5473}
5474
5475static void inode_tree_del(struct inode *inode)
5476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005481 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005482 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005487 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005488
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005489 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005490 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 spin_lock(&root->inode_lock);
5492 empty = RB_EMPTY_ROOT(&root->inode_tree);
5493 spin_unlock(&root->inode_lock);
5494 if (empty)
5495 btrfs_add_dead_root(root);
5496 }
5497}
5498
Jeff Mahoney143bede2012-03-01 14:56:26 +01005499void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005500{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005501 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005502 struct rb_node *node;
5503 struct rb_node *prev;
5504 struct btrfs_inode *entry;
5505 struct inode *inode;
5506 u64 objectid = 0;
5507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005508 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005509 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005510
5511 spin_lock(&root->inode_lock);
5512again:
5513 node = root->inode_tree.rb_node;
5514 prev = NULL;
5515 while (node) {
5516 prev = node;
5517 entry = rb_entry(node, struct btrfs_inode, rb_node);
5518
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005519 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005520 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005521 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005522 node = node->rb_right;
5523 else
5524 break;
5525 }
5526 if (!node) {
5527 while (prev) {
5528 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 node = prev;
5531 break;
5532 }
5533 prev = rb_next(prev);
5534 }
5535 }
5536 while (node) {
5537 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005538 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 inode = igrab(&entry->vfs_inode);
5540 if (inode) {
5541 spin_unlock(&root->inode_lock);
5542 if (atomic_read(&inode->i_count) > 1)
5543 d_prune_aliases(inode);
5544 /*
Al Viro45321ac2010-06-07 13:43:19 -04005545 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005546 * the inode cache when its usage count
5547 * hits zero.
5548 */
5549 iput(inode);
5550 cond_resched();
5551 spin_lock(&root->inode_lock);
5552 goto again;
5553 }
5554
5555 if (cond_resched_lock(&root->inode_lock))
5556 goto again;
5557
5558 node = rb_next(node);
5559 }
5560 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561}
5562
Chris Masone02119d2008-09-05 16:13:11 -04005563static int btrfs_init_locked_inode(struct inode *inode, void *p)
5564{
5565 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005566 inode->i_ino = args->location->objectid;
5567 memcpy(&BTRFS_I(inode)->location, args->location,
5568 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005569 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 return 0;
5571}
5572
5573static int btrfs_find_actor(struct inode *inode, void *opaque)
5574{
5575 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005576 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005577 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005578}
5579
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005581 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005583{
5584 struct inode *inode;
5585 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005586 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005587
Chris Mason90d3e592014-01-09 17:28:00 -08005588 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005589 args.root = root;
5590
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005591 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005592 btrfs_init_locked_inode,
5593 (void *)&args);
5594 return inode;
5595}
5596
Balaji Rao1a54ef82008-07-21 02:01:04 +05305597/* Get an inode object given its location and corresponding root.
5598 * Returns in *is_new if the inode was read from disk
5599 */
5600struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005601 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305602{
5603 struct inode *inode;
5604
Chris Mason90d3e592014-01-09 17:28:00 -08005605 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305606 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305608
5609 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005610 int ret;
5611
5612 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005613 if (!is_bad_inode(inode)) {
5614 inode_tree_add(inode);
5615 unlock_new_inode(inode);
5616 if (new)
5617 *new = 1;
5618 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005619 unlock_new_inode(inode);
5620 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005621 ASSERT(ret < 0);
5622 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005623 }
5624 }
5625
Balaji Rao1a54ef82008-07-21 02:01:04 +05305626 return inode;
5627}
5628
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005629static struct inode *new_simple_dir(struct super_block *s,
5630 struct btrfs_key *key,
5631 struct btrfs_root *root)
5632{
5633 struct inode *inode = new_inode(s);
5634
5635 if (!inode)
5636 return ERR_PTR(-ENOMEM);
5637
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 BTRFS_I(inode)->root = root;
5639 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005640 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641
5642 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005643 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005644 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 inode->i_fop = &simple_dir_operations;
5646 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005647 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305648 inode->i_atime = inode->i_mtime;
5649 inode->i_ctime = inode->i_mtime;
5650 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651
5652 return inode;
5653}
5654
Chris Mason3de45862008-11-17 21:02:50 -05005655struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005657 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005658 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005660 struct btrfs_root *sub_root = root;
5661 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005662 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005663 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005664
5665 if (dentry->d_name.len > BTRFS_NAME_LEN)
5666 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005667
Jeff Layton39e3c952012-11-28 11:30:53 -05005668 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005669 if (ret < 0)
5670 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005671
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005673 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674
5675 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005676 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005678 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679
5680 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005682 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005683 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005684 &location, &sub_root);
5685 if (ret < 0) {
5686 if (ret != -ENOENT)
5687 inode = ERR_PTR(ret);
5688 else
5689 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5690 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005691 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005692 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005693 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005694
Julia Lawall34d19ba2011-01-24 19:55:19 +00005695 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005696 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005697 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005698 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005699 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005700 if (ret) {
5701 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005702 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005703 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005704 }
5705
Chris Mason3de45862008-11-17 21:02:50 -05005706 return inode;
5707}
5708
Nick Pigginfe15ce42011-01-07 17:49:23 +11005709static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005710{
5711 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005712 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005713
Li Zefan848cce02012-02-21 17:04:28 +08005714 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005715 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005716
Li Zefan848cce02012-02-21 17:04:28 +08005717 if (inode) {
5718 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005719 if (btrfs_root_refs(&root->root_item) == 0)
5720 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005721
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005722 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005723 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005724 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005725 return 0;
5726}
5727
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005728static void btrfs_dentry_release(struct dentry *dentry)
5729{
Daeseok Youn944a4512014-04-14 15:37:02 +09005730 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005731}
5732
Chris Mason3de45862008-11-17 21:02:50 -05005733static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005734 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005735{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005736 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005737
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005738 inode = btrfs_lookup_dentry(dir, dentry);
5739 if (IS_ERR(inode)) {
5740 if (PTR_ERR(inode) == -ENOENT)
5741 inode = NULL;
5742 else
5743 return ERR_CAST(inode);
5744 }
5745
Al Viro41d28bc2014-10-12 22:24:21 -04005746 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005747}
5748
Miao Xie16cdcec2011-04-22 18:12:22 +08005749unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005750 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5751};
5752
Al Viro9cdda8d2013-05-22 16:48:09 -04005753static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005754{
Al Viro9cdda8d2013-05-22 16:48:09 -04005755 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005757 struct btrfs_root *root = BTRFS_I(inode)->root;
5758 struct btrfs_item *item;
5759 struct btrfs_dir_item *di;
5760 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005761 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005763 struct list_head ins_list;
5764 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005766 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 unsigned char d_type;
5769 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005770 char tmp_name[32];
5771 char *name_ptr;
5772 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005773 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005774 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005775
Al Viro9cdda8d2013-05-22 16:48:09 -04005776 if (!dir_emit_dots(file, ctx))
5777 return 0;
5778
David Woodhouse49593bf2008-08-17 17:08:36 +01005779 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005780 if (!path)
5781 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005782
David Sterbae4058b52015-11-27 16:31:35 +01005783 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005784
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005785 INIT_LIST_HEAD(&ins_list);
5786 INIT_LIST_HEAD(&del_list);
5787 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005788
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005789 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005790 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005791 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005792
Chris Mason39279cc2007-06-12 06:35:45 -04005793 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5794 if (ret < 0)
5795 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005796
5797 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005798 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005799 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005800 if (slot >= btrfs_header_nritems(leaf)) {
5801 ret = btrfs_next_leaf(root, path);
5802 if (ret < 0)
5803 goto err;
5804 else if (ret > 0)
5805 break;
5806 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Chris Mason3de45862008-11-17 21:02:50 -05005808
Ross Kirkdd3cc162013-09-16 15:58:09 +01005809 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005810 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5811
5812 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005813 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005814 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005815 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005816 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005817 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005818 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005819 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005820
Al Viro9cdda8d2013-05-22 16:48:09 -04005821 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005822
Chris Mason39279cc2007-06-12 06:35:45 -04005823 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005824 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005825 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005826
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005827 name_len = btrfs_dir_name_len(leaf, di);
5828 if (name_len <= sizeof(tmp_name)) {
5829 name_ptr = tmp_name;
5830 } else {
5831 name_ptr = kmalloc(name_len, GFP_KERNEL);
5832 if (!name_ptr) {
5833 ret = -ENOMEM;
5834 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005835 }
Chris Mason39279cc2007-06-12 06:35:45 -04005836 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005837 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5838 name_len);
5839
5840 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5841 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5842
5843 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5844 d_type);
5845
5846 if (name_ptr != tmp_name)
5847 kfree(name_ptr);
5848
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005849 if (over)
5850 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005851 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005852next:
5853 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005854 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005855
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005856 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005857 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005858 goto nopos;
5859
Zach Browndb62efb2013-07-11 16:19:42 -07005860 /*
5861 * Stop new entries from being returned after we return the last
5862 * entry.
5863 *
5864 * New directory entries are assigned a strictly increasing
5865 * offset. This means that new entries created during readdir
5866 * are *guaranteed* to be seen in the future by that readdir.
5867 * This has broken buggy programs which operate on names as
5868 * they're returned by readdir. Until we re-use freed offsets
5869 * we have this hack to stop new entries from being returned
5870 * under the assumption that they'll never reach this huge
5871 * offset.
5872 *
5873 * This is being careful not to overflow 32bit loff_t unless the
5874 * last entry requires it because doing so has broken 32bit apps
5875 * in the past.
5876 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005877 if (ctx->pos >= INT_MAX)
5878 ctx->pos = LLONG_MAX;
5879 else
5880 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005881nopos:
5882 ret = 0;
5883err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005884 if (put)
5885 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005886 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005887 return ret;
5888}
5889
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005890int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005891{
5892 struct btrfs_root *root = BTRFS_I(inode)->root;
5893 struct btrfs_trans_handle *trans;
5894 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005895 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005896
Josef Bacik72ac3c02012-05-23 14:13:11 -04005897 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005898 return 0;
5899
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005900 if (btrfs_fs_closing(root->fs_info) &&
5901 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005902 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005903
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005904 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005905 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005906 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005907 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005908 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005909 if (IS_ERR(trans))
5910 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005911 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005912 }
5913 return ret;
5914}
5915
5916/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005917 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005918 * inode changes. But, it is most likely to find the inode in cache.
5919 * FIXME, needs more benchmarking...there are no reasons other than performance
5920 * to keep or drop this code.
5921 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005922static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005923{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005925 struct btrfs_root *root = BTRFS_I(inode)->root;
5926 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005927 int ret;
5928
Josef Bacik72ac3c02012-05-23 14:13:11 -04005929 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005930 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005931
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005932 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005933 if (IS_ERR(trans))
5934 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005935
5936 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005937 if (ret && ret == -ENOSPC) {
5938 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005939 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005940 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005941 if (IS_ERR(trans))
5942 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005943
Chris Mason94b60442010-05-26 11:02:00 -04005944 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005945 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005946 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005947 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005948 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005949
5950 return ret;
5951}
5952
5953/*
5954 * This is a copy of file_update_time. We need this so we can return error on
5955 * ENOSPC for updating the inode in the case of file write and mmap writes.
5956 */
Josef Bacike41f9412012-03-26 09:46:47 -04005957static int btrfs_update_time(struct inode *inode, struct timespec *now,
5958 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005959{
Alexander Block2bc5565282012-06-15 09:49:33 +02005960 struct btrfs_root *root = BTRFS_I(inode)->root;
5961
5962 if (btrfs_root_readonly(root))
5963 return -EROFS;
5964
Josef Bacike41f9412012-03-26 09:46:47 -04005965 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005966 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005967 if (flags & S_CTIME)
5968 inode->i_ctime = *now;
5969 if (flags & S_MTIME)
5970 inode->i_mtime = *now;
5971 if (flags & S_ATIME)
5972 inode->i_atime = *now;
5973 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005974}
5975
Chris Masond352ac62008-09-29 15:18:18 -04005976/*
5977 * find the highest existing sequence number in a directory
5978 * and then set the in-memory index_cnt variable to reflect
5979 * free sequence numbers
5980 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005981static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005982{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005983 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005984 struct btrfs_key key, found_key;
5985 struct btrfs_path *path;
5986 struct extent_buffer *leaf;
5987 int ret;
5988
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005989 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005990 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005991 key.offset = (u64)-1;
5992
5993 path = btrfs_alloc_path();
5994 if (!path)
5995 return -ENOMEM;
5996
5997 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5998 if (ret < 0)
5999 goto out;
6000 /* FIXME: we should be able to handle this */
6001 if (ret == 0)
6002 goto out;
6003 ret = 0;
6004
6005 /*
6006 * MAGIC NUMBER EXPLANATION:
6007 * since we search a directory based on f_pos we have to start at 2
6008 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6009 * else has to start at 2
6010 */
6011 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006012 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006013 goto out;
6014 }
6015
6016 path->slots[0]--;
6017
6018 leaf = path->nodes[0];
6019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6020
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006021 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006022 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006023 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006024 goto out;
6025 }
6026
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006027 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006028out:
6029 btrfs_free_path(path);
6030 return ret;
6031}
6032
Chris Masond352ac62008-09-29 15:18:18 -04006033/*
6034 * helper to find a free sequence number in a given directory. This current
6035 * code is very simple, later versions will do smarter things in the btree
6036 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006037int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006038{
6039 int ret = 0;
6040
Nikolay Borisov877574e2017-02-20 13:50:33 +02006041 if (dir->index_cnt == (u64)-1) {
6042 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006043 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006044 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006045 if (ret)
6046 return ret;
6047 }
Josef Bacikaec74772008-07-24 12:12:38 -04006048 }
6049
Nikolay Borisov877574e2017-02-20 13:50:33 +02006050 *index = dir->index_cnt;
6051 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006052
6053 return ret;
6054}
6055
Chris Masonb0d5d102014-09-08 13:08:51 -07006056static int btrfs_insert_inode_locked(struct inode *inode)
6057{
6058 struct btrfs_iget_args args;
6059 args.location = &BTRFS_I(inode)->location;
6060 args.root = BTRFS_I(inode)->root;
6061
6062 return insert_inode_locked4(inode,
6063 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6064 btrfs_find_actor, &args);
6065}
6066
Chris Mason39279cc2007-06-12 06:35:45 -04006067static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6068 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006069 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006070 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006071 u64 ref_objectid, u64 objectid,
6072 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006074 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006075 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006076 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006077 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006078 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006079 struct btrfs_inode_ref *ref;
6080 struct btrfs_key key[2];
6081 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006082 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006083 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006084 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006085
Chris Mason5f39d392007-10-15 16:14:19 -04006086 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006087 if (!path)
6088 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006089
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006090 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006091 if (!inode) {
6092 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006093 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006094 }
Chris Mason39279cc2007-06-12 06:35:45 -04006095
Li Zefan581bb052011-04-20 10:06:11 +08006096 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006097 * O_TMPFILE, set link count to 0, so that after this point,
6098 * we fill in an inode item with the correct link count.
6099 */
6100 if (!name)
6101 set_nlink(inode, 0);
6102
6103 /*
Li Zefan581bb052011-04-20 10:06:11 +08006104 * we have to initialize this early, so we can reclaim the inode
6105 * number if we fail afterwards in this function.
6106 */
6107 inode->i_ino = objectid;
6108
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006109 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006110 trace_btrfs_inode_request(dir);
6111
Nikolay Borisov877574e2017-02-20 13:50:33 +02006112 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006113 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006114 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006115 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006116 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006117 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006118 } else if (dir) {
6119 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006120 }
6121 /*
6122 * index_cnt is ignored for everything but a dir,
6123 * btrfs_get_inode_index_count has an explanation for the magic
6124 * number
6125 */
6126 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006127 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006128 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006129 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006130 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006131
Josef Bacik5dc562c2012-08-17 13:14:17 -04006132 /*
6133 * We could have gotten an inode number from somebody who was fsynced
6134 * and then removed in this same transaction, so let's just set full
6135 * sync since it will be a full sync anyway and this will blow away the
6136 * old info in the log.
6137 */
6138 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6139
Chris Mason9c583092008-01-29 15:15:18 -05006140 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006141 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006142 key[0].offset = 0;
6143
Chris Mason9c583092008-01-29 15:15:18 -05006144 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006145
6146 if (name) {
6147 /*
6148 * Start new inodes with an inode_ref. This is slightly more
6149 * efficient for small numbers of hard links since they will
6150 * be packed into one item. Extended refs will kick in if we
6151 * add more hard links than can fit in the ref item.
6152 */
6153 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006154 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006155 key[1].offset = ref_objectid;
6156
6157 sizes[1] = name_len + sizeof(*ref);
6158 }
Chris Mason9c583092008-01-29 15:15:18 -05006159
Chris Masonb0d5d102014-09-08 13:08:51 -07006160 location = &BTRFS_I(inode)->location;
6161 location->objectid = objectid;
6162 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006163 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006164
6165 ret = btrfs_insert_inode_locked(inode);
6166 if (ret < 0)
6167 goto fail;
6168
Chris Masonb9473432009-03-13 11:00:37 -04006169 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006170 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006171 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006172 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006173
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006174 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006175 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306176
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006177 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306178 inode->i_atime = inode->i_mtime;
6179 inode->i_ctime = inode->i_mtime;
6180 BTRFS_I(inode)->i_otime = inode->i_mtime;
6181
Chris Mason5f39d392007-10-15 16:14:19 -04006182 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6183 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006184 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006185 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006186 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006187
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006188 if (name) {
6189 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6190 struct btrfs_inode_ref);
6191 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6192 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6193 ptr = (unsigned long)(ref + 1);
6194 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6195 }
Chris Mason9c583092008-01-29 15:15:18 -05006196
Chris Mason5f39d392007-10-15 16:14:19 -04006197 btrfs_mark_buffer_dirty(path->nodes[0]);
6198 btrfs_free_path(path);
6199
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006200 btrfs_inherit_iflags(inode, dir);
6201
Al Viro569254b2011-07-24 17:08:40 -04006202 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006203 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006204 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006205 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006206 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6207 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006208 }
6209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006210 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006211
6212 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006213 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006214
Alexander Block8ea05e32012-07-25 17:35:53 +02006215 btrfs_update_root_times(trans, root);
6216
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006217 ret = btrfs_inode_inherit_props(trans, inode, dir);
6218 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006219 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006220 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006221 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006222
Chris Mason39279cc2007-06-12 06:35:45 -04006223 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006224
6225fail_unlock:
6226 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006227fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006228 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006229 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006230 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006231 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006232 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006233}
6234
6235static inline u8 btrfs_inode_type(struct inode *inode)
6236{
6237 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6238}
6239
Chris Masond352ac62008-09-29 15:18:18 -04006240/*
6241 * utility function to add 'inode' into 'parent_inode' with
6242 * a give name and a given sequence number.
6243 * if 'add_backref' is true, also insert a backref from the
6244 * inode to the parent directory.
6245 */
Chris Masone02119d2008-09-05 16:13:11 -04006246int btrfs_add_link(struct btrfs_trans_handle *trans,
6247 struct inode *parent_inode, struct inode *inode,
6248 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006249{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006250 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006251 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006252 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006253 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006254 u64 ino = btrfs_ino(BTRFS_I(inode));
6255 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006256
Li Zefan33345d012011-04-20 10:31:50 +08006257 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6259 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006260 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006261 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006262 key.offset = 0;
6263 }
Chris Mason39279cc2007-06-12 06:35:45 -04006264
Li Zefan33345d012011-04-20 10:31:50 +08006265 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006266 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6267 root->root_key.objectid, parent_ino,
6268 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006269 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006270 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6271 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006272 }
6273
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006274 /* Nothing to clean up yet */
6275 if (ret)
6276 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006277
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +02006279 BTRFS_I(parent_inode), &key,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006281 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006282 goto fail_dir_item;
6283 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006284 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006285 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006288 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006290 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006291 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006292 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006293 ret = btrfs_update_inode(trans, root, parent_inode);
6294 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006295 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006296 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006297
6298fail_dir_item:
6299 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6300 u64 local_index;
6301 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006302 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6303 root->root_key.objectid, parent_ino,
6304 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006305
6306 } else if (add_backref) {
6307 u64 local_index;
6308 int err;
6309
6310 err = btrfs_del_inode_ref(trans, root, name, name_len,
6311 ino, parent_ino, &local_index);
6312 }
6313 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006314}
6315
6316static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006317 struct inode *dir, struct dentry *dentry,
6318 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006319{
Josef Bacika1b075d2010-11-19 20:36:11 +00006320 int err = btrfs_add_link(trans, dir, inode,
6321 dentry->d_name.name, dentry->d_name.len,
6322 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006323 if (err > 0)
6324 err = -EEXIST;
6325 return err;
6326}
6327
Josef Bacik618e21d2007-07-11 10:18:17 -04006328static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006329 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006330{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006331 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006332 struct btrfs_trans_handle *trans;
6333 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006334 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006335 int err;
6336 int drop_inode = 0;
6337 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006338 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006339
Josef Bacik9ed74f22009-09-11 16:12:44 -04006340 /*
6341 * 2 for inode item and ref
6342 * 2 for dir items
6343 * 1 for xattr if selinux is on
6344 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006345 trans = btrfs_start_transaction(root, 5);
6346 if (IS_ERR(trans))
6347 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006348
Li Zefan581bb052011-04-20 10:06:11 +08006349 err = btrfs_find_free_ino(root, &objectid);
6350 if (err)
6351 goto out_unlock;
6352
Josef Bacikaec74772008-07-24 12:12:38 -04006353 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006354 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6355 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006356 if (IS_ERR(inode)) {
6357 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006358 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006359 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006360
Casey Schauflerad19db72011-12-15 10:09:07 -05006361 /*
6362 * If the active LSM wants to access the inode during
6363 * d_instantiate it needs these. Smack checks to see
6364 * if the filesystem supports xattrs by looking at the
6365 * ops vector.
6366 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006367 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006368 init_special_inode(inode, inode->i_mode, rdev);
6369
6370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006371 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006372 goto out_unlock_inode;
6373
6374 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6375 if (err) {
6376 goto out_unlock_inode;
6377 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006378 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006379 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006380 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006381 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006382
Josef Bacik618e21d2007-07-11 10:18:17 -04006383out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006384 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006385 btrfs_balance_delayed_items(fs_info);
6386 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006387 if (drop_inode) {
6388 inode_dec_link_count(inode);
6389 iput(inode);
6390 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006391 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006392
6393out_unlock_inode:
6394 drop_inode = 1;
6395 unlock_new_inode(inode);
6396 goto out_unlock;
6397
Josef Bacik618e21d2007-07-11 10:18:17 -04006398}
6399
Chris Mason39279cc2007-06-12 06:35:45 -04006400static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006401 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006402{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006404 struct btrfs_trans_handle *trans;
6405 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006406 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006407 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006408 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006409 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006410 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006411
Josef Bacik9ed74f22009-09-11 16:12:44 -04006412 /*
6413 * 2 for inode item and ref
6414 * 2 for dir items
6415 * 1 for xattr if selinux is on
6416 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006417 trans = btrfs_start_transaction(root, 5);
6418 if (IS_ERR(trans))
6419 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006420
Li Zefan581bb052011-04-20 10:06:11 +08006421 err = btrfs_find_free_ino(root, &objectid);
6422 if (err)
6423 goto out_unlock;
6424
Josef Bacikaec74772008-07-24 12:12:38 -04006425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006426 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6427 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006428 if (IS_ERR(inode)) {
6429 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006430 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006431 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006432 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006433 /*
6434 * If the active LSM wants to access the inode during
6435 * d_instantiate it needs these. Smack checks to see
6436 * if the filesystem supports xattrs by looking at the
6437 * ops vector.
6438 */
6439 inode->i_fop = &btrfs_file_operations;
6440 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006441 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006442
6443 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6444 if (err)
6445 goto out_unlock_inode;
6446
6447 err = btrfs_update_inode(trans, root, inode);
6448 if (err)
6449 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006450
Josef Bacika1b075d2010-11-19 20:36:11 +00006451 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006452 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006453 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006454
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006455 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006456 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 d_instantiate(dentry, inode);
6458
Chris Mason39279cc2007-06-12 06:35:45 -04006459out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006460 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006461 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006462 inode_dec_link_count(inode);
6463 iput(inode);
6464 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006465 btrfs_balance_delayed_items(fs_info);
6466 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006467 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006468
6469out_unlock_inode:
6470 unlock_new_inode(inode);
6471 goto out_unlock;
6472
Chris Mason39279cc2007-06-12 06:35:45 -04006473}
6474
6475static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6476 struct dentry *dentry)
6477{
Filipe Manana271dba452016-01-05 16:24:05 +00006478 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006479 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006480 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006482 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006483 int err;
6484 int drop_inode = 0;
6485
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006486 /* do not allow sys_link's with other subvols of the same device */
6487 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006488 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006489
Mark Fashehf1863732012-08-08 11:32:27 -07006490 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006491 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006492
Nikolay Borisov877574e2017-02-20 13:50:33 +02006493 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006494 if (err)
6495 goto fail;
6496
Yan, Zhenga22285a2010-05-16 10:48:46 -04006497 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006498 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006500 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006501 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006502 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006503 if (IS_ERR(trans)) {
6504 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006505 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006506 goto fail;
6507 }
Chris Mason5f39d392007-10-15 16:14:19 -04006508
Miao Xie67de1172013-12-26 13:07:06 +08006509 /* There are several dir indexes for this inode, clear the cache. */
6510 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006511 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006512 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006513 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006514 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006515 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006516
Josef Bacika1b075d2010-11-19 20:36:11 +00006517 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006518
Yan, Zhenga5719522009-09-24 09:17:31 -04006519 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006520 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006521 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006522 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006523 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006524 if (err)
6525 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006526 if (inode->i_nlink == 1) {
6527 /*
6528 * If new hard link count is 1, it's a file created
6529 * with open(2) O_TMPFILE flag.
6530 */
6531 err = btrfs_orphan_del(trans, inode);
6532 if (err)
6533 goto fail;
6534 }
Al Viro08c422c2011-12-23 07:58:13 -05006535 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006536 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006537 }
Chris Mason39279cc2007-06-12 06:35:45 -04006538
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006539 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006540fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006541 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006542 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006543 if (drop_inode) {
6544 inode_dec_link_count(inode);
6545 iput(inode);
6546 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006548 return err;
6549}
6550
Al Viro18bb1db2011-07-26 01:41:39 -04006551static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006552{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006553 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006554 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006555 struct btrfs_trans_handle *trans;
6556 struct btrfs_root *root = BTRFS_I(dir)->root;
6557 int err = 0;
6558 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006559 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006560 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006561
Josef Bacik9ed74f22009-09-11 16:12:44 -04006562 /*
6563 * 2 items for inode and ref
6564 * 2 items for dir items
6565 * 1 for xattr if selinux is on
6566 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006567 trans = btrfs_start_transaction(root, 5);
6568 if (IS_ERR(trans))
6569 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006570
Li Zefan581bb052011-04-20 10:06:11 +08006571 err = btrfs_find_free_ino(root, &objectid);
6572 if (err)
6573 goto out_fail;
6574
Josef Bacikaec74772008-07-24 12:12:38 -04006575 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006576 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6577 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006578 if (IS_ERR(inode)) {
6579 err = PTR_ERR(inode);
6580 goto out_fail;
6581 }
Chris Mason5f39d392007-10-15 16:14:19 -04006582
Chris Mason39279cc2007-06-12 06:35:45 -04006583 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 /* these must be set before we unlock the inode */
6585 inode->i_op = &btrfs_dir_inode_operations;
6586 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006587
Eric Paris2a7dba32011-02-01 11:05:39 -05006588 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006589 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006590 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006591
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006592 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006593 err = btrfs_update_inode(trans, root, inode);
6594 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006596
Josef Bacika1b075d2010-11-19 20:36:11 +00006597 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6598 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006600 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006601
Chris Mason39279cc2007-06-12 06:35:45 -04006602 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006603 /*
6604 * mkdir is special. We're unlocking after we call d_instantiate
6605 * to avoid a race with nfsd calling d_instantiate.
6606 */
6607 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006608 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006609
6610out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006611 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006612 if (drop_on_err) {
6613 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006614 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006615 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006616 btrfs_balance_delayed_items(fs_info);
6617 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006618 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006619
6620out_fail_inode:
6621 unlock_new_inode(inode);
6622 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006623}
6624
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006625/* Find next extent map of a given extent map, caller needs to ensure locks */
6626static struct extent_map *next_extent_map(struct extent_map *em)
6627{
6628 struct rb_node *next;
6629
6630 next = rb_next(&em->rb_node);
6631 if (!next)
6632 return NULL;
6633 return container_of(next, struct extent_map, rb_node);
6634}
6635
6636static struct extent_map *prev_extent_map(struct extent_map *em)
6637{
6638 struct rb_node *prev;
6639
6640 prev = rb_prev(&em->rb_node);
6641 if (!prev)
6642 return NULL;
6643 return container_of(prev, struct extent_map, rb_node);
6644}
6645
Chris Masond352ac62008-09-29 15:18:18 -04006646/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006647 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006648 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006649 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006650 */
Chris Mason3b951512008-04-17 11:29:12 -04006651static int merge_extent_mapping(struct extent_map_tree *em_tree,
6652 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006653 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006654 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006655{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006656 struct extent_map *prev;
6657 struct extent_map *next;
6658 u64 start;
6659 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006660 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006661
Chris Masone6dcd2d2008-07-17 12:53:50 -04006662 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006663
6664 if (existing->start > map_start) {
6665 next = existing;
6666 prev = prev_extent_map(next);
6667 } else {
6668 prev = existing;
6669 next = next_extent_map(prev);
6670 }
6671
6672 start = prev ? extent_map_end(prev) : em->start;
6673 start = max_t(u64, start, em->start);
6674 end = next ? next->start : extent_map_end(em);
6675 end = min_t(u64, end, extent_map_end(em));
6676 start_diff = start - em->start;
6677 em->start = start;
6678 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006679 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6680 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006681 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006682 em->block_len -= start_diff;
6683 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006684 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006685}
6686
Chris Masonc8b97812008-10-29 14:49:59 -04006687static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006688 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006689 size_t pg_offset, u64 extent_offset,
6690 struct btrfs_file_extent_item *item)
6691{
6692 int ret;
6693 struct extent_buffer *leaf = path->nodes[0];
6694 char *tmp;
6695 size_t max_size;
6696 unsigned long inline_size;
6697 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006698 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006699
6700 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006701 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006702 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6703 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006704 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006705 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006706 if (!tmp)
6707 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006708 ptr = btrfs_file_extent_inline_start(item);
6709
6710 read_extent_buffer(leaf, tmp, ptr, inline_size);
6711
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006712 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006713 ret = btrfs_decompress(compress_type, tmp, page,
6714 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006715 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006716 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006717}
6718
Chris Masond352ac62008-09-29 15:18:18 -04006719/*
6720 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006721 * the ugly parts come from merging extents from the disk with the in-ram
6722 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006723 * where the in-ram extents might be locked pending data=ordered completion.
6724 *
6725 * This also copies inline extents directly into the page.
6726 */
Chris Masond3977122009-01-05 21:25:51 -05006727
Chris Masona52d9a82007-08-27 16:49:44 -04006728struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006729 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006730 int create)
6731{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006733 int ret;
6734 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006735 u64 extent_start = 0;
6736 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006737 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006738 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006739 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006740 struct btrfs_root *root = BTRFS_I(inode)->root;
6741 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006742 struct extent_buffer *leaf;
6743 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006744 struct extent_map *em = NULL;
6745 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006746 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006747 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006748 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006749
Chris Masona52d9a82007-08-27 16:49:44 -04006750again:
Chris Mason890871b2009-09-02 16:24:52 -04006751 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006752 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006753 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006754 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006755 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006756
Chris Masona52d9a82007-08-27 16:49:44 -04006757 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006758 if (em->start > start || em->start + em->len <= start)
6759 free_extent_map(em);
6760 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006761 free_extent_map(em);
6762 else
6763 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006764 }
David Sterba172ddd62011-04-21 00:48:27 +02006765 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006766 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006767 err = -ENOMEM;
6768 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006769 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006770 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006771 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006772 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006773 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006774 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006775
6776 if (!path) {
6777 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006778 if (!path) {
6779 err = -ENOMEM;
6780 goto out;
6781 }
6782 /*
6783 * Chances are we'll be called again, so go ahead and do
6784 * readahead
6785 */
David Sterbae4058b52015-11-27 16:31:35 +01006786 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006787 }
6788
Chris Mason179e29e2007-11-01 11:28:41 -04006789 ret = btrfs_lookup_file_extent(trans, root, path,
6790 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006791 if (ret < 0) {
6792 err = ret;
6793 goto out;
6794 }
6795
6796 if (ret != 0) {
6797 if (path->slots[0] == 0)
6798 goto not_found;
6799 path->slots[0]--;
6800 }
6801
Chris Mason5f39d392007-10-15 16:14:19 -04006802 leaf = path->nodes[0];
6803 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006804 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006805 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006806 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006807 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006808 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006809 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006810 /*
6811 * If we backup past the first extent we want to move forward
6812 * and see if there is an extent in front of us, otherwise we'll
6813 * say there is a hole for our whole search range which can
6814 * cause problems.
6815 */
6816 extent_end = start;
6817 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006818 }
6819
Chris Mason5f39d392007-10-15 16:14:19 -04006820 found_type = btrfs_file_extent_type(leaf, item);
6821 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006822 if (found_type == BTRFS_FILE_EXTENT_REG ||
6823 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006824 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006825 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006826 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6827 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006828 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006829 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006830 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006831 }
Josef Bacik25a50342013-10-14 12:08:38 -04006832next:
Yan Zheng9036c102008-10-30 14:19:41 -04006833 if (start >= extent_end) {
6834 path->slots[0]++;
6835 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6836 ret = btrfs_next_leaf(root, path);
6837 if (ret < 0) {
6838 err = ret;
6839 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006840 }
Yan Zheng9036c102008-10-30 14:19:41 -04006841 if (ret > 0)
6842 goto not_found;
6843 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006844 }
Yan Zheng9036c102008-10-30 14:19:41 -04006845 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6846 if (found_key.objectid != objectid ||
6847 found_key.type != BTRFS_EXTENT_DATA_KEY)
6848 goto not_found;
6849 if (start + len <= found_key.offset)
6850 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006851 if (start > found_key.offset)
6852 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006853 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006854 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006855 em->len = found_key.offset - start;
6856 goto not_found_em;
6857 }
6858
Filipe Manana7ffbb592014-06-09 03:48:05 +01006859 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6860
Yan Zhengd899e052008-10-30 14:25:28 -04006861 if (found_type == BTRFS_FILE_EXTENT_REG ||
6862 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006863 goto insert;
6864 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006865 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006866 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006867 size_t size;
6868 size_t extent_offset;
6869 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006870
Filipe Manana7ffbb592014-06-09 03:48:05 +01006871 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006872 goto out;
Yan689f9342007-10-29 11:41:07 -04006873
Chris Mason514ac8a2014-01-03 21:07:00 -08006874 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006875 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006876 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6877 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006878 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006879 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006880 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006881 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006882 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006883 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006884 if (btrfs_file_extent_compression(leaf, item) !=
6885 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006886 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006887 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006888 if (ret) {
6889 err = ret;
6890 goto out;
6891 }
Chris Masonc8b97812008-10-29 14:49:59 -04006892 } else {
6893 map = kmap(page);
6894 read_extent_buffer(leaf, map + pg_offset, ptr,
6895 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006896 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006897 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006898 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006899 copy_size);
6900 }
Chris Masonc8b97812008-10-29 14:49:59 -04006901 kunmap(page);
6902 }
Chris Mason179e29e2007-11-01 11:28:41 -04006903 flush_dcache_page(page);
6904 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006905 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006906 if (!trans) {
6907 kunmap(page);
6908 free_extent_map(em);
6909 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006910
David Sterbab3b4aa72011-04-21 01:20:15 +02006911 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006912 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006913
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006914 if (IS_ERR(trans))
6915 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006916 goto again;
6917 }
Chris Masonc8b97812008-10-29 14:49:59 -04006918 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006919 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006920 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006921 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006922 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006923 }
Chris Masond1310b22008-01-24 16:13:08 -05006924 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006925 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006926 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 }
6928not_found:
6929 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006930 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006931 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006932not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006933 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006934 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006935insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006936 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006937 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006938 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006939 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6940 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006941 err = -EIO;
6942 goto out;
6943 }
Chris Masond1310b22008-01-24 16:13:08 -05006944
6945 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006946 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006947 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006948 /* it is possible that someone inserted the extent into the tree
6949 * while we had the lock dropped. It is also possible that
6950 * an overlapping map exists in the tree
6951 */
Chris Masona52d9a82007-08-27 16:49:44 -04006952 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006953 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006954
6955 ret = 0;
6956
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006957 existing = search_extent_mapping(em_tree, start, len);
6958 /*
6959 * existing will always be non-NULL, since there must be
6960 * extent causing the -EEXIST.
6961 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006962 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006963 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006964 em->block_start == existing->block_start) {
6965 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006966 * The existing extent map already encompasses the
6967 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006968 */
6969 free_extent_map(em);
6970 em = existing;
6971 err = 0;
6972
6973 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006974 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006975 /*
6976 * The existing extent map is the one nearest to
6977 * the [start, start + len) range which overlaps
6978 */
6979 err = merge_extent_mapping(em_tree, existing,
6980 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006981 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006982 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006983 free_extent_map(em);
6984 em = NULL;
6985 }
6986 } else {
6987 free_extent_map(em);
6988 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006989 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Chris Mason890871b2009-09-02 16:24:52 -04006992 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006993out:
liubo1abe9b82011-03-24 11:18:59 +00006994
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006995 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00006996
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006997 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006998 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006999 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007000 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007001 err = ret;
7002 }
Chris Masona52d9a82007-08-27 16:49:44 -04007003 if (err) {
7004 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007005 return ERR_PTR(err);
7006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007007 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007008 return em;
7009}
7010
Chris Masonec29ed52011-02-23 16:23:20 -05007011struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7012 size_t pg_offset, u64 start, u64 len,
7013 int create)
7014{
7015 struct extent_map *em;
7016 struct extent_map *hole_em = NULL;
7017 u64 range_start = start;
7018 u64 end;
7019 u64 found;
7020 u64 found_end;
7021 int err = 0;
7022
7023 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7024 if (IS_ERR(em))
7025 return em;
7026 if (em) {
7027 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007028 * if our em maps to
7029 * - a hole or
7030 * - a pre-alloc extent,
7031 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007032 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007033 if (em->block_start != EXTENT_MAP_HOLE &&
7034 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007035 return em;
7036 else
7037 hole_em = em;
7038 }
7039
7040 /* check to see if we've wrapped (len == -1 or similar) */
7041 end = start + len;
7042 if (end < start)
7043 end = (u64)-1;
7044 else
7045 end -= 1;
7046
7047 em = NULL;
7048
7049 /* ok, we didn't find anything, lets look for delalloc */
7050 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7051 end, len, EXTENT_DELALLOC, 1);
7052 found_end = range_start + found;
7053 if (found_end < range_start)
7054 found_end = (u64)-1;
7055
7056 /*
7057 * we didn't find anything useful, return
7058 * the original results from get_extent()
7059 */
7060 if (range_start > end || found_end <= start) {
7061 em = hole_em;
7062 hole_em = NULL;
7063 goto out;
7064 }
7065
7066 /* adjust the range_start to make sure it doesn't
7067 * go backwards from the start they passed in
7068 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307069 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007070 found = found_end - range_start;
7071
7072 if (found > 0) {
7073 u64 hole_start = start;
7074 u64 hole_len = len;
7075
David Sterba172ddd62011-04-21 00:48:27 +02007076 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007077 if (!em) {
7078 err = -ENOMEM;
7079 goto out;
7080 }
7081 /*
7082 * when btrfs_get_extent can't find anything it
7083 * returns one huge hole
7084 *
7085 * make sure what it found really fits our range, and
7086 * adjust to make sure it is based on the start from
7087 * the caller
7088 */
7089 if (hole_em) {
7090 u64 calc_end = extent_map_end(hole_em);
7091
7092 if (calc_end <= start || (hole_em->start > end)) {
7093 free_extent_map(hole_em);
7094 hole_em = NULL;
7095 } else {
7096 hole_start = max(hole_em->start, start);
7097 hole_len = calc_end - hole_start;
7098 }
7099 }
7100 em->bdev = NULL;
7101 if (hole_em && range_start > hole_start) {
7102 /* our hole starts before our delalloc, so we
7103 * have to return just the parts of the hole
7104 * that go until the delalloc starts
7105 */
7106 em->len = min(hole_len,
7107 range_start - hole_start);
7108 em->start = hole_start;
7109 em->orig_start = hole_start;
7110 /*
7111 * don't adjust block start at all,
7112 * it is fixed at EXTENT_MAP_HOLE
7113 */
7114 em->block_start = hole_em->block_start;
7115 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007116 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7117 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007118 } else {
7119 em->start = range_start;
7120 em->len = found;
7121 em->orig_start = range_start;
7122 em->block_start = EXTENT_MAP_DELALLOC;
7123 em->block_len = found;
7124 }
7125 } else if (hole_em) {
7126 return hole_em;
7127 }
7128out:
7129
7130 free_extent_map(hole_em);
7131 if (err) {
7132 free_extent_map(em);
7133 return ERR_PTR(err);
7134 }
7135 return em;
7136}
7137
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007138static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7139 const u64 start,
7140 const u64 len,
7141 const u64 orig_start,
7142 const u64 block_start,
7143 const u64 block_len,
7144 const u64 orig_block_len,
7145 const u64 ram_bytes,
7146 const int type)
7147{
7148 struct extent_map *em = NULL;
7149 int ret;
7150
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007151 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007152 em = create_io_em(inode, start, len, orig_start,
7153 block_start, block_len, orig_block_len,
7154 ram_bytes,
7155 BTRFS_COMPRESS_NONE, /* compress_type */
7156 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007157 if (IS_ERR(em))
7158 goto out;
7159 }
7160 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7161 len, block_len, type);
7162 if (ret) {
7163 if (em) {
7164 free_extent_map(em);
7165 btrfs_drop_extent_cache(inode, start,
7166 start + len - 1, 0);
7167 }
7168 em = ERR_PTR(ret);
7169 }
7170 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007171
7172 return em;
7173}
7174
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7176 u64 start, u64 len)
7177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007179 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007180 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181 struct btrfs_key ins;
7182 u64 alloc_hint;
7183 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007184
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007186 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007187 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007188 if (ret)
7189 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007191 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7192 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007193 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007195 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007196 btrfs_free_reserved_extent(fs_info, ins.objectid,
7197 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007198
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199 return em;
7200}
7201
Chris Mason46bfbb52010-05-26 11:04:10 -04007202/*
7203 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7204 * block must be cow'd
7205 */
Josef Bacik00361582013-08-14 14:02:47 -04007206noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007207 u64 *orig_start, u64 *orig_block_len,
7208 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007209{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007211 struct btrfs_path *path;
7212 int ret;
7213 struct extent_buffer *leaf;
7214 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007215 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007216 struct btrfs_file_extent_item *fi;
7217 struct btrfs_key key;
7218 u64 disk_bytenr;
7219 u64 backref_offset;
7220 u64 extent_end;
7221 u64 num_bytes;
7222 int slot;
7223 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007224 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007225
Chris Mason46bfbb52010-05-26 11:04:10 -04007226 path = btrfs_alloc_path();
7227 if (!path)
7228 return -ENOMEM;
7229
David Sterbaf85b7372017-01-20 14:54:07 +01007230 ret = btrfs_lookup_file_extent(NULL, root, path,
7231 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007232 if (ret < 0)
7233 goto out;
7234
7235 slot = path->slots[0];
7236 if (ret == 1) {
7237 if (slot == 0) {
7238 /* can't find the item, must cow */
7239 ret = 0;
7240 goto out;
7241 }
7242 slot--;
7243 }
7244 ret = 0;
7245 leaf = path->nodes[0];
7246 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007247 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007248 key.type != BTRFS_EXTENT_DATA_KEY) {
7249 /* not our file or wrong item type, must cow */
7250 goto out;
7251 }
7252
7253 if (key.offset > offset) {
7254 /* Wrong offset, must cow */
7255 goto out;
7256 }
7257
7258 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7259 found_type = btrfs_file_extent_type(leaf, fi);
7260 if (found_type != BTRFS_FILE_EXTENT_REG &&
7261 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7262 /* not a regular extent, must cow */
7263 goto out;
7264 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007265
7266 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7267 goto out;
7268
Miao Xiee77751a2013-12-27 21:11:50 +08007269 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7270 if (extent_end <= offset)
7271 goto out;
7272
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007274 if (disk_bytenr == 0)
7275 goto out;
7276
7277 if (btrfs_file_extent_compression(leaf, fi) ||
7278 btrfs_file_extent_encryption(leaf, fi) ||
7279 btrfs_file_extent_other_encoding(leaf, fi))
7280 goto out;
7281
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 backref_offset = btrfs_file_extent_offset(leaf, fi);
7283
Josef Bacik7ee9e442013-06-21 16:37:03 -04007284 if (orig_start) {
7285 *orig_start = key.offset - backref_offset;
7286 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7287 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7288 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007289
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007290 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007291 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007292
7293 num_bytes = min(offset + *len, extent_end) - offset;
7294 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7295 u64 range_end;
7296
Jeff Mahoneyda170662016-06-15 09:22:56 -04007297 range_end = round_up(offset + num_bytes,
7298 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007299 ret = test_range_bit(io_tree, offset, range_end,
7300 EXTENT_DELALLOC, 0, NULL);
7301 if (ret) {
7302 ret = -EAGAIN;
7303 goto out;
7304 }
7305 }
7306
Josef Bacik1bda19e2013-10-18 12:10:36 -04007307 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007308
7309 /*
7310 * look for other files referencing this extent, if we
7311 * find any we must cow
7312 */
Josef Bacik00361582013-08-14 14:02:47 -04007313
Liu Boe4c3b2d2017-01-30 12:25:28 -08007314 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007315 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007316 if (ret) {
7317 ret = 0;
7318 goto out;
7319 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007320
7321 /*
7322 * adjust disk_bytenr and num_bytes to cover just the bytes
7323 * in this extent we are about to write. If there
7324 * are any csums in that range we have to cow in order
7325 * to keep the csums correct
7326 */
7327 disk_bytenr += backref_offset;
7328 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007329 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7330 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007331 /*
7332 * all of the above have passed, it is safe to overwrite this extent
7333 * without cow
7334 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007335 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007336 ret = 1;
7337out:
7338 btrfs_free_path(path);
7339 return ret;
7340}
7341
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007342bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7343{
7344 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7345 int found = false;
7346 void **pagep = NULL;
7347 struct page *page = NULL;
7348 int start_idx;
7349 int end_idx;
7350
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007351 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007352
7353 /*
7354 * end is the last byte in the last page. end == start is legal
7355 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007356 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007357
7358 rcu_read_lock();
7359
7360 /* Most of the code in this while loop is lifted from
7361 * find_get_page. It's been modified to begin searching from a
7362 * page and return just the first page found in that range. If the
7363 * found idx is less than or equal to the end idx then we know that
7364 * a page exists. If no pages are found or if those pages are
7365 * outside of the range then we're fine (yay!) */
7366 while (page == NULL &&
7367 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7368 page = radix_tree_deref_slot(pagep);
7369 if (unlikely(!page))
7370 break;
7371
7372 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007373 if (radix_tree_deref_retry(page)) {
7374 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007375 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007376 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007377 /*
7378 * Otherwise, shmem/tmpfs must be storing a swap entry
7379 * here as an exceptional entry: so return it without
7380 * attempting to raise page count.
7381 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007382 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007383 break; /* TODO: Is this relevant for this use case? */
7384 }
7385
Filipe Manana91405152014-06-05 13:22:24 +01007386 if (!page_cache_get_speculative(page)) {
7387 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007388 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007389 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007390
7391 /*
7392 * Has the page moved?
7393 * This is part of the lockless pagecache protocol. See
7394 * include/linux/pagemap.h for details.
7395 */
7396 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007397 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007398 page = NULL;
7399 }
7400 }
7401
7402 if (page) {
7403 if (page->index <= end_idx)
7404 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007405 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007406 }
7407
7408 rcu_read_unlock();
7409 return found;
7410}
7411
Josef Bacikeb838e72012-07-31 16:28:48 -04007412static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7413 struct extent_state **cached_state, int writing)
7414{
7415 struct btrfs_ordered_extent *ordered;
7416 int ret = 0;
7417
7418 while (1) {
7419 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007420 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007421 /*
7422 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007423 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 * extents in this range.
7425 */
7426 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7427 lockend - lockstart + 1);
7428
7429 /*
7430 * We need to make sure there are no buffered pages in this
7431 * range either, we could have raced between the invalidate in
7432 * generic_file_direct_write and locking the extent. The
7433 * invalidate needs to happen so that reads after a write do not
7434 * get stale data.
7435 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007436 if (!ordered &&
7437 (!writing ||
7438 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007439 break;
7440
7441 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7442 cached_state, GFP_NOFS);
7443
7444 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007445 /*
7446 * If we are doing a DIO read and the ordered extent we
7447 * found is for a buffered write, we can not wait for it
7448 * to complete and retry, because if we do so we can
7449 * deadlock with concurrent buffered writes on page
7450 * locks. This happens only if our DIO read covers more
7451 * than one extent map, if at this point has already
7452 * created an ordered extent for a previous extent map
7453 * and locked its range in the inode's io tree, and a
7454 * concurrent write against that previous extent map's
7455 * range and this range started (we unlock the ranges
7456 * in the io tree only when the bios complete and
7457 * buffered writes always lock pages before attempting
7458 * to lock range in the io tree).
7459 */
7460 if (writing ||
7461 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7462 btrfs_start_ordered_extent(inode, ordered, 1);
7463 else
7464 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007465 btrfs_put_ordered_extent(ordered);
7466 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007467 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007468 * We could trigger writeback for this range (and wait
7469 * for it to complete) and then invalidate the pages for
7470 * this range (through invalidate_inode_pages2_range()),
7471 * but that can lead us to a deadlock with a concurrent
7472 * call to readpages() (a buffered read or a defrag call
7473 * triggered a readahead) on a page lock due to an
7474 * ordered dio extent we created before but did not have
7475 * yet a corresponding bio submitted (whence it can not
7476 * complete), which makes readpages() wait for that
7477 * ordered extent to complete while holding a lock on
7478 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007479 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007480 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 }
7482
Filipe Mananaade77022016-02-18 14:28:55 +00007483 if (ret)
7484 break;
7485
Josef Bacikeb838e72012-07-31 16:28:48 -04007486 cond_resched();
7487 }
7488
7489 return ret;
7490}
7491
Liu Bo6f9994d2017-01-31 07:50:22 -08007492/* The callers of this must take lock_extent() */
7493static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7494 u64 orig_start, u64 block_start,
7495 u64 block_len, u64 orig_block_len,
7496 u64 ram_bytes, int compress_type,
7497 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007498{
7499 struct extent_map_tree *em_tree;
7500 struct extent_map *em;
7501 struct btrfs_root *root = BTRFS_I(inode)->root;
7502 int ret;
7503
Liu Bo6f9994d2017-01-31 07:50:22 -08007504 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7505 type == BTRFS_ORDERED_COMPRESSED ||
7506 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007507 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007508
Josef Bacik69ffb542012-09-11 15:40:07 -04007509 em_tree = &BTRFS_I(inode)->extent_tree;
7510 em = alloc_extent_map();
7511 if (!em)
7512 return ERR_PTR(-ENOMEM);
7513
7514 em->start = start;
7515 em->orig_start = orig_start;
7516 em->len = len;
7517 em->block_len = block_len;
7518 em->block_start = block_start;
7519 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007520 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007521 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007522 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007523 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007524 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007525 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007526 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007527 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7528 em->compress_type = compress_type;
7529 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007530
7531 do {
7532 btrfs_drop_extent_cache(inode, em->start,
7533 em->start + em->len - 1, 0);
7534 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007535 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007536 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007537 /*
7538 * The caller has taken lock_extent(), who could race with us
7539 * to add em?
7540 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007541 } while (ret == -EEXIST);
7542
7543 if (ret) {
7544 free_extent_map(em);
7545 return ERR_PTR(ret);
7546 }
7547
Liu Bo6f9994d2017-01-31 07:50:22 -08007548 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007549 return em;
7550}
7551
Filipe Manana9c9464c2015-11-04 09:52:04 +00007552static void adjust_dio_outstanding_extents(struct inode *inode,
7553 struct btrfs_dio_data *dio_data,
7554 const u64 len)
7555{
David Sterba823bb202017-01-04 11:09:51 +01007556 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007557
Filipe Manana9c9464c2015-11-04 09:52:04 +00007558 /*
7559 * If we have an outstanding_extents count still set then we're
7560 * within our reservation, otherwise we need to adjust our inode
7561 * counter appropriately.
7562 */
Liu Boc2931662016-12-22 17:13:54 -08007563 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007564 dio_data->outstanding_extents -= num_extents;
7565 } else {
Liu Boc2931662016-12-22 17:13:54 -08007566 /*
7567 * If dio write length has been split due to no large enough
7568 * contiguous space, we need to compensate our inode counter
7569 * appropriately.
7570 */
7571 u64 num_needed = num_extents - dio_data->outstanding_extents;
7572
Filipe Manana9c9464c2015-11-04 09:52:04 +00007573 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007574 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007575 spin_unlock(&BTRFS_I(inode)->lock);
7576 }
7577}
7578
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7580 struct buffer_head *bh_result, int create)
7581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007583 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307585 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007586 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007587 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007588 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007589 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007590 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007591
Miao Xie172a5042013-02-21 02:48:22 -07007592 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007593 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007594 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007595 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007596
Josef Bacikc3298612012-08-03 16:49:19 -04007597 lockstart = start;
7598 lockend = start + len - 1;
7599
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007600 if (current->journal_info) {
7601 /*
7602 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007603 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007604 * confused.
7605 */
chandan50745b02015-08-28 21:10:13 +05307606 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007607 current->journal_info = NULL;
7608 }
7609
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 /*
7611 * If this errors out it's because we couldn't invalidate pagecache for
7612 * this range and we need to fallback to buffered.
7613 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007614 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7615 create)) {
7616 ret = -ENOTBLK;
7617 goto err;
7618 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007619
Josef Bacik4b46fce2010-05-23 11:00:55 -04007620 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007621 if (IS_ERR(em)) {
7622 ret = PTR_ERR(em);
7623 goto unlock_err;
7624 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007625
7626 /*
7627 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7628 * io. INLINE is special, and we could probably kludge it in here, but
7629 * it's still buffered so for safety lets just fall back to the generic
7630 * buffered path.
7631 *
7632 * For COMPRESSED we _have_ to read the entire extent in so we can
7633 * decompress it, so there will be buffering required no matter what we
7634 * do, so go ahead and fallback to buffered.
7635 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007636 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007637 * to buffered IO. Don't blame me, this is the price we pay for using
7638 * the generic code.
7639 */
7640 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7641 em->block_start == EXTENT_MAP_INLINE) {
7642 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007643 ret = -ENOTBLK;
7644 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007645 }
7646
7647 /* Just a good old fashioned hole, return */
7648 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7649 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7650 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 }
7653
7654 /*
7655 * We don't allocate a new extent in the following cases
7656 *
7657 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7658 * existing extent.
7659 * 2) The extent is marked as PREALLOC. We're good to go here and can
7660 * just use the extent.
7661 *
7662 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007663 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 len = min(len, em->len - (start - em->start));
7665 lockstart = start + len;
7666 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007667 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
7669 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7670 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7671 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007673 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674
7675 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7676 type = BTRFS_ORDERED_PREALLOC;
7677 else
7678 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007679 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007681
Josef Bacik00361582013-08-14 14:02:47 -04007682 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007683 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007684 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007685 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007686
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007687 em2 = btrfs_create_dio_extent(inode, start, len,
7688 orig_start, block_start,
7689 len, orig_block_len,
7690 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007691 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007692 if (type == BTRFS_ORDERED_PREALLOC) {
7693 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007694 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007695 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007696 if (em2 && IS_ERR(em2)) {
7697 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007698 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007699 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007700 /*
7701 * For inode marked NODATACOW or extent marked PREALLOC,
7702 * use the existing or preallocated extent, so does not
7703 * need to adjust btrfs_space_info's bytes_may_use.
7704 */
7705 btrfs_free_reserved_data_space_noquota(inode,
7706 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007707 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
Josef Bacik00361582013-08-14 14:02:47 -04007710
Chris Mason46bfbb52010-05-26 11:04:10 -04007711 /*
7712 * this will cow the extent, reset the len in case we changed
7713 * it above
7714 */
7715 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007716 free_extent_map(em);
7717 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 if (IS_ERR(em)) {
7719 ret = PTR_ERR(em);
7720 goto unlock_err;
7721 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007722 len = min(len, em->len - (start - em->start));
7723unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7725 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007726 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 bh_result->b_bdev = em->bdev;
7728 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007729 if (create) {
7730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7731 set_buffer_new(bh_result);
7732
7733 /*
7734 * Need to update the i_size under the extent lock so buffered
7735 * readers will get the updated i_size when we unlock.
7736 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007737 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007738 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007739
Filipe Manana9c9464c2015-11-04 09:52:04 +00007740 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307741 WARN_ON(dio_data->reserve < len);
7742 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007743 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307744 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007745 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746
Josef Bacikeb838e72012-07-31 16:28:48 -04007747 /*
7748 * In the case of write we need to clear and unlock the entire range,
7749 * in the case of read we need to unlock only the end area that we
7750 * aren't using if there is any left over space.
7751 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007752 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007753 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7754 lockend, unlock_bits, 1, 0,
7755 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007756 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007758 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007759
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 free_extent_map(em);
7761
7762 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
7764unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7766 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007767err:
chandan50745b02015-08-28 21:10:13 +05307768 if (dio_data)
7769 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007770 /*
7771 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7772 * write less data then expected, so that we don't underflow our inode's
7773 * outstanding extents counter.
7774 */
7775 if (create && dio_data)
7776 adjust_dio_outstanding_extents(inode, dio_data, len);
7777
Josef Bacikeb838e72012-07-31 16:28:48 -04007778 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779}
7780
Miao Xie8b110e32014-09-12 18:44:03 +08007781static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007782 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007783{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007785 int ret;
7786
Mike Christie37226b22016-06-05 14:31:52 -05007787 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007788
7789 bio_get(bio);
7790
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007791 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007792 if (ret)
7793 goto err;
7794
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007795 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007796err:
7797 bio_put(bio);
7798 return ret;
7799}
7800
7801static int btrfs_check_dio_repairable(struct inode *inode,
7802 struct bio *failed_bio,
7803 struct io_failure_record *failrec,
7804 int failed_mirror)
7805{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007807 int num_copies;
7808
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007809 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007810 if (num_copies == 1) {
7811 /*
7812 * we only have a single copy of the data, so don't bother with
7813 * all the retry and error correction code that follows. no
7814 * matter what the error is, it is very likely to persist.
7815 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007816 btrfs_debug(fs_info,
7817 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7818 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007819 return 0;
7820 }
7821
7822 failrec->failed_mirror = failed_mirror;
7823 failrec->this_mirror++;
7824 if (failrec->this_mirror == failed_mirror)
7825 failrec->this_mirror++;
7826
7827 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007828 btrfs_debug(fs_info,
7829 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7830 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007831 return 0;
7832 }
7833
7834 return 1;
7835}
7836
7837static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307838 struct page *page, unsigned int pgoff,
7839 u64 start, u64 end, int failed_mirror,
7840 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007841{
7842 struct io_failure_record *failrec;
7843 struct bio *bio;
7844 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007845 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007846 int ret;
7847
Mike Christie37226b22016-06-05 14:31:52 -05007848 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007849
7850 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7851 if (ret)
7852 return ret;
7853
7854 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7855 failed_mirror);
7856 if (!ret) {
7857 free_io_failure(inode, failrec);
7858 return -EIO;
7859 }
7860
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 if ((failed_bio->bi_vcnt > 1)
7862 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007863 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007864 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007865
7866 isector = start - btrfs_io_bio(failed_bio)->logical;
7867 isector >>= inode->i_sb->s_blocksize_bits;
7868 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307869 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007870 if (!bio) {
7871 free_io_failure(inode, failrec);
7872 return -EIO;
7873 }
Mike Christie37226b22016-06-05 14:31:52 -05007874 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007875
7876 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7877 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7878 read_mode, failrec->this_mirror, failrec->in_validation);
7879
Mike Christie81a75f672016-06-05 14:31:54 -05007880 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007881 if (ret) {
7882 free_io_failure(inode, failrec);
7883 bio_put(bio);
7884 }
7885
7886 return ret;
7887}
7888
7889struct btrfs_retry_complete {
7890 struct completion done;
7891 struct inode *inode;
7892 u64 start;
7893 int uptodate;
7894};
7895
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007896static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007897{
7898 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307899 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007900 struct bio_vec *bvec;
7901 int i;
7902
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007903 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007904 goto end;
7905
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906 ASSERT(bio->bi_vcnt == 1);
7907 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007908 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307909
Miao Xie8b110e32014-09-12 18:44:03 +08007910 done->uptodate = 1;
7911 bio_for_each_segment_all(bvec, bio, i)
7912 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7913end:
7914 complete(&done->done);
7915 bio_put(bio);
7916}
7917
7918static int __btrfs_correct_data_nocsum(struct inode *inode,
7919 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007920{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307921 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007922 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007923 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 unsigned int pgoff;
7926 u32 sectorsize;
7927 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007928 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007929 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007930
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007932 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307933
Miao Xiec1dc0892014-09-12 18:43:56 +08007934 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007935 done.inode = inode;
7936
7937 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307938 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7939 pgoff = bvec->bv_offset;
7940
7941next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007942 done.uptodate = 0;
7943 done.start = start;
7944 init_completion(&done.done);
7945
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307946 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7947 pgoff, start, start + sectorsize - 1,
7948 io_bio->mirror_num,
7949 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007950 if (ret)
7951 return ret;
7952
7953 wait_for_completion(&done.done);
7954
7955 if (!done.uptodate) {
7956 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307957 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007958 }
7959
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 start += sectorsize;
7961
7962 if (nr_sectors--) {
7963 pgoff += sectorsize;
7964 goto next_block_or_try_again;
7965 }
Miao Xie8b110e32014-09-12 18:44:03 +08007966 }
7967
7968 return 0;
7969}
7970
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007971static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007972{
7973 struct btrfs_retry_complete *done = bio->bi_private;
7974 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007976 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007978 int uptodate;
7979 int ret;
7980 int i;
7981
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007982 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007983 goto end;
7984
7985 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307986
7987 start = done->start;
7988
7989 ASSERT(bio->bi_vcnt == 1);
7990 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007991 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992
Miao Xie8b110e32014-09-12 18:44:03 +08007993 bio_for_each_segment_all(bvec, bio, i) {
7994 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 bvec->bv_page, bvec->bv_offset,
7996 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007997 if (!ret)
7998 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008000 else
8001 uptodate = 0;
8002 }
8003
8004 done->uptodate = uptodate;
8005end:
8006 complete(&done->done);
8007 bio_put(bio);
8008}
8009
8010static int __btrfs_subio_endio_read(struct inode *inode,
8011 struct btrfs_io_bio *io_bio, int err)
8012{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008014 struct bio_vec *bvec;
8015 struct btrfs_retry_complete done;
8016 u64 start;
8017 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 u32 sectorsize;
8019 int nr_sectors;
8020 unsigned int pgoff;
8021 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008022 int i;
8023 int ret;
8024
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008026 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027
Miao Xie8b110e32014-09-12 18:44:03 +08008028 err = 0;
8029 start = io_bio->logical;
8030 done.inode = inode;
8031
Miao Xiec1dc0892014-09-12 18:43:56 +08008032 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8034
8035 pgoff = bvec->bv_offset;
8036next_block:
8037 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8038 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8039 bvec->bv_page, pgoff, start,
8040 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008041 if (likely(!ret))
8042 goto next;
8043try_again:
8044 done.uptodate = 0;
8045 done.start = start;
8046 init_completion(&done.done);
8047
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8049 pgoff, start, start + sectorsize - 1,
8050 io_bio->mirror_num,
8051 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008052 if (ret) {
8053 err = ret;
8054 goto next;
8055 }
8056
8057 wait_for_completion(&done.done);
8058
8059 if (!done.uptodate) {
8060 /* We might have another mirror, so try again */
8061 goto try_again;
8062 }
8063next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308064 offset += sectorsize;
8065 start += sectorsize;
8066
8067 ASSERT(nr_sectors);
8068
8069 if (--nr_sectors) {
8070 pgoff += sectorsize;
8071 goto next_block;
8072 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008073 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008074
8075 return err;
8076}
8077
Miao Xie8b110e32014-09-12 18:44:03 +08008078static int btrfs_subio_endio_read(struct inode *inode,
8079 struct btrfs_io_bio *io_bio, int err)
8080{
8081 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8082
8083 if (skip_csum) {
8084 if (unlikely(err))
8085 return __btrfs_correct_data_nocsum(inode, io_bio);
8086 else
8087 return 0;
8088 } else {
8089 return __btrfs_subio_endio_read(inode, io_bio, err);
8090 }
8091}
8092
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008093static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008094{
8095 struct btrfs_dio_private *dip = bio->bi_private;
8096 struct inode *inode = dip->inode;
8097 struct bio *dio_bio;
8098 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008099 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008100
Miao Xie8b110e32014-09-12 18:44:03 +08008101 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8102 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008103
Josef Bacik4b46fce2010-05-23 11:00:55 -04008104 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008105 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008106 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008107
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008109
Filipe Manana1636d1d2016-02-15 16:20:26 +00008110 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008111 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008112
8113 if (io_bio->end_io)
8114 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008115 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008116}
8117
Filipe Manana14543772015-11-24 16:23:54 +00008118static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8119 const u64 offset,
8120 const u64 bytes,
8121 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008125 u64 ordered_offset = offset;
8126 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008127 int ret;
8128
Chris Mason163cf092010-11-28 19:56:33 -05008129again:
8130 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8131 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008132 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008133 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008134 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008135 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008136
Liu Bo9e0af232014-08-15 23:36:53 +08008137 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8138 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008139 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008140out_test:
8141 /*
8142 * our bio might span multiple ordered extents. If we haven't
8143 * completed the accounting for the whole dio, go back and try again
8144 */
Filipe Manana14543772015-11-24 16:23:54 +00008145 if (ordered_offset < offset + bytes) {
8146 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008147 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008148 goto again;
8149 }
Filipe Manana14543772015-11-24 16:23:54 +00008150}
8151
8152static void btrfs_endio_direct_write(struct bio *bio)
8153{
8154 struct btrfs_dio_private *dip = bio->bi_private;
8155 struct bio *dio_bio = dip->dio_bio;
8156
8157 btrfs_endio_direct_write_update_ordered(dip->inode,
8158 dip->logical_offset,
8159 dip->bytes,
8160 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008161
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008163
Filipe Manana1636d1d2016-02-15 16:20:26 +00008164 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008165 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008166 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167}
8168
Mike Christie81a75f672016-06-05 14:31:54 -05008169static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008170 struct bio *bio, int mirror_num,
8171 unsigned long bio_flags, u64 offset)
8172{
8173 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008174 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008175 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008176 return 0;
8177}
8178
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008179static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008180{
8181 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008182 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008183
Miao Xie8b110e32014-09-12 18:44:03 +08008184 if (err)
8185 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008186 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008187 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8188 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008189 (unsigned long long)bio->bi_iter.bi_sector,
8190 bio->bi_iter.bi_size, err);
8191
8192 if (dip->subio_endio)
8193 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008194
8195 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008196 dip->errors = 1;
8197
8198 /*
8199 * before atomic variable goto zero, we must make sure
8200 * dip->errors is perceived to be set.
8201 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008202 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008203 }
8204
8205 /* if there are more bios still pending for this dio, just exit */
8206 if (!atomic_dec_and_test(&dip->pending_bios))
8207 goto out;
8208
Chris Mason9be33952013-05-17 18:30:14 -04008209 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008210 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008211 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008212 dip->dio_bio->bi_error = 0;
8213 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008214 }
8215out:
8216 bio_put(bio);
8217}
8218
8219static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8220 u64 first_sector, gfp_t gfp_flags)
8221{
Chris Masonda2f0f72015-07-02 13:57:22 -07008222 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008223 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008224 if (bio)
8225 bio_associate_current(bio);
8226 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008227}
8228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008229static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008230 struct btrfs_dio_private *dip,
8231 struct bio *bio,
8232 u64 file_offset)
8233{
8234 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8235 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8236 int ret;
8237
8238 /*
8239 * We load all the csum data we need when we submit
8240 * the first bio to reduce the csum tree search and
8241 * contention.
8242 */
8243 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008244 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008245 file_offset);
8246 if (ret)
8247 return ret;
8248 }
8249
8250 if (bio == dip->orig_bio)
8251 return 0;
8252
8253 file_offset -= dip->logical_offset;
8254 file_offset >>= inode->i_sb->s_blocksize_bits;
8255 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8256
8257 return 0;
8258}
8259
Miao Xiee65e1532010-11-22 03:04:43 +00008260static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008261 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008262 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008265 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008266 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008267 int ret;
8268
Josef Bacikb812ce22012-11-16 13:56:32 -05008269 if (async_submit)
8270 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8271
Miao Xiee65e1532010-11-22 03:04:43 +00008272 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008273
8274 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008275 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008276 if (ret)
8277 goto err;
8278 }
Miao Xiee65e1532010-11-22 03:04:43 +00008279
Josef Bacik1ae39932011-04-06 14:41:34 -04008280 if (skip_sum)
8281 goto map;
8282
8283 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008284 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8285 file_offset,
8286 __btrfs_submit_bio_start_direct_io,
8287 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008288 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008289 } else if (write) {
8290 /*
8291 * If we aren't doing async submit, calculate the csum of the
8292 * bio now.
8293 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008295 if (ret)
8296 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008297 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008298 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008299 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008300 if (ret)
8301 goto err;
8302 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008303map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008304 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008305err:
8306 bio_put(bio);
8307 return ret;
8308}
8309
Mike Christie81a75f672016-06-05 14:31:54 -05008310static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008311 int skip_sum)
8312{
8313 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008315 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008316 struct bio *bio;
8317 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008318 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008319 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008320 u64 file_offset = dip->logical_offset;
8321 u64 submit_len = 0;
8322 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008323 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008324 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308325 int nr_sectors;
8326 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008327 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008328
Kent Overstreet4f024f32013-10-11 15:44:27 -07008329 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008330 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8331 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008332 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008333 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008334
Kent Overstreet4f024f32013-10-11 15:44:27 -07008335 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008336 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008337 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008338 goto submit;
8339 }
8340
David Woodhouse53b381b2013-01-29 18:40:14 -05008341 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008342 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008343 async_submit = 0;
8344 else
8345 async_submit = 1;
8346
Josef Bacik02f57c72011-04-06 14:25:44 -04008347 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8348 if (!bio)
8349 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008350
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008351 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008352 bio->bi_private = dip;
8353 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008354 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008355 atomic_inc(&dip->pending_bios);
8356
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008357 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308359 i = 0;
8360next_block:
8361 if (unlikely(map_length < submit_len + blocksize ||
8362 bio_add_page(bio, bvec->bv_page, blocksize,
8363 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008364 /*
8365 * inc the count before we submit the bio so
8366 * we know the end IO handler won't happen before
8367 * we inc the count. Otherwise, the dip might get freed
8368 * before we're done setting it up
8369 */
8370 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008371 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008372 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008373 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008374 if (ret) {
8375 bio_put(bio);
8376 atomic_dec(&dip->pending_bios);
8377 goto out_err;
8378 }
8379
Miao Xiee65e1532010-11-22 03:04:43 +00008380 start_sector += submit_len >> 9;
8381 file_offset += submit_len;
8382
8383 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008384
8385 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8386 start_sector, GFP_NOFS);
8387 if (!bio)
8388 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008389 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008390 bio->bi_private = dip;
8391 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008392 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008393
Kent Overstreet4f024f32013-10-11 15:44:27 -07008394 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008395 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008396 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008397 &map_length, NULL, 0);
8398 if (ret) {
8399 bio_put(bio);
8400 goto out_err;
8401 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308402
8403 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008404 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308405 submit_len += blocksize;
8406 if (--nr_sectors) {
8407 i++;
8408 goto next_block;
8409 }
Miao Xiee65e1532010-11-22 03:04:43 +00008410 }
8411 }
8412
Josef Bacik02f57c72011-04-06 14:25:44 -04008413submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008414 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008415 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008416 if (!ret)
8417 return 0;
8418
8419 bio_put(bio);
8420out_err:
8421 dip->errors = 1;
8422 /*
8423 * before atomic variable goto zero, we must
8424 * make sure dip->errors is perceived to be set.
8425 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008426 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008427 if (atomic_dec_and_test(&dip->pending_bios))
8428 bio_io_error(dip->orig_bio);
8429
8430 /* bio_end_io() will handle error, so we needn't return it */
8431 return 0;
8432}
8433
Mike Christie8a4c1e42016-06-05 14:31:50 -05008434static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8435 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008436{
Filipe Manana61de7182015-07-01 12:13:10 +01008437 struct btrfs_dio_private *dip = NULL;
8438 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008439 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008440 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008441 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008442 int ret = 0;
8443
8444 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8445
Chris Mason9be33952013-05-17 18:30:14 -04008446 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008447 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008448 ret = -ENOMEM;
8449 goto free_ordered;
8450 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008451
Miao Xiec1dc0892014-09-12 18:43:56 +08008452 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008453 if (!dip) {
8454 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008455 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008456 }
8457
8458 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008459 dip->inode = inode;
8460 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008461 dip->bytes = dio_bio->bi_iter.bi_size;
8462 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008463 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008464 dip->orig_bio = io_bio;
8465 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008466 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008467 btrfs_bio = btrfs_io_bio(io_bio);
8468 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008469
Miao Xiec1dc0892014-09-12 18:43:56 +08008470 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008471 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008472 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008473 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008474 dip->subio_endio = btrfs_subio_endio_read;
8475 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008476
Filipe Mananaf28a4922015-12-08 19:23:20 +00008477 /*
8478 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8479 * even if we fail to submit a bio, because in such case we do the
8480 * corresponding error handling below and it must not be done a second
8481 * time by btrfs_direct_IO().
8482 */
8483 if (write) {
8484 struct btrfs_dio_data *dio_data = current->journal_info;
8485
8486 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8487 dip->bytes;
8488 dio_data->unsubmitted_oe_range_start =
8489 dio_data->unsubmitted_oe_range_end;
8490 }
8491
Mike Christie81a75f672016-06-05 14:31:54 -05008492 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008493 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008494 return;
Chris Mason9be33952013-05-17 18:30:14 -04008495
Miao Xie23ea8e52014-09-12 18:43:54 +08008496 if (btrfs_bio->end_io)
8497 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008498
Josef Bacik4b46fce2010-05-23 11:00:55 -04008499free_ordered:
8500 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008501 * If we arrived here it means either we failed to submit the dip
8502 * or we either failed to clone the dio_bio or failed to allocate the
8503 * dip. If we cloned the dio_bio and allocated the dip, we can just
8504 * call bio_endio against our io_bio so that we get proper resource
8505 * cleanup if we fail to submit the dip, otherwise, we must do the
8506 * same as btrfs_endio_direct_[write|read] because we can't call these
8507 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008508 */
Filipe Manana61de7182015-07-01 12:13:10 +01008509 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008510 io_bio->bi_error = -EIO;
8511 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008512 /*
8513 * The end io callbacks free our dip, do the final put on io_bio
8514 * and all the cleanup and final put for dio_bio (through
8515 * dio_end_io()).
8516 */
8517 dip = NULL;
8518 io_bio = NULL;
8519 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008520 if (write)
8521 btrfs_endio_direct_write_update_ordered(inode,
8522 file_offset,
8523 dio_bio->bi_iter.bi_size,
8524 0);
8525 else
Filipe Manana61de7182015-07-01 12:13:10 +01008526 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8527 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008528
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008529 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008530 /*
8531 * Releases and cleans up our dio_bio, no need to bio_put()
8532 * nor bio_endio()/bio_io_error() against dio_bio.
8533 */
8534 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008535 }
Filipe Manana61de7182015-07-01 12:13:10 +01008536 if (io_bio)
8537 bio_put(io_bio);
8538 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008539}
8540
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008541static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8542 struct kiocb *iocb,
8543 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008544{
8545 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008546 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008547 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008548 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008549
8550 if (offset & blocksize_mask)
8551 goto out;
8552
Al Viro28060d52014-03-22 05:15:17 -04008553 if (iov_iter_alignment(iter) & blocksize_mask)
8554 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008555
Al Viro28060d52014-03-22 05:15:17 -04008556 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008557 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008558 return 0;
8559 /*
8560 * Check to make sure we don't have duplicate iov_base's in this
8561 * iovec, if so return EINVAL, otherwise we'll get csum errors
8562 * when reading back.
8563 */
8564 for (seg = 0; seg < iter->nr_segs; seg++) {
8565 for (i = seg + 1; i < iter->nr_segs; i++) {
8566 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008567 goto out;
8568 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008569 }
8570 retval = 0;
8571out:
8572 return retval;
8573}
Josef Bacikeb838e72012-07-31 16:28:48 -04008574
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008575static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008576{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008577 struct file *file = iocb->ki_filp;
8578 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308580 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008581 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008582 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008583 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008584 bool wakeup = true;
8585 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008586 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008588 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008589 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008590
Jens Axboefe0f07d2015-04-15 17:05:48 -06008591 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008592 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008593
Josef Bacik0e267c42013-07-02 10:38:02 -04008594 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008595 * The generic stuff only does filemap_write_and_wait_range, which
8596 * isn't enough if we've written compressed pages to this area, so
8597 * we need to flush the dirty pages again to make absolutely sure
8598 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008599 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008600 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008601 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8602 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008603 filemap_fdatawrite_range(inode->i_mapping, offset,
8604 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008605
Omar Sandoval6f673762015-03-16 04:33:52 -07008606 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008607 /*
8608 * If the write DIO is beyond the EOF, we need update
8609 * the isize, but it is protected by i_mutex. So we can
8610 * not unlock the i_mutex at this case.
8611 */
8612 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008613 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008614 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008615 relock = true;
8616 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008617 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008618 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008619 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008620 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008621
8622 /*
8623 * We need to know how many extents we reserved so that we can
8624 * do the accounting properly if we go over the number we
8625 * originally calculated. Abuse current->journal_info for this.
8626 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008627 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008628 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008629 dio_data.unsubmitted_oe_range_start = (u64)offset;
8630 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308631 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308632 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008633 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8634 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008635 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008636 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8637 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008638 }
8639
Omar Sandoval17f8c842015-03-16 04:33:50 -07008640 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008641 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008642 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008643 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008644 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308645 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008646 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008647 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308648 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008649 btrfs_delalloc_release_space(inode, offset,
8650 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008651 /*
8652 * On error we might have left some ordered extents
8653 * without submitting corresponding bios for them, so
8654 * cleanup them up to avoid other tasks getting them
8655 * and waiting for them to complete forever.
8656 */
8657 if (dio_data.unsubmitted_oe_range_start <
8658 dio_data.unsubmitted_oe_range_end)
8659 btrfs_endio_direct_write_update_ordered(inode,
8660 dio_data.unsubmitted_oe_range_start,
8661 dio_data.unsubmitted_oe_range_end -
8662 dio_data.unsubmitted_oe_range_start,
8663 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008664 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008665 btrfs_delalloc_release_space(inode, offset,
8666 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008667 }
Miao Xie38851cc2013-02-08 07:04:11 +00008668out:
Miao Xie2e60a512013-02-08 07:01:08 +00008669 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008670 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008671 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008672 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008673
8674 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008675}
8676
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008677#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8678
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008679static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8680 __u64 start, __u64 len)
8681{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008682 int ret;
8683
8684 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8685 if (ret)
8686 return ret;
8687
Chris Masonec29ed52011-02-23 16:23:20 -05008688 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008689}
8690
Chris Mason9ebefb182007-06-15 13:50:00 -04008691int btrfs_readpage(struct file *file, struct page *page)
8692{
Chris Masond1310b22008-01-24 16:13:08 -05008693 struct extent_io_tree *tree;
8694 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008695 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008696}
Chris Mason1832a6d2007-12-21 16:27:21 -05008697
Chris Mason39279cc2007-06-12 06:35:45 -04008698static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8699{
Chris Masond1310b22008-01-24 16:13:08 -05008700 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008701 struct inode *inode = page->mapping->host;
8702 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008703
8704 if (current->flags & PF_MEMALLOC) {
8705 redirty_page_for_writepage(wbc, page);
8706 unlock_page(page);
8707 return 0;
8708 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008709
8710 /*
8711 * If we are under memory pressure we will call this directly from the
8712 * VM, we need to make sure we have the inode referenced for the ordered
8713 * extent. If not just return like we didn't do anything.
8714 */
8715 if (!igrab(inode)) {
8716 redirty_page_for_writepage(wbc, page);
8717 return AOP_WRITEPAGE_ACTIVATE;
8718 }
Chris Masond1310b22008-01-24 16:13:08 -05008719 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008720 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8721 btrfs_add_delayed_iput(inode);
8722 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008723}
Chris Mason39279cc2007-06-12 06:35:45 -04008724
Eric Sandeen48a3b632013-04-25 20:41:01 +00008725static int btrfs_writepages(struct address_space *mapping,
8726 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008727{
Chris Masond1310b22008-01-24 16:13:08 -05008728 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008729
Chris Masond1310b22008-01-24 16:13:08 -05008730 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008731 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8732}
8733
Chris Mason3ab2fb52007-11-08 10:59:22 -05008734static int
8735btrfs_readpages(struct file *file, struct address_space *mapping,
8736 struct list_head *pages, unsigned nr_pages)
8737{
Chris Masond1310b22008-01-24 16:13:08 -05008738 struct extent_io_tree *tree;
8739 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008740 return extent_readpages(tree, mapping, pages, nr_pages,
8741 btrfs_get_extent);
8742}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008743static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008744{
Chris Masond1310b22008-01-24 16:13:08 -05008745 struct extent_io_tree *tree;
8746 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008747 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008748
Chris Masond1310b22008-01-24 16:13:08 -05008749 tree = &BTRFS_I(page->mapping->host)->io_tree;
8750 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008751 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008752 if (ret == 1) {
8753 ClearPagePrivate(page);
8754 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008755 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008756 }
8757 return ret;
8758}
Chris Mason39279cc2007-06-12 06:35:45 -04008759
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8761{
Chris Mason98509cf2008-09-11 15:51:43 -04008762 if (PageWriteback(page) || PageDirty(page))
8763 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008764 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008765}
8766
Lukas Czernerd47992f2013-05-21 23:17:23 -04008767static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8768 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008769{
Josef Bacik5fd02042012-05-02 14:00:54 -04008770 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008771 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008772 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008773 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008774 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008775 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308776 u64 start;
8777 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008778 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008779
Chris Mason8b62b722009-09-02 16:53:46 -04008780 /*
8781 * we have the page locked, so new writeback can't start,
8782 * and the dirty bit won't be cleared while we are here.
8783 *
8784 * Wait for IO on this page so that we can safely clear
8785 * the PagePrivate2 bit and do ordered accounting
8786 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008788
Josef Bacik5fd02042012-05-02 14:00:54 -04008789 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008790 if (offset) {
8791 btrfs_releasepage(page, GFP_NOFS);
8792 return;
8793 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008794
8795 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008796 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308797again:
8798 start = page_start;
8799 ordered = btrfs_lookup_ordered_range(inode, start,
8800 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308802 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008803 /*
8804 * IO on this page will never be started, so we need
8805 * to account for any ordered extents now
8806 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008807 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308808 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008809 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8811 EXTENT_DEFRAG, 1, 0, &cached_state,
8812 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008813 /*
8814 * whoever cleared the private bit is responsible
8815 * for the finish_ordered_io
8816 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008817 if (TestClearPagePrivate2(page)) {
8818 struct btrfs_ordered_inode_tree *tree;
8819 u64 new_len;
8820
8821 tree = &BTRFS_I(inode)->ordered_tree;
8822
8823 spin_lock_irq(&tree->lock);
8824 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308825 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008826 if (new_len < ordered->truncated_len)
8827 ordered->truncated_len = new_len;
8828 spin_unlock_irq(&tree->lock);
8829
8830 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308831 start,
8832 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008833 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008834 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008836 if (!inode_evicting) {
8837 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308838 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008839 &cached_state);
8840 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308841
8842 start = end + 1;
8843 if (start < page_end)
8844 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008846
Qu Wenruob9d0b382015-09-29 10:35:16 +08008847 /*
8848 * Qgroup reserved space handler
8849 * Page here will be either
8850 * 1) Already written to disk
8851 * In this case, its reserved space is released from data rsv map
8852 * and will be freed by delayed_ref handler finally.
8853 * So even we call qgroup_free_data(), it won't decrease reserved
8854 * space.
8855 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008856 * This means the reserved space should be freed here. However,
8857 * if a truncate invalidates the page (by clearing PageDirty)
8858 * and the page is accounted for while allocating extent
8859 * in btrfs_check_data_free_space() we let delayed_ref to
8860 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008861 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008862 if (PageDirty(page))
8863 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008864 if (!inode_evicting) {
8865 clear_extent_bit(tree, page_start, page_end,
8866 EXTENT_LOCKED | EXTENT_DIRTY |
8867 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8868 EXTENT_DEFRAG, 1, 1,
8869 &cached_state, GFP_NOFS);
8870
8871 __btrfs_releasepage(page, GFP_NOFS);
8872 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008873
Chris Mason4a096752008-07-21 10:29:44 -04008874 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008875 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008876 ClearPagePrivate(page);
8877 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008878 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008879 }
Chris Mason39279cc2007-06-12 06:35:45 -04008880}
8881
Chris Mason9ebefb182007-06-15 13:50:00 -04008882/*
8883 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8884 * called from a page fault handler when a page is first dirtied. Hence we must
8885 * be careful to check for EOF conditions here. We set the page up correctly
8886 * for a written page which means we get ENOSPC checking when writing into
8887 * holes and correct delalloc and unwritten extent mapping on filesystems that
8888 * support these features.
8889 *
8890 * We are not allowed to take the i_mutex here so we have to play games to
8891 * protect against truncate races as the page could now be beyond EOF. Because
8892 * vmtruncate() writes the inode size before removing pages, once we have the
8893 * page lock we can determine safely if the page is beyond EOF. If it is not
8894 * beyond EOF, then the page is guaranteed safe against truncation until we
8895 * unlock the page.
8896 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008897int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008898{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008899 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008900 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8903 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008904 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 char *kaddr;
8906 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008907 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008908 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008909 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308910 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008911 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308913 u64 end;
8914
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008916
Jan Karab2b5ef52012-06-12 16:20:45 +02008917 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008918 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008919 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308920 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008921
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308922 /*
8923 * Reserving delalloc space after obtaining the page lock can lead to
8924 * deadlock. For example, if a dirty page is locked by this function
8925 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8926 * dirty page write out, then the btrfs_writepage() function could
8927 * end up waiting indefinitely to get a lock on the page currently
8928 * being processed by btrfs_page_mkwrite() function.
8929 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008930 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308931 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008932 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008933 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008934 reserved = 1;
8935 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008936 if (ret) {
8937 if (ret == -ENOMEM)
8938 ret = VM_FAULT_OOM;
8939 else /* -ENOSPC, -EIO, etc */
8940 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008941 if (reserved)
8942 goto out;
8943 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008944 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008945
Nick Piggin56a76f82009-03-31 15:23:23 -07008946 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008948 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008950
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008953 /* page got truncated out from underneath us */
8954 goto out_unlock;
8955 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 wait_on_page_writeback(page);
8957
David Sterbaff13db42015-12-03 14:30:40 +01008958 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 set_page_extent_mapped(page);
8960
Chris Masoneb84ae02008-07-17 13:53:27 -04008961 /*
8962 * we can't set the delalloc bits if there are pending ordered
8963 * extents. Drop our locks and wait for them to finish
8964 */
Liu Bo9a9239a2017-01-24 15:58:51 -08008965 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008967 unlock_extent_cached(io_tree, page_start, page_end,
8968 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008970 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 btrfs_put_ordered_extent(ordered);
8972 goto again;
8973 }
8974
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008975 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008976 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008977 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008978 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 end = page_start + reserved_space - 1;
8980 spin_lock(&BTRFS_I(inode)->lock);
8981 BTRFS_I(inode)->outstanding_extents++;
8982 spin_unlock(&BTRFS_I(inode)->lock);
8983 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008984 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308985 }
8986 }
8987
Josef Bacikfbf19082009-10-01 17:10:23 -04008988 /*
Liu Bo54160342016-12-13 12:15:19 -08008989 * page_mkwrite gets called when the page is firstly dirtied after it's
8990 * faulted in, but write(2) could also dirty a page and set delalloc
8991 * bits, thus in this case for space account reason, we still need to
8992 * clear any delalloc bits within this page range since we have to
8993 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008994 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308995 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008996 EXTENT_DIRTY | EXTENT_DELALLOC |
8997 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008998 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008999
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309000 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009001 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009002 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009003 unlock_extent_cached(io_tree, page_start, page_end,
9004 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009005 ret = VM_FAULT_SIGBUS;
9006 goto out_unlock;
9007 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009008 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009009
9010 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009011 if (page_start + PAGE_SIZE > size)
9012 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009013 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009014 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009015
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009016 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 flush_dcache_page(page);
9020 kunmap(page);
9021 }
Chris Mason247e7432008-07-17 12:53:51 -04009022 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009024 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009025
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009026 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009027 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009028 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009029
Josef Bacik2ac55d42010-02-03 19:33:23 +00009030 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009031
9032out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009033 if (!ret) {
9034 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009035 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009036 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009037 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009038out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309039 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009040out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009041 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009042 return ret;
9043}
9044
Josef Bacika41ad392011-01-31 15:30:16 -05009045static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009046{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009047 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009048 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009049 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009050 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009051 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009052 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009053 u64 mask = fs_info->sectorsize - 1;
9054 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009055
Josef Bacik0ef8b722013-10-25 16:13:35 -04009056 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9057 (u64)-1);
9058 if (ret)
9059 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009060
Josef Bacikfcb80c22011-05-03 10:40:22 -04009061 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009062 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009063 * 3 things going on here
9064 *
9065 * 1) We need to reserve space for our orphan item and the space to
9066 * delete our orphan item. Lord knows we don't want to have a dangling
9067 * orphan item because we didn't reserve space to remove it.
9068 *
9069 * 2) We need to reserve space to update our inode.
9070 *
9071 * 3) We need to have something to cache all the space that is going to
9072 * be free'd up by the truncate operation, but also have some slack
9073 * space reserved in case it uses space during the truncate (thank you
9074 * very much snapshotting).
9075 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009076 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009077 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009078 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009079 * doesn't end up using space reserved for updating the inode or
9080 * removing the orphan item. We also need to be able to stop the
9081 * transaction and start a new one, which means we need to be able to
9082 * update the inode several times, and we have no idea of knowing how
9083 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009084 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009085 * Then there is the orphan item, which does indeed need to be held on
9086 * to for the whole operation, and we need nobody to touch this reserved
9087 * space except the orphan code.
9088 *
9089 * So that leaves us with
9090 *
9091 * 1) root->orphan_block_rsv - for the orphan deletion.
9092 * 2) rsv - for the truncate reservation, which we will steal from the
9093 * transaction reservation.
9094 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9095 * updating the inode.
9096 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009097 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009098 if (!rsv)
9099 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009100 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009101 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009102
Josef Bacik907cbce2011-08-08 13:46:15 -04009103 /*
Josef Bacik07127182011-08-19 10:29:59 -04009104 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009105 * 1 for updating the inode.
9106 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009107 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009108 if (IS_ERR(trans)) {
9109 err = PTR_ERR(trans);
9110 goto out;
9111 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009112
Josef Bacik907cbce2011-08-08 13:46:15 -04009113 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009114 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009115 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009116 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009117
Chris Mason5a3f23d2009-03-31 13:27:11 -04009118 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009119 * So if we truncate and then write and fsync we normally would just
9120 * write the extents that changed, which is a problem if we need to
9121 * first truncate that entire inode. So set this flag so we write out
9122 * all of the extents in the inode to the sync log so we're completely
9123 * safe.
9124 */
9125 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009126 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009127
Yan, Zheng80825102009-11-12 09:35:36 +00009128 while (1) {
9129 ret = btrfs_truncate_inode_items(trans, root, inode,
9130 inode->i_size,
9131 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009132 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009133 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009134 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009135 }
Chris Mason39279cc2007-06-12 06:35:45 -04009136
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009137 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009138 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009139 if (ret) {
9140 err = ret;
9141 break;
9142 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009143
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009144 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009145 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009146
9147 trans = btrfs_start_transaction(root, 2);
9148 if (IS_ERR(trans)) {
9149 ret = err = PTR_ERR(trans);
9150 trans = NULL;
9151 break;
9152 }
9153
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009154 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009155 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009156 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009157 BUG_ON(ret); /* shouldn't happen */
9158 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009159 }
9160
9161 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009162 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009163 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009164 if (ret)
9165 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009166 }
9167
Chris Mason917c16b2011-11-08 14:49:59 -05009168 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009169 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009170 ret = btrfs_update_inode(trans, root, inode);
9171 if (ret && !err)
9172 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009173
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009174 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009175 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009176 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009177out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009178 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009179
Josef Bacik3893e332011-01-31 16:03:11 -05009180 if (ret && !err)
9181 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009182
Josef Bacik3893e332011-01-31 16:03:11 -05009183 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009184}
9185
Sven Wegener3b963622008-06-09 21:57:42 -04009186/*
Chris Masond352ac62008-09-29 15:18:18 -04009187 * create a new subvolume directory/inode (helper for the ioctl).
9188 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009189int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009190 struct btrfs_root *new_root,
9191 struct btrfs_root *parent_root,
9192 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009193{
Chris Mason39279cc2007-06-12 06:35:45 -04009194 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009195 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009196 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009197
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009198 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9199 new_dirid, new_dirid,
9200 S_IFDIR | (~current_umask() & S_IRWXUGO),
9201 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009202 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009203 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009204 inode->i_op = &btrfs_dir_inode_operations;
9205 inode->i_fop = &btrfs_dir_file_operations;
9206
Miklos Szeredibfe86842011-10-28 14:13:29 +02009207 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009208 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009209 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009210
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009211 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9212 if (err)
9213 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009214 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009215 new_root->root_key.objectid, err);
9216
Yan, Zheng76dda932009-09-21 16:00:26 -04009217 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009218
Yan, Zheng76dda932009-09-21 16:00:26 -04009219 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009220 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009221}
9222
Chris Mason39279cc2007-06-12 06:35:45 -04009223struct inode *btrfs_alloc_inode(struct super_block *sb)
9224{
9225 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009226 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009227
9228 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9229 if (!ei)
9230 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009231
9232 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009233 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009234 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009235 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009236 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009237 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009238 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009239 ei->disk_i_size = 0;
9240 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009241 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009242 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009243 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009245 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009246 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009247
Josef Bacik9e0baf62011-07-15 15:16:44 +00009248 spin_lock_init(&ei->lock);
9249 ei->outstanding_extents = 0;
9250 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009251
Josef Bacik72ac3c02012-05-23 14:13:11 -04009252 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009253 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009254
Miao Xie16cdcec2011-04-22 18:12:22 +08009255 ei->delayed_node = NULL;
9256
chandan r9cc97d62012-07-04 12:48:07 +05309257 ei->i_otime.tv_sec = 0;
9258 ei->i_otime.tv_nsec = 0;
9259
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009260 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009261 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009262 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9263 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009264 ei->io_tree.track_uptodate = 1;
9265 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009266 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009267 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009268 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009269 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009270 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009271 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009273 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274
9275 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009276}
9277
Josef Bacikaaedb552013-10-11 14:44:09 -04009278#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9279void btrfs_test_destroy_inode(struct inode *inode)
9280{
9281 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9282 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9283}
9284#endif
9285
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009286static void btrfs_i_callback(struct rcu_head *head)
9287{
9288 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009289 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9290}
9291
Chris Mason39279cc2007-06-12 06:35:45 -04009292void btrfs_destroy_inode(struct inode *inode)
9293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009295 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009296 struct btrfs_root *root = BTRFS_I(inode)->root;
9297
Al Virob3d9b7a2012-06-09 13:51:19 -04009298 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009299 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009300 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9301 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009302 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9303 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009304 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009305
Chris Mason5a3f23d2009-03-31 13:27:11 -04009306 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009307 * This can happen where we create an inode, but somebody else also
9308 * created the same inode and we need to destroy the one we already
9309 * created.
9310 */
9311 if (!root)
9312 goto free;
9313
Josef Bacik8a35d952012-05-23 14:26:42 -04009314 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9315 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009316 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009317 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009318 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009319 }
Josef Bacik7b128762008-07-24 12:17:14 -04009320
Chris Masond3977122009-01-05 21:25:51 -05009321 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009322 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9323 if (!ordered)
9324 break;
9325 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009326 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009327 "found ordered extent %llu %llu on inode cleanup",
9328 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009329 btrfs_remove_ordered_extent(inode, ordered);
9330 btrfs_put_ordered_extent(ordered);
9331 btrfs_put_ordered_extent(ordered);
9332 }
9333 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009334 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009335 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009336 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009337free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009338 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009339}
9340
Al Viro45321ac2010-06-07 13:43:19 -04009341int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009342{
9343 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009344
Naohiro Aota6379ef92013-06-06 09:56:34 +00009345 if (root == NULL)
9346 return 1;
9347
Liu Bofa6ac872013-02-20 14:10:23 +00009348 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009349 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009350 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009351 else
Al Viro45321ac2010-06-07 13:43:19 -04009352 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009353}
9354
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009355static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009356{
9357 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9358
9359 inode_init_once(&ei->vfs_inode);
9360}
9361
9362void btrfs_destroy_cachep(void)
9363{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009364 /*
9365 * Make sure all delayed rcu free inodes are flushed before we
9366 * destroy cache.
9367 */
9368 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009369 kmem_cache_destroy(btrfs_inode_cachep);
9370 kmem_cache_destroy(btrfs_trans_handle_cachep);
9371 kmem_cache_destroy(btrfs_transaction_cachep);
9372 kmem_cache_destroy(btrfs_path_cachep);
9373 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009374}
9375
9376int btrfs_init_cachep(void)
9377{
David Sterba837e1972012-09-07 03:00:48 -06009378 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009379 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009380 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9381 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009382 if (!btrfs_inode_cachep)
9383 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009384
David Sterba837e1972012-09-07 03:00:48 -06009385 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009386 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009387 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009388 if (!btrfs_trans_handle_cachep)
9389 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009390
David Sterba837e1972012-09-07 03:00:48 -06009391 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009393 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009394 if (!btrfs_transaction_cachep)
9395 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009396
David Sterba837e1972012-09-07 03:00:48 -06009397 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009398 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009399 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009400 if (!btrfs_path_cachep)
9401 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009402
David Sterba837e1972012-09-07 03:00:48 -06009403 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009404 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009405 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009406 if (!btrfs_free_space_cachep)
9407 goto fail;
9408
Chris Mason39279cc2007-06-12 06:35:45 -04009409 return 0;
9410fail:
9411 btrfs_destroy_cachep();
9412 return -ENOMEM;
9413}
9414
9415static int btrfs_getattr(struct vfsmount *mnt,
9416 struct dentry *dentry, struct kstat *stat)
9417{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009418 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009419 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009420 u32 blocksize = inode->i_sb->s_blocksize;
9421
Chris Mason39279cc2007-06-12 06:35:45 -04009422 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009423 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009424
9425 spin_lock(&BTRFS_I(inode)->lock);
9426 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9427 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009428 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009429 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009430 return 0;
9431}
9432
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009433static int btrfs_rename_exchange(struct inode *old_dir,
9434 struct dentry *old_dentry,
9435 struct inode *new_dir,
9436 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009438 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009439 struct btrfs_trans_handle *trans;
9440 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009441 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009442 struct inode *new_inode = new_dentry->d_inode;
9443 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009444 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009445 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009446 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9447 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009448 u64 old_idx = 0;
9449 u64 new_idx = 0;
9450 u64 root_objectid;
9451 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009452 bool root_log_pinned = false;
9453 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454
9455 /* we only allow rename subvolume link between subvolumes */
9456 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9457 return -EXDEV;
9458
9459 /* close the race window with snapshot create/destroy ioctl */
9460 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009461 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009462 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009463 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009464
9465 /*
9466 * We want to reserve the absolute worst case amount of items. So if
9467 * both inodes are subvols and we need to unlink them then that would
9468 * require 4 item modifications, but if they are both normal inodes it
9469 * would require 5 item modifications, so we'll assume their normal
9470 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9471 * should cover the worst case number of items we'll modify.
9472 */
9473 trans = btrfs_start_transaction(root, 12);
9474 if (IS_ERR(trans)) {
9475 ret = PTR_ERR(trans);
9476 goto out_notrans;
9477 }
9478
9479 /*
9480 * We need to find a free sequence number both in the source and
9481 * in the destination directory for the exchange.
9482 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009483 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009484 if (ret)
9485 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009486 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009487 if (ret)
9488 goto out_fail;
9489
9490 BTRFS_I(old_inode)->dir_index = 0ULL;
9491 BTRFS_I(new_inode)->dir_index = 0ULL;
9492
9493 /* Reference for the source. */
9494 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9495 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009496 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009498 btrfs_pin_log_trans(root);
9499 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009500 ret = btrfs_insert_inode_ref(trans, dest,
9501 new_dentry->d_name.name,
9502 new_dentry->d_name.len,
9503 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009504 btrfs_ino(BTRFS_I(new_dir)),
9505 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 if (ret)
9507 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 }
9509
9510 /* And now for the dest. */
9511 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9512 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009513 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009515 btrfs_pin_log_trans(dest);
9516 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 ret = btrfs_insert_inode_ref(trans, root,
9518 old_dentry->d_name.name,
9519 old_dentry->d_name.len,
9520 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009521 btrfs_ino(BTRFS_I(old_dir)),
9522 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009523 if (ret)
9524 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 }
9526
9527 /* Update inode version and ctime/mtime. */
9528 inode_inc_iversion(old_dir);
9529 inode_inc_iversion(new_dir);
9530 inode_inc_iversion(old_inode);
9531 inode_inc_iversion(new_inode);
9532 old_dir->i_ctime = old_dir->i_mtime = ctime;
9533 new_dir->i_ctime = new_dir->i_mtime = ctime;
9534 old_inode->i_ctime = ctime;
9535 new_inode->i_ctime = ctime;
9536
9537 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009538 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9539 BTRFS_I(old_inode), 1);
9540 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9541 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542 }
9543
9544 /* src is a subvolume */
9545 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9546 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9547 ret = btrfs_unlink_subvol(trans, root, old_dir,
9548 root_objectid,
9549 old_dentry->d_name.name,
9550 old_dentry->d_name.len);
9551 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009552 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9553 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 old_dentry->d_name.name,
9555 old_dentry->d_name.len);
9556 if (!ret)
9557 ret = btrfs_update_inode(trans, root, old_inode);
9558 }
9559 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009560 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 goto out_fail;
9562 }
9563
9564 /* dest is a subvolume */
9565 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9566 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9567 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9568 root_objectid,
9569 new_dentry->d_name.name,
9570 new_dentry->d_name.len);
9571 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009572 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9573 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 new_dentry->d_name.name,
9575 new_dentry->d_name.len);
9576 if (!ret)
9577 ret = btrfs_update_inode(trans, dest, new_inode);
9578 }
9579 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009580 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 goto out_fail;
9582 }
9583
9584 ret = btrfs_add_link(trans, new_dir, old_inode,
9585 new_dentry->d_name.name,
9586 new_dentry->d_name.len, 0, old_idx);
9587 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009588 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 goto out_fail;
9590 }
9591
9592 ret = btrfs_add_link(trans, old_dir, new_inode,
9593 old_dentry->d_name.name,
9594 old_dentry->d_name.len, 0, new_idx);
9595 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009596 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 goto out_fail;
9598 }
9599
9600 if (old_inode->i_nlink == 1)
9601 BTRFS_I(old_inode)->dir_index = old_idx;
9602 if (new_inode->i_nlink == 1)
9603 BTRFS_I(new_inode)->dir_index = new_idx;
9604
Filipe Manana86e8aa02016-05-05 02:02:27 +01009605 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009607 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9608 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009610 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009614 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9615 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 }
9619out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009620 /*
9621 * If we have pinned a log and an error happened, we unpin tasks
9622 * trying to sync the log and force them to fallback to a transaction
9623 * commit if the log currently contains any of the inodes involved in
9624 * this rename operation (to ensure we do not persist a log with an
9625 * inconsistent state for any of these inodes or leading to any
9626 * inconsistencies when replayed). If the transaction was aborted, the
9627 * abortion reason is propagated to userspace when attempting to commit
9628 * the transaction. If the log does not contain any of these inodes, we
9629 * allow the tasks to sync it.
9630 */
9631 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009632 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9633 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9634 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009635 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009636 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009637 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009638
9639 if (root_log_pinned) {
9640 btrfs_end_log_trans(root);
9641 root_log_pinned = false;
9642 }
9643 if (dest_log_pinned) {
9644 btrfs_end_log_trans(dest);
9645 dest_log_pinned = false;
9646 }
9647 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009648 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649out_notrans:
9650 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009651 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009653 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654
9655 return ret;
9656}
9657
9658static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9659 struct btrfs_root *root,
9660 struct inode *dir,
9661 struct dentry *dentry)
9662{
9663 int ret;
9664 struct inode *inode;
9665 u64 objectid;
9666 u64 index;
9667
9668 ret = btrfs_find_free_ino(root, &objectid);
9669 if (ret)
9670 return ret;
9671
9672 inode = btrfs_new_inode(trans, root, dir,
9673 dentry->d_name.name,
9674 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009675 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 objectid,
9677 S_IFCHR | WHITEOUT_MODE,
9678 &index);
9679
9680 if (IS_ERR(inode)) {
9681 ret = PTR_ERR(inode);
9682 return ret;
9683 }
9684
9685 inode->i_op = &btrfs_special_inode_operations;
9686 init_special_inode(inode, inode->i_mode,
9687 WHITEOUT_DEV);
9688
9689 ret = btrfs_init_inode_security(trans, inode, dir,
9690 &dentry->d_name);
9691 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009692 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693
9694 ret = btrfs_add_nondir(trans, dir, dentry,
9695 inode, 0, index);
9696 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009697 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698
9699 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009700out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009702 if (ret)
9703 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 iput(inode);
9705
Filipe Mananac9901612016-05-05 01:41:57 +01009706 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707}
9708
Chris Mason39279cc2007-06-12 06:35:45 -04009709static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 struct inode *new_dir, struct dentry *new_dentry,
9711 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009714 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009715 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009716 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9717 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009718 struct inode *new_inode = d_inode(new_dentry);
9719 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009720 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009721 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009722 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009723 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009724 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009725
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009726 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009727 return -EPERM;
9728
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009729 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009730 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009731 return -EXDEV;
9732
Li Zefan33345d012011-04-20 10:31:50 +08009733 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009734 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009735 return -ENOTEMPTY;
9736
Chris Mason39279cc2007-06-12 06:35:45 -04009737 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009738 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009739 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009740
9741
9742 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009743 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009744 new_dentry->d_name.name,
9745 new_dentry->d_name.len);
9746
9747 if (ret) {
9748 if (ret == -EEXIST) {
9749 /* we shouldn't get
9750 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309751 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009752 return ret;
9753 }
9754 } else {
9755 /* maybe -EOVERFLOW */
9756 return ret;
9757 }
9758 }
9759 ret = 0;
9760
Chris Mason5a3f23d2009-03-31 13:27:11 -04009761 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009762 * we're using rename to replace one file with another. Start IO on it
9763 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009764 */
Chris Mason8d875f92014-08-12 10:47:42 -07009765 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 filemap_flush(old_inode->i_mapping);
9767
Yan, Zheng76dda932009-09-21 16:00:26 -04009768 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009769 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009770 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009771 /*
9772 * We want to reserve the absolute worst case amount of items. So if
9773 * both inodes are subvols and we need to unlink them then that would
9774 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009775 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009776 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9777 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009778 * If our rename has the whiteout flag, we need more 5 units for the
9779 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9780 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009781 */
Filipe Manana5062af32016-05-05 10:26:26 +01009782 trans_num_items = 11;
9783 if (flags & RENAME_WHITEOUT)
9784 trans_num_items += 5;
9785 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009786 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009787 ret = PTR_ERR(trans);
9788 goto out_notrans;
9789 }
Chris Mason5f39d392007-10-15 16:14:19 -04009790
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009791 if (dest != root)
9792 btrfs_record_root_in_trans(trans, dest);
9793
Nikolay Borisov877574e2017-02-20 13:50:33 +02009794 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009795 if (ret)
9796 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009797
Miao Xie67de1172013-12-26 13:07:06 +08009798 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009799 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009800 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009801 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009803 btrfs_pin_log_trans(root);
9804 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009805 ret = btrfs_insert_inode_ref(trans, dest,
9806 new_dentry->d_name.name,
9807 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009808 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009809 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009810 if (ret)
9811 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009812 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009813
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009814 inode_inc_iversion(old_dir);
9815 inode_inc_iversion(new_dir);
9816 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009817 old_dir->i_ctime = old_dir->i_mtime =
9818 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009819 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009820
Chris Mason12fcfd22009-03-24 10:24:20 -04009821 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009822 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9823 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009824
Li Zefan33345d012011-04-20 10:31:50 +08009825 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009826 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9827 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9828 old_dentry->d_name.name,
9829 old_dentry->d_name.len);
9830 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009831 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9832 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009833 old_dentry->d_name.name,
9834 old_dentry->d_name.len);
9835 if (!ret)
9836 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009837 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009838 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009839 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009840 goto out_fail;
9841 }
Chris Mason39279cc2007-06-12 06:35:45 -04009842
9843 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009844 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009845 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009846 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009847 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9848 root_objectid = BTRFS_I(new_inode)->location.objectid;
9849 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9850 root_objectid,
9851 new_dentry->d_name.name,
9852 new_dentry->d_name.len);
9853 BUG_ON(new_inode->i_nlink == 0);
9854 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009855 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9856 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009857 new_dentry->d_name.name,
9858 new_dentry->d_name.len);
9859 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009860 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009861 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009862 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009863 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009864 goto out_fail;
9865 }
Chris Mason39279cc2007-06-12 06:35:45 -04009866 }
Josef Bacikaec74772008-07-24 12:12:38 -04009867
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009868 ret = btrfs_add_link(trans, new_dir, old_inode,
9869 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009870 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009871 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009872 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009873 goto out_fail;
9874 }
Chris Mason39279cc2007-06-12 06:35:45 -04009875
Miao Xie67de1172013-12-26 13:07:06 +08009876 if (old_inode->i_nlink == 1)
9877 BTRFS_I(old_inode)->dir_index = index;
9878
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009879 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009880 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009881
David Sterbaf85b7372017-01-20 14:54:07 +01009882 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9883 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009884 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009885 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009887
9888 if (flags & RENAME_WHITEOUT) {
9889 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9890 old_dentry);
9891
9892 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009893 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009894 goto out_fail;
9895 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009896 }
Chris Mason39279cc2007-06-12 06:35:45 -04009897out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009898 /*
9899 * If we have pinned the log and an error happened, we unpin tasks
9900 * trying to sync the log and force them to fallback to a transaction
9901 * commit if the log currently contains any of the inodes involved in
9902 * this rename operation (to ensure we do not persist a log with an
9903 * inconsistent state for any of these inodes or leading to any
9904 * inconsistencies when replayed). If the transaction was aborted, the
9905 * abortion reason is propagated to userspace when attempting to commit
9906 * the transaction. If the log does not contain any of these inodes, we
9907 * allow the tasks to sync it.
9908 */
9909 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009910 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9911 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9912 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009913 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009914 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009915 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009916
9917 btrfs_end_log_trans(root);
9918 log_pinned = false;
9919 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009920 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009921out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009922 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009923 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009924
Chris Mason39279cc2007-06-12 06:35:45 -04009925 return ret;
9926}
9927
Miklos Szeredi80ace852014-07-23 15:15:32 +02009928static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9929 struct inode *new_dir, struct dentry *new_dentry,
9930 unsigned int flags)
9931{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009932 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009933 return -EINVAL;
9934
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009935 if (flags & RENAME_EXCHANGE)
9936 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9937 new_dentry);
9938
9939 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009940}
9941
Miao Xie8ccf6f192012-10-25 09:28:04 +00009942static void btrfs_run_delalloc_work(struct btrfs_work *work)
9943{
9944 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009945 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009946
9947 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9948 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009949 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009950 filemap_flush(inode->i_mapping);
9951 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9952 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009953 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009954
9955 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009956 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009957 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009958 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009959 complete(&delalloc_work->completion);
9960}
9961
9962struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009963 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009964{
9965 struct btrfs_delalloc_work *work;
9966
David Sterba100d5702015-12-08 14:39:32 +01009967 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009968 if (!work)
9969 return NULL;
9970
9971 init_completion(&work->completion);
9972 INIT_LIST_HEAD(&work->list);
9973 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009974 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009975 WARN_ON_ONCE(!inode);
9976 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9977 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009978
9979 return work;
9980}
9981
9982void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9983{
9984 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009985 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009986}
9987
Chris Masond352ac62008-09-29 15:18:18 -04009988/*
9989 * some fairly slow code that needs optimization. This walks the list
9990 * of all the inodes with pending delalloc and forces them to disk.
9991 */
Miao Xie6c255e62014-03-06 13:55:01 +08009992static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9993 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009994{
Chris Masonea8c2812008-08-04 23:17:27 -04009995 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009996 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997 struct btrfs_delalloc_work *work, *next;
9998 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009999 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010001
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010003 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010004
Miao Xie573bfb72014-03-06 13:55:03 +080010005 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010006 spin_lock(&root->delalloc_lock);
10007 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010008 while (!list_empty(&splice)) {
10009 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010010 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010011
Miao Xieeb73c1b2013-05-15 07:48:22 +000010012 list_move_tail(&binode->delalloc_inodes,
10013 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010014 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010015 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010016 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010017 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010018 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010020
David Sterba651d4942015-11-27 19:24:16 +010010021 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010022 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010023 if (delay_iput)
10024 btrfs_add_delayed_iput(inode);
10025 else
10026 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010027 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010028 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010029 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010031 btrfs_queue_work(root->fs_info->flush_workers,
10032 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010033 ret++;
10034 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010035 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010036 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010037 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010038 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010040
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010041out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010042 list_for_each_entry_safe(work, next, &works, list) {
10043 list_del_init(&work->list);
10044 btrfs_wait_and_free_delalloc_work(work);
10045 }
10046
Miao Xieeb73c1b2013-05-15 07:48:22 +000010047 if (!list_empty_careful(&splice)) {
10048 spin_lock(&root->delalloc_lock);
10049 list_splice_tail(&splice, &root->delalloc_inodes);
10050 spin_unlock(&root->delalloc_lock);
10051 }
Miao Xie573bfb72014-03-06 13:55:03 +080010052 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010053 return ret;
10054}
10055
10056int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10057{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010058 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010059 int ret;
10060
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010061 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062 return -EROFS;
10063
Miao Xie6c255e62014-03-06 13:55:01 +080010064 ret = __start_delalloc_inodes(root, delay_iput, -1);
10065 if (ret > 0)
10066 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010067 /*
10068 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010069 * we have to make sure the IO is actually started and that
10070 * ordered extents get created before we return
10071 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010072 atomic_inc(&fs_info->async_submit_draining);
10073 while (atomic_read(&fs_info->nr_async_submits) ||
10074 atomic_read(&fs_info->async_delalloc_pages)) {
10075 wait_event(fs_info->async_submit_wait,
10076 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10077 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010078 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010079 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 return ret;
10081}
10082
Miao Xie6c255e62014-03-06 13:55:01 +080010083int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10084 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010085{
10086 struct btrfs_root *root;
10087 struct list_head splice;
10088 int ret;
10089
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010090 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010091 return -EROFS;
10092
10093 INIT_LIST_HEAD(&splice);
10094
Miao Xie573bfb72014-03-06 13:55:03 +080010095 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 spin_lock(&fs_info->delalloc_root_lock);
10097 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010098 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010099 root = list_first_entry(&splice, struct btrfs_root,
10100 delalloc_root);
10101 root = btrfs_grab_fs_root(root);
10102 BUG_ON(!root);
10103 list_move_tail(&root->delalloc_root,
10104 &fs_info->delalloc_roots);
10105 spin_unlock(&fs_info->delalloc_root_lock);
10106
Miao Xie6c255e62014-03-06 13:55:01 +080010107 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010109 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 goto out;
10111
Miao Xie6c255e62014-03-06 13:55:01 +080010112 if (nr != -1) {
10113 nr -= ret;
10114 WARN_ON(nr < 0);
10115 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 spin_lock(&fs_info->delalloc_root_lock);
10117 }
10118 spin_unlock(&fs_info->delalloc_root_lock);
10119
Miao Xie6c255e62014-03-06 13:55:01 +080010120 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 atomic_inc(&fs_info->async_submit_draining);
10122 while (atomic_read(&fs_info->nr_async_submits) ||
10123 atomic_read(&fs_info->async_delalloc_pages)) {
10124 wait_event(fs_info->async_submit_wait,
10125 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10126 atomic_read(&fs_info->async_delalloc_pages) == 0));
10127 }
10128 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010130 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010131 spin_lock(&fs_info->delalloc_root_lock);
10132 list_splice_tail(&splice, &fs_info->delalloc_roots);
10133 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010134 }
Miao Xie573bfb72014-03-06 13:55:03 +080010135 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010136 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010137}
10138
Chris Mason39279cc2007-06-12 06:35:45 -040010139static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10140 const char *symname)
10141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010142 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010143 struct btrfs_trans_handle *trans;
10144 struct btrfs_root *root = BTRFS_I(dir)->root;
10145 struct btrfs_path *path;
10146 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010147 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010148 int err;
10149 int drop_inode = 0;
10150 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010151 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010152 int name_len;
10153 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010154 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010155 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010156 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010157
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010158 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010159 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010160 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010161
Josef Bacik9ed74f22009-09-11 16:12:44 -040010162 /*
10163 * 2 items for inode item and ref
10164 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010165 * 1 item for updating parent inode item
10166 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010167 * 1 item for xattr if selinux is on
10168 */
Filipe Manana9269d122015-12-31 18:16:29 +000010169 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010170 if (IS_ERR(trans))
10171 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010172
Li Zefan581bb052011-04-20 10:06:11 +080010173 err = btrfs_find_free_ino(root, &objectid);
10174 if (err)
10175 goto out_unlock;
10176
Josef Bacikaec74772008-07-24 12:12:38 -040010177 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010178 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10179 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010180 if (IS_ERR(inode)) {
10181 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010182 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010183 }
Chris Mason39279cc2007-06-12 06:35:45 -040010184
Casey Schauflerad19db72011-12-15 10:09:07 -050010185 /*
10186 * If the active LSM wants to access the inode during
10187 * d_instantiate it needs these. Smack checks to see
10188 * if the filesystem supports xattrs by looking at the
10189 * ops vector.
10190 */
10191 inode->i_fop = &btrfs_file_operations;
10192 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010193 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010194 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10195
10196 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10197 if (err)
10198 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010199
Chris Mason39279cc2007-06-12 06:35:45 -040010200 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010201 if (!path) {
10202 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010203 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010204 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010205 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010206 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010207 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010208 datasize = btrfs_file_extent_calc_inline_size(name_len);
10209 err = btrfs_insert_empty_item(trans, root, path, &key,
10210 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010211 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010212 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010213 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010214 }
Chris Mason5f39d392007-10-15 16:14:19 -040010215 leaf = path->nodes[0];
10216 ei = btrfs_item_ptr(leaf, path->slots[0],
10217 struct btrfs_file_extent_item);
10218 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10219 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010220 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010221 btrfs_set_file_extent_encryption(leaf, ei, 0);
10222 btrfs_set_file_extent_compression(leaf, ei, 0);
10223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10224 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10225
Chris Mason39279cc2007-06-12 06:35:45 -040010226 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010227 write_extent_buffer(leaf, symname, ptr, name_len);
10228 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010229 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010230
Chris Mason39279cc2007-06-12 06:35:45 -040010231 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010232 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010233 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010234 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010235 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010236 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010237 /*
10238 * Last step, add directory indexes for our symlink inode. This is the
10239 * last step to avoid extra cleanup of these indexes if an error happens
10240 * elsewhere above.
10241 */
10242 if (!err)
10243 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010244 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010245 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010246 goto out_unlock_inode;
10247 }
10248
10249 unlock_new_inode(inode);
10250 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010251
10252out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010253 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010254 if (drop_inode) {
10255 inode_dec_link_count(inode);
10256 iput(inode);
10257 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010258 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010259 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010260
10261out_unlock_inode:
10262 drop_inode = 1;
10263 unlock_new_inode(inode);
10264 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010265}
Chris Mason16432982008-04-10 10:23:21 -040010266
Josef Bacik0af3d002010-06-21 14:48:16 -040010267static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10268 u64 start, u64 num_bytes, u64 min_size,
10269 loff_t actual_len, u64 *alloc_hint,
10270 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010271{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10274 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010275 struct btrfs_root *root = BTRFS_I(inode)->root;
10276 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010277 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010278 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010279 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010280 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010281 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010282 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010283 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010284
Josef Bacik0af3d002010-06-21 14:48:16 -040010285 if (trans)
10286 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010287 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010288 if (own_trans) {
10289 trans = btrfs_start_transaction(root, 3);
10290 if (IS_ERR(trans)) {
10291 ret = PTR_ERR(trans);
10292 break;
10293 }
Yan Zhengd899e052008-10-30 14:25:28 -040010294 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010295
Byongho Leeee221842015-12-15 01:42:10 +090010296 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010297 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010298 /*
10299 * If we are severely fragmented we could end up with really
10300 * small allocations, so if the allocator is returning small
10301 * chunks lets make its job easier by only searching for those
10302 * sized chunks.
10303 */
10304 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010305 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10306 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010307 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010308 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010309 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010310 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010311 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010312 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010313
Josef Bacik0b670dc2015-09-23 17:11:16 -040010314 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010315 ret = insert_reserved_file_extent(trans, inode,
10316 cur_offset, ins.objectid,
10317 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010318 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010319 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010320 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010321 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010322 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010323 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010324 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010325 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010326 break;
10327 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010328
Chris Masona1ed8352009-09-11 12:27:37 -040010329 btrfs_drop_extent_cache(inode, cur_offset,
10330 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010331
Josef Bacik5dc562c2012-08-17 13:14:17 -040010332 em = alloc_extent_map();
10333 if (!em) {
10334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10335 &BTRFS_I(inode)->runtime_flags);
10336 goto next;
10337 }
10338
10339 em->start = cur_offset;
10340 em->orig_start = cur_offset;
10341 em->len = ins.offset;
10342 em->block_start = ins.objectid;
10343 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010344 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010345 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010346 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010347 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10348 em->generation = trans->transid;
10349
10350 while (1) {
10351 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010352 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010353 write_unlock(&em_tree->lock);
10354 if (ret != -EEXIST)
10355 break;
10356 btrfs_drop_extent_cache(inode, cur_offset,
10357 cur_offset + ins.offset - 1,
10358 0);
10359 }
10360 free_extent_map(em);
10361next:
Yan Zhengd899e052008-10-30 14:25:28 -040010362 num_bytes -= ins.offset;
10363 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010364 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010365
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010366 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010367 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010368 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010369 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010370 (actual_len > inode->i_size) &&
10371 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010372 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010373 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010374 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010375 i_size = cur_offset;
10376 i_size_write(inode, i_size);
10377 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010378 }
10379
Yan Zhengd899e052008-10-30 14:25:28 -040010380 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010381
10382 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010383 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010385 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010386 break;
10387 }
Yan Zhengd899e052008-10-30 14:25:28 -040010388
Josef Bacik0af3d002010-06-21 14:48:16 -040010389 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010390 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010391 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010392 if (cur_offset < end)
10393 btrfs_free_reserved_data_space(inode, cur_offset,
10394 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010395 return ret;
10396}
10397
Josef Bacik0af3d002010-06-21 14:48:16 -040010398int btrfs_prealloc_file_range(struct inode *inode, int mode,
10399 u64 start, u64 num_bytes, u64 min_size,
10400 loff_t actual_len, u64 *alloc_hint)
10401{
10402 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10403 min_size, actual_len, alloc_hint,
10404 NULL);
10405}
10406
10407int btrfs_prealloc_file_range_trans(struct inode *inode,
10408 struct btrfs_trans_handle *trans, int mode,
10409 u64 start, u64 num_bytes, u64 min_size,
10410 loff_t actual_len, u64 *alloc_hint)
10411{
10412 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10413 min_size, actual_len, alloc_hint, trans);
10414}
10415
Chris Masone6dcd2d2008-07-17 12:53:50 -040010416static int btrfs_set_page_dirty(struct page *page)
10417{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010418 return __set_page_dirty_nobuffers(page);
10419}
10420
Al Viro10556cb22011-06-20 19:28:19 -040010421static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010422{
Li Zefanb83cc962010-12-20 16:04:08 +080010423 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010424 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010425
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010426 if (mask & MAY_WRITE &&
10427 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10428 if (btrfs_root_readonly(root))
10429 return -EROFS;
10430 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10431 return -EACCES;
10432 }
Al Viro2830ba72011-06-20 19:16:29 -040010433 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010434}
Chris Mason39279cc2007-06-12 06:35:45 -040010435
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010436static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10437{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010438 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010439 struct btrfs_trans_handle *trans;
10440 struct btrfs_root *root = BTRFS_I(dir)->root;
10441 struct inode *inode = NULL;
10442 u64 objectid;
10443 u64 index;
10444 int ret = 0;
10445
10446 /*
10447 * 5 units required for adding orphan entry
10448 */
10449 trans = btrfs_start_transaction(root, 5);
10450 if (IS_ERR(trans))
10451 return PTR_ERR(trans);
10452
10453 ret = btrfs_find_free_ino(root, &objectid);
10454 if (ret)
10455 goto out;
10456
10457 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010458 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010459 if (IS_ERR(inode)) {
10460 ret = PTR_ERR(inode);
10461 inode = NULL;
10462 goto out;
10463 }
10464
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010465 inode->i_fop = &btrfs_file_operations;
10466 inode->i_op = &btrfs_file_inode_operations;
10467
10468 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010469 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10470
Chris Masonb0d5d102014-09-08 13:08:51 -070010471 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10472 if (ret)
10473 goto out_inode;
10474
10475 ret = btrfs_update_inode(trans, root, inode);
10476 if (ret)
10477 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010478 ret = btrfs_orphan_add(trans, inode);
10479 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010480 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010481
Filipe Manana5762b5c2014-08-01 00:10:32 +010010482 /*
10483 * We set number of links to 0 in btrfs_new_inode(), and here we set
10484 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10485 * through:
10486 *
10487 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10488 */
10489 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010490 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010491 d_tmpfile(dentry, inode);
10492 mark_inode_dirty(inode);
10493
10494out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010495 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010496 if (ret)
10497 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010498 btrfs_balance_delayed_items(fs_info);
10499 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010500 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010501
10502out_inode:
10503 unlock_new_inode(inode);
10504 goto out;
10505
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010506}
10507
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010508static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010509 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010510 .lookup = btrfs_lookup,
10511 .create = btrfs_create,
10512 .unlink = btrfs_unlink,
10513 .link = btrfs_link,
10514 .mkdir = btrfs_mkdir,
10515 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010516 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010517 .symlink = btrfs_symlink,
10518 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010519 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010520 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010521 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010522 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010523 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010524 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010525 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010526};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010527static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010528 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010529 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010530 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010531};
Yan, Zheng76dda932009-09-21 16:00:26 -040010532
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010533static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010534 .llseek = generic_file_llseek,
10535 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010536 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010537 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010538#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010539 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010540#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010541 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010542 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010543};
10544
David Sterba20e55062015-11-19 11:42:28 +010010545static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010546 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010547 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010548 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010549 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010550 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010551 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010552 .set_bit_hook = btrfs_set_bit_hook,
10553 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010554 .merge_extent_hook = btrfs_merge_extent_hook,
10555 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010556};
10557
Chris Mason35054392009-01-21 13:11:13 -050010558/*
10559 * btrfs doesn't support the bmap operation because swapfiles
10560 * use bmap to make a mapping of extents in the file. They assume
10561 * these extents won't change over the life of the file and they
10562 * use the bmap result to do IO directly to the drive.
10563 *
10564 * the btrfs bmap call would return logical addresses that aren't
10565 * suitable for IO and they also will change frequently as COW
10566 * operations happen. So, swapfile + btrfs == corruption.
10567 *
10568 * For now we're avoiding this by dropping bmap.
10569 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010570static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010571 .readpage = btrfs_readpage,
10572 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010573 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010574 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010575 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010576 .invalidatepage = btrfs_invalidatepage,
10577 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010578 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010579 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010580};
10581
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010582static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010583 .readpage = btrfs_readpage,
10584 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010585 .invalidatepage = btrfs_invalidatepage,
10586 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010587};
10588
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010589static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .getattr = btrfs_getattr,
10591 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010592 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010593 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010594 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010595 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010596 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010597 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010598};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010599static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010600 .getattr = btrfs_getattr,
10601 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010602 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010603 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010604 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010605 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010606 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010607};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010608static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010609 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010610 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010611 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010612 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010613 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010614 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010615};
Yan, Zheng76dda932009-09-21 16:00:26 -040010616
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010617const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010618 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010619 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010620};