blob: 4805489984eb55677700e250c78e30bbbae3f4f7 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200319 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200320 btrfs_drop_extent_cache(BTRFS_I(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
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200392static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800393 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 &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200397 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800398 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
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200443 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
444 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400445
Chris Mason42dc7ba2008-12-15 11:44:56 -0500446 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400447again:
448 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300449 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
450 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400451
Chris Masonf03d9301f2009-02-04 09:31:06 -0500452 /*
453 * we don't want to send crud past the end of i_size through
454 * compression, that's just a waste of CPU time. So, if the
455 * end of the file is before the start of our current
456 * requested range of bytes, we bail out to the uncompressed
457 * cleanup code that can deal with all of this.
458 *
459 * It isn't really the fastest way to fix things, but this is a
460 * very uncommon corner.
461 */
462 if (actual_end <= start)
463 goto cleanup_and_bail_uncompressed;
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465 total_compressed = actual_end - start;
466
Shilong Wang4bcbb332014-10-07 18:44:35 -0400467 /*
468 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400469 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400470 */
471 if (total_compressed <= blocksize &&
472 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
473 goto cleanup_and_bail_uncompressed;
474
Chris Masonc8b97812008-10-29 14:49:59 -0400475 /* we want to make sure that amount of ram required to uncompress
476 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500477 * of a compressed extent to 128k. This is a crucial number
478 * because it also controls how easily we can spread reads across
479 * cpus for decompression.
480 *
481 * We also want to make sure the amount of IO required to do
482 * a random read is reasonably small, so we limit the size of
483 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400484 */
485 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000486 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500487 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400488 total_in = 0;
489 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400490
Chris Mason771ed682008-11-06 22:02:51 -0500491 /*
492 * we do compression for mount -o compress and when the
493 * inode has not been flagged as nocompress. This flag can
494 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400495 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800496 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400497 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100498 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800499 if (!pages) {
500 /* just bail out to the uncompressed code */
501 goto cont;
502 }
Chris Mason179e29e2007-11-01 11:28:41 -0400503
Li Zefan261507a02010-12-17 14:21:50 +0800504 if (BTRFS_I(inode)->force_compress)
505 compress_type = BTRFS_I(inode)->force_compress;
506
Chris Mason4adaa612013-03-26 13:07:00 -0400507 /*
508 * we need to call clear_page_dirty_for_io on each
509 * page in the range. Otherwise applications with the file
510 * mmap'd can wander in and change the page contents while
511 * we are compressing them.
512 *
513 * If the compression fails for any reason, we set the pages
514 * dirty again later on.
515 */
516 extent_range_clear_dirty_for_io(inode, start, end);
517 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800518 ret = btrfs_compress_pages(compress_type,
519 inode->i_mapping, start,
520 total_compressed, pages,
521 nr_pages, &nr_pages_ret,
522 &total_in,
523 &total_compressed,
524 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400525
526 if (!ret) {
527 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300528 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 struct page *page = pages[nr_pages_ret - 1];
530 char *kaddr;
531
532 /* zero the tail end of the last page, we might be
533 * sending it down to disk
534 */
535 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800536 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400537 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300538 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800539 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
541 will_compress = 1;
542 }
543 }
Li Zefan560f7d72011-09-08 10:22:01 +0800544cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400545 if (start == 0) {
546 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500547 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400548 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500549 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400550 */
Josef Bacik00361582013-08-14 14:02:47 -0400551 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800552 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400553 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500554 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400555 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000556 total_compressed,
557 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400560 unsigned long clear_flags = EXTENT_DELALLOC |
561 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100562 unsigned long page_error_op;
563
Josef Bacik151a41b2013-07-29 13:22:24 -0400564 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100565 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400566
Chris Mason771ed682008-11-06 22:02:51 -0500567 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 * inline extent creation worked or returned error,
569 * we don't need to create any more async work items.
570 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500571 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800572 extent_clear_unlock_delalloc(inode, start, end, end,
573 NULL, clear_flags,
574 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400575 PAGE_CLEAR_DIRTY |
576 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100577 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400578 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800579 btrfs_free_reserved_data_space_noquota(inode, start,
580 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400581 goto free_pages_out;
582 }
583 }
584
585 if (will_compress) {
586 /*
587 * we aren't doing an inline extent round the compressed size
588 * up to a block size boundary so the allocator does sane
589 * things
590 */
Qu Wenruofda28322013-02-26 08:10:22 +0000591 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400592
593 /*
594 * one last check to make sure the compression is really a
595 * win, compare the page count read with the blocks on disk
596 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300597 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 if (total_compressed >= total_in) {
599 will_compress = 0;
600 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400601 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700602 *num_added += 1;
603
604 /*
605 * The async work queues will take care of doing actual
606 * allocation on disk for these compressed pages, and
607 * will submit them to the elevator.
608 */
609 add_async_extent(async_cow, start, num_bytes,
610 total_compressed, pages, nr_pages_ret,
611 compress_type);
612
613 if (start + num_bytes < end) {
614 start += num_bytes;
615 pages = NULL;
616 cond_resched();
617 goto again;
618 }
619 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400620 }
621 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700622 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400623 /*
624 * the compression code ran but failed to make things smaller,
625 * free any pages it allocated and our page pointer array
626 */
627 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400628 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300629 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
631 kfree(pages);
632 pages = NULL;
633 total_compressed = 0;
634 nr_pages_ret = 0;
635
636 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400637 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500638 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500639 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500640 }
Chris Masonc8b97812008-10-29 14:49:59 -0400641 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500642cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 /*
644 * No compression, but we still need to write the pages in the file
645 * we've been given so far. redirty the locked page if it corresponds
646 * to our extent and set things up for the async work queue to run
647 * cow_file_range to do the normal delalloc dance.
648 */
649 if (page_offset(locked_page) >= start &&
650 page_offset(locked_page) <= end)
651 __set_page_dirty_nobuffers(locked_page);
652 /* unlocked later on in the async handlers */
653
654 if (redirty)
655 extent_range_redirty_for_io(inode, start, end);
656 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
657 BTRFS_COMPRESS_NONE);
658 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500659
Filipe Mananac44f6492014-10-09 21:15:44 +0100660 return;
Chris Mason771ed682008-11-06 22:02:51 -0500661
662free_pages_out:
663 for (i = 0; i < nr_pages_ret; i++) {
664 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300665 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500666 }
Chris Masond3977122009-01-05 21:25:51 -0500667 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500668}
Chris Mason771ed682008-11-06 22:02:51 -0500669
Filipe Manana40ae8372014-10-06 22:14:24 +0100670static void free_async_extent_pages(struct async_extent *async_extent)
671{
672 int i;
673
674 if (!async_extent->pages)
675 return;
676
677 for (i = 0; i < async_extent->nr_pages; i++) {
678 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100680 }
681 kfree(async_extent->pages);
682 async_extent->nr_pages = 0;
683 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500684}
685
686/*
687 * phase two of compressed writeback. This is the ordered portion
688 * of the code, which only gets called in the order the work was
689 * queued. We walk all the async extents created by compress_file_range
690 * and send them down to the disk.
691 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100692static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500693 struct async_cow *async_cow)
694{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400695 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500696 struct async_extent *async_extent;
697 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500698 struct btrfs_key ins;
699 struct extent_map *em;
700 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500701 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500703
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500704again:
Chris Masond3977122009-01-05 21:25:51 -0500705 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500706 async_extent = list_entry(async_cow->extents.next,
707 struct async_extent, list);
708 list_del(&async_extent->list);
709
710 io_tree = &BTRFS_I(inode)->io_tree;
711
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712retry:
Chris Mason771ed682008-11-06 22:02:51 -0500713 /* did the compression code fall back to uncompressed IO? */
714 if (!async_extent->pages) {
715 int page_started = 0;
716 unsigned long nr_written = 0;
717
718 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000719 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100720 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500721
722 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500723 ret = cow_file_range(inode, async_cow->locked_page,
724 async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800727 async_extent->start +
728 async_extent->ram_size - 1,
729 &page_started, &nr_written, 0,
730 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500731
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100732 /* JDM XXX */
733
Chris Mason771ed682008-11-06 22:02:51 -0500734 /*
735 * if page_started, cow_file_range inserted an
736 * inline extent and took care of all the unlocking
737 * and IO for us. Otherwise, we need to submit
738 * all those pages down to the drive.
739 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500741 extent_write_locked_range(io_tree,
742 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500743 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500744 async_extent->ram_size - 1,
745 btrfs_get_extent,
746 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500747 else if (ret)
748 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500749 kfree(async_extent);
750 cond_resched();
751 continue;
752 }
753
754 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100755 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500756
Wang Xiaoguang18513092016-07-25 15:51:40 +0800757 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500758 async_extent->compressed_size,
759 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800760 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100762 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400764 if (ret == -ENOSPC) {
765 unlock_extent(io_tree, async_extent->start,
766 async_extent->start +
767 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800768
769 /*
770 * we need to redirty the pages if we decide to
771 * fallback to uncompressed IO, otherwise we
772 * will not submit these pages down to lower
773 * layers.
774 */
775 extent_range_redirty_for_io(inode,
776 async_extent->start,
777 async_extent->start +
778 async_extent->ram_size - 1);
779
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100780 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400781 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500782 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500783 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000784 /*
785 * here we're doing allocation and writeback of the
786 * compressed pages
787 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800788 em = create_io_em(inode, async_extent->start,
789 async_extent->ram_size, /* len */
790 async_extent->start, /* orig_start */
791 ins.objectid, /* block_start */
792 ins.offset, /* block_len */
793 ins.offset, /* orig_block_len */
794 async_extent->ram_size, /* ram_bytes */
795 async_extent->compress_type,
796 BTRFS_ORDERED_COMPRESSED);
797 if (IS_ERR(em))
798 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800800 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801
Li Zefan261507a02010-12-17 14:21:50 +0800802 ret = btrfs_add_ordered_extent_compress(inode,
803 async_extent->start,
804 ins.objectid,
805 async_extent->ram_size,
806 ins.offset,
807 BTRFS_ORDERED_COMPRESSED,
808 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100809 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200810 btrfs_drop_extent_cache(BTRFS_I(inode),
811 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100812 async_extent->start +
813 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100815 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400816 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500817
Chris Mason771ed682008-11-06 22:02:51 -0500818 /*
819 * clear dirty, set writeback and unlock the pages.
820 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400821 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400822 async_extent->start +
823 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800824 async_extent->start +
825 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400826 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
827 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400828 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500829 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500830 async_extent->start,
831 async_extent->ram_size,
832 ins.objectid,
833 ins.offset, async_extent->pages,
834 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100835 if (ret) {
836 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
837 struct page *p = async_extent->pages[0];
838 const u64 start = async_extent->start;
839 const u64 end = start + async_extent->ram_size - 1;
840
841 p->mapping = inode->i_mapping;
842 tree->ops->writepage_end_io_hook(p, start, end,
843 NULL, 0);
844 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800845 extent_clear_unlock_delalloc(inode, start, end, end,
846 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100847 PAGE_END_WRITEBACK |
848 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100849 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 }
Chris Mason771ed682008-11-06 22:02:51 -0500851 alloc_hint = ins.objectid + ins.offset;
852 kfree(async_extent);
853 cond_resched();
854 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100855 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400857 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400858 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100859out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400860 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 async_extent->start +
862 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800863 async_extent->start +
864 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400865 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400866 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
867 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100868 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
869 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100870 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100871 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500873}
874
Josef Bacik4b46fce2010-05-23 11:00:55 -0400875static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
876 u64 num_bytes)
877{
878 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
879 struct extent_map *em;
880 u64 alloc_hint = 0;
881
882 read_lock(&em_tree->lock);
883 em = search_extent_mapping(em_tree, start, num_bytes);
884 if (em) {
885 /*
886 * if block start isn't an actual block number then find the
887 * first block in this inode and use that as a hint. If that
888 * block is also bogus then just don't worry about it.
889 */
890 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
891 free_extent_map(em);
892 em = search_extent_mapping(em_tree, 0, 0);
893 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
894 alloc_hint = em->block_start;
895 if (em)
896 free_extent_map(em);
897 } else {
898 alloc_hint = em->block_start;
899 free_extent_map(em);
900 }
901 }
902 read_unlock(&em_tree->lock);
903
904 return alloc_hint;
905}
906
Chris Mason771ed682008-11-06 22:02:51 -0500907/*
908 * when extent_io.c finds a delayed allocation range in the file,
909 * the call backs end up in this code. The basic idea is to
910 * allocate extents on disk for the range, and create ordered data structs
911 * in ram to track those extents.
912 *
913 * locked_page is the page that writepage had locked already. We use
914 * it to make sure we don't do extra locks or unlocks.
915 *
916 * *page_started is set to one if we unlock locked_page and do everything
917 * required to start IO on it. It may be clean and already done with
918 * IO when we return.
919 */
Josef Bacik00361582013-08-14 14:02:47 -0400920static noinline int cow_file_range(struct inode *inode,
921 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800922 u64 start, u64 end, u64 delalloc_end,
923 int *page_started, unsigned long *nr_written,
924 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500928 u64 alloc_hint = 0;
929 u64 num_bytes;
930 unsigned long ram_size;
931 u64 disk_num_bytes;
932 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400933 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500934 struct btrfs_key ins;
935 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500936 int ret = 0;
937
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200938 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400939 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500940 ret = -EINVAL;
941 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400942 }
Chris Mason771ed682008-11-06 22:02:51 -0500943
Qu Wenruofda28322013-02-26 08:10:22 +0000944 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500945 num_bytes = max(blocksize, num_bytes);
946 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500947
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200948 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400949
Chris Mason771ed682008-11-06 22:02:51 -0500950 if (start == 0) {
951 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800952 ret = cow_file_range_inline(root, inode, start, end, 0,
953 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500954 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800955 extent_clear_unlock_delalloc(inode, start, end,
956 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400958 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400959 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
960 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800961 btrfs_free_reserved_data_space_noquota(inode, start,
962 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500963 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300964 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500965 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500966 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500969 }
970 }
Chris Masonc8b97812008-10-29 14:49:59 -0400971
972 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400973 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400974
Josef Bacik4b46fce2010-05-23 11:00:55 -0400975 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200976 btrfs_drop_extent_cache(BTRFS_I(inode), start,
977 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400978
Chris Masond3977122009-01-05 21:25:51 -0500979 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400980 unsigned long op;
981
Josef Bacik287a0ab2010-03-19 18:07:23 +0000982 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800983 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400984 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800985 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400986 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500988
Chris Mason771ed682008-11-06 22:02:51 -0500989 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800990 em = create_io_em(inode, start, ins.offset, /* len */
991 start, /* orig_start */
992 ins.objectid, /* block_start */
993 ins.offset, /* block_len */
994 ins.offset, /* orig_block_len */
995 ram_size, /* ram_bytes */
996 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800997 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800998 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000999 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001000 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001001
Chris Mason98d20f62008-04-14 09:46:10 -04001002 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001004 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001005 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001006 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Yan Zheng17d217f2008-12-12 10:03:38 -05001008 if (root->root_key.objectid ==
1009 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1010 ret = btrfs_reloc_clone_csums(inode, start,
1011 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001012 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001013 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001014 }
1015
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001016 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001017
Chris Masond3977122009-01-05 21:25:51 -05001018 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001019 break;
Chris Masond3977122009-01-05 21:25:51 -05001020
Chris Masonc8b97812008-10-29 14:49:59 -04001021 /* we're not doing compressed IO, don't unlock the first
1022 * page (which the caller expects to stay locked), don't
1023 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001024 *
1025 * Do set the Private2 bit so we know this page was properly
1026 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001027 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001028 op = unlock ? PAGE_UNLOCK : 0;
1029 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001030
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001032 start + ram_size - 1,
1033 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001034 EXTENT_LOCKED | EXTENT_DELALLOC,
1035 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001036 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001037 num_bytes -= cur_alloc_size;
1038 alloc_hint = ins.objectid + ins.offset;
1039 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001040 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001041out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001042 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001043
Filipe Mananad9f85962014-08-25 10:43:00 +01001044out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001045 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001046out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001047 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001048 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001049out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001050 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1051 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001052 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1053 EXTENT_DELALLOC | EXTENT_DEFRAG,
1054 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1055 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001057}
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Chris Mason771ed682008-11-06 22:02:51 -05001059/*
1060 * work queue call back to started compression on a file and pages
1061 */
1062static noinline void async_cow_start(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 int num_added = 0;
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 compress_file_range(async_cow->inode, async_cow->locked_page,
1069 async_cow->start, async_cow->end, async_cow,
1070 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001072 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 }
Chris Mason771ed682008-11-06 22:02:51 -05001075}
1076
1077/*
1078 * work queue call back to submit previously compressed pages
1079 */
1080static noinline void async_cow_submit(struct btrfs_work *work)
1081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001082 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001083 struct async_cow *async_cow;
1084 struct btrfs_root *root;
1085 unsigned long nr_pages;
1086
1087 async_cow = container_of(work, struct async_cow, work);
1088
1089 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001090 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1092 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001093
David Sterbaee863952015-02-16 19:41:40 +01001094 /*
1095 * atomic_sub_return implies a barrier for waitqueue_active
1096 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001097 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001098 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001099 waitqueue_active(&fs_info->async_submit_wait))
1100 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001101
Chris Masond3977122009-01-05 21:25:51 -05001102 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001103 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001104}
Chris Masonc8b97812008-10-29 14:49:59 -04001105
Chris Mason771ed682008-11-06 22:02:51 -05001106static noinline void async_cow_free(struct btrfs_work *work)
1107{
1108 struct async_cow *async_cow;
1109 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001110 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001111 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001112 kfree(async_cow);
1113}
1114
1115static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1116 u64 start, u64 end, int *page_started,
1117 unsigned long *nr_written)
1118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001120 struct async_cow *async_cow;
1121 struct btrfs_root *root = BTRFS_I(inode)->root;
1122 unsigned long nr_pages;
1123 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001124
Chris Masona3429ab2009-10-08 12:30:20 -04001125 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1126 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001127 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001128 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001129 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001130 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow->root = root;
1132 async_cow->locked_page = locked_page;
1133 async_cow->start = start;
1134
Wang Shilongf79707b2014-07-17 11:44:09 +08001135 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001136 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001137 cur_end = end;
1138 else
Byongho Leeee221842015-12-15 01:42:10 +09001139 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001140
1141 async_cow->end = cur_end;
1142 INIT_LIST_HEAD(&async_cow->extents);
1143
Liu Bo9e0af232014-08-15 23:36:53 +08001144 btrfs_init_work(&async_cow->work,
1145 btrfs_delalloc_helper,
1146 async_cow_start, async_cow_submit,
1147 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001148
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 nr_pages = (cur_end - start + PAGE_SIZE) >>
1150 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001151 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001152
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001154
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001155 while (atomic_read(&fs_info->async_submit_draining) &&
1156 atomic_read(&fs_info->async_delalloc_pages)) {
1157 wait_event(fs_info->async_submit_wait,
1158 (atomic_read(&fs_info->async_delalloc_pages) ==
1159 0));
Chris Mason771ed682008-11-06 22:02:51 -05001160 }
1161
1162 *nr_written += nr_pages;
1163 start = cur_end + 1;
1164 }
1165 *page_started = 1;
1166 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001167}
1168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001169static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001170 u64 bytenr, u64 num_bytes)
1171{
1172 int ret;
1173 struct btrfs_ordered_sum *sums;
1174 LIST_HEAD(list);
1175
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001177 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001178 if (ret == 0 && list_empty(&list))
1179 return 0;
1180
1181 while (!list_empty(&list)) {
1182 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1183 list_del(&sums->list);
1184 kfree(sums);
1185 }
1186 return 1;
1187}
1188
Chris Masond352ac62008-09-29 15:18:18 -04001189/*
1190 * when nowcow writeback call back. This checks for snapshots or COW copies
1191 * of the extents that exist in the file, and COWs the file as required.
1192 *
1193 * If no cow copies or snapshots exist, we write directly to the existing
1194 * blocks on disk
1195 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001196static noinline int run_delalloc_nocow(struct inode *inode,
1197 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001198 u64 start, u64 end, int *page_started, int force,
1199 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001207 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 u64 cow_start;
1209 u64 cur_offset;
1210 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001211 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 u64 disk_bytenr;
1213 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001214 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001215 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001218 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 int nocow;
1220 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001221 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001222 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
1224 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001225 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001226 extent_clear_unlock_delalloc(inode, start, end, end,
1227 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001229 EXTENT_DO_ACCOUNTING |
1230 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001231 PAGE_CLEAR_DIRTY |
1232 PAGE_SET_WRITEBACK |
1233 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001234 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001235 }
Li Zefan82d59022011-04-20 10:33:24 +08001236
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001237 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 cow_start = (u64)-1;
1240 cur_offset = start;
1241 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001242 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001259 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0)
1262 break;
1263 leaf = path->nodes[0];
1264 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 nocow = 0;
1267 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001268 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Filipe Manana1d512cb2015-11-09 00:33:58 +00001271 if (found_key.objectid > ino)
1272 break;
1273 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1274 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1275 path->slots[0]++;
1276 goto next_slot;
1277 }
1278 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 found_key.offset > end)
1280 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 if (found_key.offset > cur_offset) {
1283 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001284 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 goto out_check;
1286 }
Chris Masonc31f8832008-01-08 15:46:31 -05001287
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 fi = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_file_extent_item);
1290 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Josef Bacikcc95bef2013-04-04 14:31:27 -04001292 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001293 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1294 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 extent_end = found_key.offset +
1298 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001299 disk_num_bytes =
1300 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (extent_end <= start) {
1302 path->slots[0]++;
1303 goto next_slot;
1304 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 if (disk_bytenr == 0)
1306 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (btrfs_file_extent_compression(leaf, fi) ||
1308 btrfs_file_extent_encryption(leaf, fi) ||
1309 btrfs_file_extent_other_encoding(leaf, fi))
1310 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1312 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001313 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001315 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 found_key.offset -
1317 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001320 disk_bytenr += cur_offset - found_key.offset;
1321 num_bytes = min(end + 1, extent_end) - cur_offset;
1322 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001323 * if there are pending snapshots for this root,
1324 * we fall into common COW way.
1325 */
1326 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001327 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001328 if (!err)
1329 goto out_check;
1330 }
1331 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 * force cow if csum exists in the range.
1333 * this ensure that csum for a given extent are
1334 * either valid or do not exist.
1335 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001336 if (csum_exist_in_range(fs_info, disk_bytenr,
1337 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001339 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001340 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 nocow = 1;
1342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001344 btrfs_file_extent_inline_len(leaf,
1345 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001346 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001347 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 } else {
1349 BUG_ON(1);
1350 }
1351out_check:
1352 if (extent_end <= start) {
1353 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001354 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001355 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001356 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001357 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 goto next_slot;
1359 }
1360 if (!nocow) {
1361 if (cow_start == (u64)-1)
1362 cow_start = cur_offset;
1363 cur_offset = extent_end;
1364 if (cur_offset > end)
1365 break;
1366 path->slots[0]++;
1367 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001368 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001372 ret = cow_file_range(inode, locked_page,
1373 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001374 end, page_started, nr_written, 1,
1375 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001376 if (ret) {
1377 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001378 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001379 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001380 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001381 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001382 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001383 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
Yan Zhengd899e052008-10-30 14:25:28 -04001387 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001388 u64 orig_start = found_key.offset - extent_offset;
1389
1390 em = create_io_em(inode, cur_offset, num_bytes,
1391 orig_start,
1392 disk_bytenr, /* block_start */
1393 num_bytes, /* block_len */
1394 disk_num_bytes, /* orig_block_len */
1395 ram_bytes, BTRFS_COMPRESS_NONE,
1396 BTRFS_ORDERED_PREALLOC);
1397 if (IS_ERR(em)) {
1398 if (!nolock && nocow)
1399 btrfs_end_write_no_snapshoting(root);
1400 if (nocow)
1401 btrfs_dec_nocow_writers(fs_info,
1402 disk_bytenr);
1403 ret = PTR_ERR(em);
1404 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001406 free_extent_map(em);
1407 }
1408
1409 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001410 type = BTRFS_ORDERED_PREALLOC;
1411 } else {
1412 type = BTRFS_ORDERED_NOCOW;
1413 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001414
1415 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001416 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001417 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001419 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001420
Yan, Zhengefa56462010-05-16 10:49:59 -04001421 if (root->root_key.objectid ==
1422 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1423 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1424 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001425 if (ret) {
1426 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001427 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001430 }
1431
Josef Bacikc2790a22013-07-29 11:20:47 -04001432 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001433 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001434 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001435 EXTENT_DELALLOC |
1436 EXTENT_CLEAR_DATA_RESV,
1437 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1438
Wang Shilonge9894fd2014-03-27 11:12:25 +08001439 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001440 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 cur_offset = extent_end;
1442 if (cur_offset > end)
1443 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001445 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001446
Josef Bacik17ca04a2012-05-31 15:58:55 -04001447 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001449 cur_offset = end;
1450 }
1451
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001453 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1454 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001455 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 }
1458
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001460 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001461 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001462 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001463 EXTENT_DELALLOC | EXTENT_DEFRAG |
1464 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1465 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001466 PAGE_SET_WRITEBACK |
1467 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001468 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001470}
1471
Wang Shilong47059d92014-07-03 18:22:07 +08001472static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1473{
1474
1475 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1476 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1477 return 0;
1478
1479 /*
1480 * @defrag_bytes is a hint value, no spinlock held here,
1481 * if is not zero, it means the file is defragging.
1482 * Force cow if given extent needs to be defragged.
1483 */
1484 if (BTRFS_I(inode)->defrag_bytes &&
1485 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1486 EXTENT_DEFRAG, 0, NULL))
1487 return 1;
1488
1489 return 0;
1490}
1491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * extent_io.c call back to do delayed allocation processing
1494 */
Chris Masonc8b97812008-10-29 14:49:59 -04001495static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001496 u64 start, u64 end, int *page_started,
1497 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001498{
Chris Masonbe20aa92007-12-17 20:14:01 -05001499 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001500 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001501
Wang Shilong47059d92014-07-03 18:22:07 +08001502 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001503 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001504 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001505 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001506 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001507 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001508 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001509 ret = cow_file_range(inode, locked_page, start, end, end,
1510 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001511 } else {
1512 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1513 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001514 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001515 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001516 }
Chris Masonb888db22007-08-27 16:49:44 -04001517 return ret;
1518}
1519
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001520static void btrfs_split_extent_hook(struct inode *inode,
1521 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001523 u64 size;
1524
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001525 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001527 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528
Josef Bacikdcab6a32015-02-11 15:08:59 -05001529 size = orig->end - orig->start + 1;
1530 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001531 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001532 u64 new_size;
1533
1534 /*
Josef Bacikba117212015-03-13 15:01:24 -04001535 * See the explanation in btrfs_merge_extent_hook, the same
1536 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001537 */
1538 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001539 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001540 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001541 num_extents += count_max_extents(new_size);
1542 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001543 return;
1544 }
1545
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 spin_lock(&BTRFS_I(inode)->lock);
1547 BTRFS_I(inode)->outstanding_extents++;
1548 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549}
1550
1551/*
1552 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1553 * extents so we can keep track of new extents that are just merged onto old
1554 * extents, such as when we are doing sequential writes, so we can properly
1555 * account for the metadata space we'll need.
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_merge_extent_hook(struct inode *inode,
1558 struct extent_state *new,
1559 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001560{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001561 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001562 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001563
Josef Bacik9ed74f22009-09-11 16:12:44 -04001564 /* not delalloc, ignore it */
1565 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001566 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001567
Josef Bacik8461a3d2015-03-13 15:12:08 -04001568 if (new->start > other->start)
1569 new_size = new->end - other->start + 1;
1570 else
1571 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001572
1573 /* we're not bigger than the max, unreserve the space and go */
1574 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents--;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 return;
1579 }
1580
1581 /*
Josef Bacikba117212015-03-13 15:01:24 -04001582 * We have to add up either side to figure out how many extents were
1583 * accounted for before we merged into one big extent. If the number of
1584 * extents we accounted for is <= the amount we need for the new range
1585 * then we can return, otherwise drop. Think of it like this
1586 *
1587 * [ 4k][MAX_SIZE]
1588 *
1589 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1590 * need 2 outstanding extents, on one side we have 1 and the other side
1591 * we have 1 so they are == and we can return. But in this case
1592 *
1593 * [MAX_SIZE+4k][MAX_SIZE+4k]
1594 *
1595 * Each range on their own accounts for 2 extents, but merged together
1596 * they are only 3 extents worth of accounting, so we need to drop in
1597 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001598 */
Josef Bacikba117212015-03-13 15:01:24 -04001599 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001600 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001601 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001602 num_extents += count_max_extents(old_size);
1603 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 return;
1605
Josef Bacik9e0baf62011-07-15 15:16:44 +00001606 spin_lock(&BTRFS_I(inode)->lock);
1607 BTRFS_I(inode)->outstanding_extents--;
1608 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609}
1610
Miao Xieeb73c1b2013-05-15 07:48:22 +00001611static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1612 struct inode *inode)
1613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001614 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1615
Miao Xieeb73c1b2013-05-15 07:48:22 +00001616 spin_lock(&root->delalloc_lock);
1617 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1618 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1619 &root->delalloc_inodes);
1620 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1621 &BTRFS_I(inode)->runtime_flags);
1622 root->nr_delalloc_inodes++;
1623 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001625 BUG_ON(!list_empty(&root->delalloc_root));
1626 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001627 &fs_info->delalloc_roots);
1628 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001629 }
1630 }
1631 spin_unlock(&root->delalloc_lock);
1632}
1633
1634static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1635 struct inode *inode)
1636{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1638
Miao Xieeb73c1b2013-05-15 07:48:22 +00001639 spin_lock(&root->delalloc_lock);
1640 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1641 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1642 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1643 &BTRFS_I(inode)->runtime_flags);
1644 root->nr_delalloc_inodes--;
1645 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001647 BUG_ON(list_empty(&root->delalloc_root));
1648 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001649 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001650 }
1651 }
1652 spin_unlock(&root->delalloc_lock);
1653}
1654
Chris Masond352ac62008-09-29 15:18:18 -04001655/*
1656 * extent_io.c set_bit_hook, used to track delayed allocation
1657 * bytes in this file, and to maintain the list of inodes that
1658 * have pending delalloc work to be done.
1659 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001660static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001661 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001662{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1665
Wang Shilong47059d92014-07-03 18:22:07 +08001666 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1667 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001668 /*
1669 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001670 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001671 * bit, which is only set or cleared with irqs on
1672 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001673 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001674 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001675 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001676 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001677
Josef Bacik9e0baf62011-07-15 15:16:44 +00001678 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001679 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001680 } else {
1681 spin_lock(&BTRFS_I(inode)->lock);
1682 BTRFS_I(inode)->outstanding_extents++;
1683 spin_unlock(&BTRFS_I(inode)->lock);
1684 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001685
Josef Bacik6a3891c2015-03-16 17:38:52 -04001686 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001688 return;
1689
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1691 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001692 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001693 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001694 if (*bits & EXTENT_DEFRAG)
1695 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001696 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001697 &BTRFS_I(inode)->runtime_flags))
1698 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001699 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001700 }
Chris Mason291d6732008-01-29 15:55:23 -05001701}
1702
Chris Masond352ac62008-09-29 15:18:18 -04001703/*
1704 * extent_io.c clear_bit_hook, see set_bit_hook for why
1705 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001706static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001707 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001708 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001711 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001712 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001713
1714 spin_lock(&BTRFS_I(inode)->lock);
1715 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1716 BTRFS_I(inode)->defrag_bytes -= len;
1717 spin_unlock(&BTRFS_I(inode)->lock);
1718
Chris Mason75eff682008-12-15 15:54:40 -05001719 /*
1720 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001721 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001722 * bit, which is only set or cleared with irqs on
1723 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001724 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001725 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001726 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Chris Masonbcbfce82008-04-22 13:26:47 -04001727
Josef Bacik9e0baf62011-07-15 15:16:44 +00001728 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001729 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001730 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1731 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001732 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001733 spin_unlock(&BTRFS_I(inode)->lock);
1734 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001735
Josef Bacikb6d08f02013-09-27 14:57:43 -04001736 /*
1737 * We don't reserve metadata space for space cache inodes so we
1738 * don't need to call dellalloc_release_metadata if there is an
1739 * error.
1740 */
1741 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001742 root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02001743 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001744
Josef Bacik6a3891c2015-03-16 17:38:52 -04001745 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001747 return;
1748
Josef Bacik0cb59c92010-07-02 12:14:14 -04001749 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001750 && do_list && !(state->state & EXTENT_NORESERVE)
1751 && (*bits & (EXTENT_DO_ACCOUNTING |
1752 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001753 btrfs_free_reserved_data_space_noquota(inode,
1754 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001755
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001756 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1757 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001758 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001759 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001760 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 &BTRFS_I(inode)->runtime_flags))
1763 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001764 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001765 }
Chris Mason291d6732008-01-29 15:55:23 -05001766}
1767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
1769 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1770 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001771 *
1772 * return 1 if page cannot be merged to bio
1773 * return 0 if page can be merged to bio
1774 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001775 */
Mike Christie81a75f672016-06-05 14:31:54 -05001776int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001777 size_t size, struct bio *bio,
1778 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001780 struct inode *inode = page->mapping->host;
1781 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001782 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001783 u64 length = 0;
1784 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001785 int ret;
1786
Chris Mason771ed682008-11-06 22:02:51 -05001787 if (bio_flags & EXTENT_BIO_COMPRESSED)
1788 return 0;
1789
Kent Overstreet4f024f32013-10-11 15:44:27 -07001790 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001791 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001792 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1793 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001794 if (ret < 0)
1795 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001796 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001797 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001798 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001799}
1800
Chris Masond352ac62008-09-29 15:18:18 -04001801/*
1802 * in order to insert checksums into the metadata in large chunks,
1803 * we wait until bio submission time. All the pages in the bio are
1804 * checksummed and sums are attached onto the ordered extent record.
1805 *
1806 * At IO completion time the cums attached on the ordered extent record
1807 * are inserted into the btree
1808 */
Mike Christie81a75f672016-06-05 14:31:54 -05001809static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1810 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001811 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001812{
Chris Mason065631f2008-02-20 12:07:25 -05001813 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001814
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001815 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001816 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001817 return 0;
1818}
Chris Masone0156402008-04-16 11:15:20 -04001819
Chris Mason4a69a412008-11-06 22:03:00 -05001820/*
1821 * in order to insert checksums into the metadata in large chunks,
1822 * we wait until bio submission time. All the pages in the bio are
1823 * checksummed and sums are attached onto the ordered extent record.
1824 *
1825 * At IO completion time the cums attached on the ordered extent record
1826 * are inserted into the btree
1827 */
Mike Christie81a75f672016-06-05 14:31:54 -05001828static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001829 int mirror_num, unsigned long bio_flags,
1830 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001831{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001833 int ret;
1834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001835 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001836 if (ret) {
1837 bio->bi_error = ret;
1838 bio_endio(bio);
1839 }
Stefan Behrens61891922012-11-05 18:51:52 +01001840 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/*
Chris Masoncad321a2008-12-17 14:51:42 -05001844 * extent_io.c submission hook. This does the right thing for csum calculation
1845 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001846 */
Mike Christie81a75f672016-06-05 14:31:54 -05001847static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001848 int mirror_num, unsigned long bio_flags,
1849 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001850{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001851 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001852 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301853 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001854 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001855 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001856 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001857
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001858 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001859
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001860 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301861 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001862
Mike Christie37226b22016-06-05 14:31:52 -05001863 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001864 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001865 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001866 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001867
Chris Masond20f7042008-12-08 16:58:54 -05001868 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001869 ret = btrfs_submit_compressed_read(inode, bio,
1870 mirror_num,
1871 bio_flags);
1872 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001873 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001874 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001875 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001876 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001877 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001878 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001879 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001880 /* csum items have already been cloned */
1881 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1882 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001883 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001884 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1885 bio_flags, bio_offset,
1886 __btrfs_submit_bio_start,
1887 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001888 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001889 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001890 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001891 if (ret)
1892 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001893 }
1894
Chris Mason0b86a832008-03-24 15:01:56 -04001895mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001896 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001897
1898out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001899 if (ret < 0) {
1900 bio->bi_error = ret;
1901 bio_endio(bio);
1902 }
Stefan Behrens61891922012-11-05 18:51:52 +01001903 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001904}
Chris Mason6885f302008-02-20 16:11:05 -05001905
Chris Masond352ac62008-09-29 15:18:18 -04001906/*
1907 * given a list of ordered sums record them in the inode. This happens
1908 * at IO completion time based on sums calculated at bio submission time.
1909 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001910static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001911 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001912{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001913 struct btrfs_ordered_sum *sum;
1914
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001915 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001916 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001917 btrfs_csum_file_blocks(trans,
1918 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001919 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001920 }
1921 return 0;
1922}
1923
Josef Bacik2ac55d42010-02-03 19:33:23 +00001924int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001925 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001926{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001927 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001928 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001929 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001933struct btrfs_writepage_fixup {
1934 struct page *page;
1935 struct btrfs_work work;
1936};
1937
Christoph Hellwigb2950862008-12-02 09:54:17 -05001938static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001939{
1940 struct btrfs_writepage_fixup *fixup;
1941 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001942 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001943 struct page *page;
1944 struct inode *inode;
1945 u64 page_start;
1946 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001947 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001948
1949 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1950 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001951again:
Chris Mason247e7432008-07-17 12:53:51 -04001952 lock_page(page);
1953 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1954 ClearPageChecked(page);
1955 goto out_page;
1956 }
1957
1958 inode = page->mapping->host;
1959 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001961
David Sterbaff13db42015-12-03 14:30:40 +01001962 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001963 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001964
1965 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001966 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001967 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001968
Nikolay Borisova776c6f2017-02-20 13:50:49 +02001969 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001971 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001972 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1973 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001974 unlock_page(page);
1975 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001976 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001977 goto again;
1978 }
Chris Mason247e7432008-07-17 12:53:51 -04001979
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001980 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001982 if (ret) {
1983 mapping_set_error(page->mapping, ret);
1984 end_extent_writepage(page, ret, page_start, page_end);
1985 ClearPageChecked(page);
1986 goto out;
1987 }
1988
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001989 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1990 0);
Chris Mason247e7432008-07-17 12:53:51 -04001991 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001992 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001993out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001994 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1995 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001996out_page:
1997 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001998 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001999 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002000}
2001
2002/*
2003 * There are a few paths in the higher layers of the kernel that directly
2004 * set the page dirty bit without asking the filesystem if it is a
2005 * good idea. This causes problems because we want to make sure COW
2006 * properly happens and the data=ordered rules are followed.
2007 *
Chris Masonc8b97812008-10-29 14:49:59 -04002008 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002009 * hasn't been properly setup for IO. We kick off an async process
2010 * to fix it up. The async helper will wait for ordered extents, set
2011 * the delalloc bit and make it safe to write the page.
2012 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002013static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002014{
2015 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002017 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002018
Chris Mason8b62b722009-09-02 16:53:46 -04002019 /* this page is properly in the ordered list */
2020 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002021 return 0;
2022
2023 if (PageChecked(page))
2024 return -EAGAIN;
2025
2026 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2027 if (!fixup)
2028 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002029
Chris Mason247e7432008-07-17 12:53:51 -04002030 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002031 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002032 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2033 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002034 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002035 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002036 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002037}
2038
Yan Zhengd899e052008-10-30 14:25:28 -04002039static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2040 struct inode *inode, u64 file_pos,
2041 u64 disk_bytenr, u64 disk_num_bytes,
2042 u64 num_bytes, u64 ram_bytes,
2043 u8 compression, u8 encryption,
2044 u16 other_encoding, int extent_type)
2045{
2046 struct btrfs_root *root = BTRFS_I(inode)->root;
2047 struct btrfs_file_extent_item *fi;
2048 struct btrfs_path *path;
2049 struct extent_buffer *leaf;
2050 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002051 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002052 int ret;
2053
2054 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002055 if (!path)
2056 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002057
Chris Masona1ed8352009-09-11 12:27:37 -04002058 /*
2059 * we may be replacing one extent in the tree with another.
2060 * The new extent is pinned in the extent map, and we don't want
2061 * to drop it from the cache until it is completely in the btree.
2062 *
2063 * So, tell btrfs_drop_extents to leave this extent in the cache.
2064 * the caller is expected to unpin it and allow it to be merged
2065 * with the others.
2066 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002067 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2068 file_pos + num_bytes, NULL, 0,
2069 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002070 if (ret)
2071 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002072
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002073 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002074 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002075 ins.offset = file_pos;
2076 ins.type = BTRFS_EXTENT_DATA_KEY;
2077
2078 path->leave_spinning = 1;
2079 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2080 sizeof(*fi));
2081 if (ret)
2082 goto out;
2083 }
Yan Zhengd899e052008-10-30 14:25:28 -04002084 leaf = path->nodes[0];
2085 fi = btrfs_item_ptr(leaf, path->slots[0],
2086 struct btrfs_file_extent_item);
2087 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2088 btrfs_set_file_extent_type(leaf, fi, extent_type);
2089 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2090 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2091 btrfs_set_file_extent_offset(leaf, fi, 0);
2092 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2093 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2094 btrfs_set_file_extent_compression(leaf, fi, compression);
2095 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2096 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002097
Yan Zhengd899e052008-10-30 14:25:28 -04002098 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002099 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002100
2101 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002102
2103 ins.objectid = disk_bytenr;
2104 ins.offset = disk_num_bytes;
2105 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002106 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002107 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002108 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002109 * Release the reserved range from inode dirty range map, as it is
2110 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002111 */
2112 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002113out:
Yan Zhengd899e052008-10-30 14:25:28 -04002114 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002115
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002116 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002117}
2118
Liu Bo38c227d2013-01-29 03:18:40 +00002119/* snapshot-aware defrag */
2120struct sa_defrag_extent_backref {
2121 struct rb_node node;
2122 struct old_sa_defrag_extent *old;
2123 u64 root_id;
2124 u64 inum;
2125 u64 file_pos;
2126 u64 extent_offset;
2127 u64 num_bytes;
2128 u64 generation;
2129};
2130
2131struct old_sa_defrag_extent {
2132 struct list_head list;
2133 struct new_sa_defrag_extent *new;
2134
2135 u64 extent_offset;
2136 u64 bytenr;
2137 u64 offset;
2138 u64 len;
2139 int count;
2140};
2141
2142struct new_sa_defrag_extent {
2143 struct rb_root root;
2144 struct list_head head;
2145 struct btrfs_path *path;
2146 struct inode *inode;
2147 u64 file_pos;
2148 u64 len;
2149 u64 bytenr;
2150 u64 disk_len;
2151 u8 compress_type;
2152};
2153
2154static int backref_comp(struct sa_defrag_extent_backref *b1,
2155 struct sa_defrag_extent_backref *b2)
2156{
2157 if (b1->root_id < b2->root_id)
2158 return -1;
2159 else if (b1->root_id > b2->root_id)
2160 return 1;
2161
2162 if (b1->inum < b2->inum)
2163 return -1;
2164 else if (b1->inum > b2->inum)
2165 return 1;
2166
2167 if (b1->file_pos < b2->file_pos)
2168 return -1;
2169 else if (b1->file_pos > b2->file_pos)
2170 return 1;
2171
2172 /*
2173 * [------------------------------] ===> (a range of space)
2174 * |<--->| |<---->| =============> (fs/file tree A)
2175 * |<---------------------------->| ===> (fs/file tree B)
2176 *
2177 * A range of space can refer to two file extents in one tree while
2178 * refer to only one file extent in another tree.
2179 *
2180 * So we may process a disk offset more than one time(two extents in A)
2181 * and locate at the same extent(one extent in B), then insert two same
2182 * backrefs(both refer to the extent in B).
2183 */
2184 return 0;
2185}
2186
2187static void backref_insert(struct rb_root *root,
2188 struct sa_defrag_extent_backref *backref)
2189{
2190 struct rb_node **p = &root->rb_node;
2191 struct rb_node *parent = NULL;
2192 struct sa_defrag_extent_backref *entry;
2193 int ret;
2194
2195 while (*p) {
2196 parent = *p;
2197 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2198
2199 ret = backref_comp(backref, entry);
2200 if (ret < 0)
2201 p = &(*p)->rb_left;
2202 else
2203 p = &(*p)->rb_right;
2204 }
2205
2206 rb_link_node(&backref->node, parent, p);
2207 rb_insert_color(&backref->node, root);
2208}
2209
2210/*
2211 * Note the backref might has changed, and in this case we just return 0.
2212 */
2213static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2214 void *ctx)
2215{
2216 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002217 struct old_sa_defrag_extent *old = ctx;
2218 struct new_sa_defrag_extent *new = old->new;
2219 struct btrfs_path *path = new->path;
2220 struct btrfs_key key;
2221 struct btrfs_root *root;
2222 struct sa_defrag_extent_backref *backref;
2223 struct extent_buffer *leaf;
2224 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002226 int slot;
2227 int ret;
2228 u64 extent_offset;
2229 u64 num_bytes;
2230
2231 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002232 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002233 return 0;
2234
2235 key.objectid = root_id;
2236 key.type = BTRFS_ROOT_ITEM_KEY;
2237 key.offset = (u64)-1;
2238
Liu Bo38c227d2013-01-29 03:18:40 +00002239 root = btrfs_read_fs_root_no_name(fs_info, &key);
2240 if (IS_ERR(root)) {
2241 if (PTR_ERR(root) == -ENOENT)
2242 return 0;
2243 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002244 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002245 inum, offset, root_id);
2246 return PTR_ERR(root);
2247 }
2248
2249 key.objectid = inum;
2250 key.type = BTRFS_EXTENT_DATA_KEY;
2251 if (offset > (u64)-1 << 32)
2252 key.offset = 0;
2253 else
2254 key.offset = offset;
2255
2256 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302257 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002258 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002259 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002260
2261 while (1) {
2262 cond_resched();
2263
2264 leaf = path->nodes[0];
2265 slot = path->slots[0];
2266
2267 if (slot >= btrfs_header_nritems(leaf)) {
2268 ret = btrfs_next_leaf(root, path);
2269 if (ret < 0) {
2270 goto out;
2271 } else if (ret > 0) {
2272 ret = 0;
2273 goto out;
2274 }
2275 continue;
2276 }
2277
2278 path->slots[0]++;
2279
2280 btrfs_item_key_to_cpu(leaf, &key, slot);
2281
2282 if (key.objectid > inum)
2283 goto out;
2284
2285 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2286 continue;
2287
2288 extent = btrfs_item_ptr(leaf, slot,
2289 struct btrfs_file_extent_item);
2290
2291 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2292 continue;
2293
Liu Boe68afa42013-07-01 22:13:26 +08002294 /*
2295 * 'offset' refers to the exact key.offset,
2296 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2297 * (key.offset - extent_offset).
2298 */
2299 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002300 continue;
2301
Liu Boe68afa42013-07-01 22:13:26 +08002302 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002303 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002304
Liu Bo38c227d2013-01-29 03:18:40 +00002305 if (extent_offset >= old->extent_offset + old->offset +
2306 old->len || extent_offset + num_bytes <=
2307 old->extent_offset + old->offset)
2308 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002309 break;
2310 }
2311
2312 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2313 if (!backref) {
2314 ret = -ENOENT;
2315 goto out;
2316 }
2317
2318 backref->root_id = root_id;
2319 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002320 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002321 backref->num_bytes = num_bytes;
2322 backref->extent_offset = extent_offset;
2323 backref->generation = btrfs_file_extent_generation(leaf, extent);
2324 backref->old = old;
2325 backref_insert(&new->root, backref);
2326 old->count++;
2327out:
2328 btrfs_release_path(path);
2329 WARN_ON(ret);
2330 return ret;
2331}
2332
2333static noinline bool record_extent_backrefs(struct btrfs_path *path,
2334 struct new_sa_defrag_extent *new)
2335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002336 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002337 struct old_sa_defrag_extent *old, *tmp;
2338 int ret;
2339
2340 new->path = path;
2341
2342 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002343 ret = iterate_inodes_from_logical(old->bytenr +
2344 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002345 path, record_one_backref,
2346 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002347 if (ret < 0 && ret != -ENOENT)
2348 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002349
2350 /* no backref to be processed for this extent */
2351 if (!old->count) {
2352 list_del(&old->list);
2353 kfree(old);
2354 }
2355 }
2356
2357 if (list_empty(&new->head))
2358 return false;
2359
2360 return true;
2361}
2362
2363static int relink_is_mergable(struct extent_buffer *leaf,
2364 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002365 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002366{
Liu Bo116e0022013-08-02 16:30:40 +08002367 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002368 return 0;
2369
2370 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2371 return 0;
2372
Liu Bo116e0022013-08-02 16:30:40 +08002373 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2374 return 0;
2375
2376 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002377 btrfs_file_extent_other_encoding(leaf, fi))
2378 return 0;
2379
2380 return 1;
2381}
2382
2383/*
2384 * Note the backref might has changed, and in this case we just return 0.
2385 */
2386static noinline int relink_extent_backref(struct btrfs_path *path,
2387 struct sa_defrag_extent_backref *prev,
2388 struct sa_defrag_extent_backref *backref)
2389{
2390 struct btrfs_file_extent_item *extent;
2391 struct btrfs_file_extent_item *item;
2392 struct btrfs_ordered_extent *ordered;
2393 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002394 struct btrfs_root *root;
2395 struct btrfs_key key;
2396 struct extent_buffer *leaf;
2397 struct old_sa_defrag_extent *old = backref->old;
2398 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002399 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002400 struct inode *inode;
2401 struct extent_state *cached = NULL;
2402 int ret = 0;
2403 u64 start;
2404 u64 len;
2405 u64 lock_start;
2406 u64 lock_end;
2407 bool merge = false;
2408 int index;
2409
2410 if (prev && prev->root_id == backref->root_id &&
2411 prev->inum == backref->inum &&
2412 prev->file_pos + prev->num_bytes == backref->file_pos)
2413 merge = true;
2414
2415 /* step 1: get root */
2416 key.objectid = backref->root_id;
2417 key.type = BTRFS_ROOT_ITEM_KEY;
2418 key.offset = (u64)-1;
2419
Liu Bo38c227d2013-01-29 03:18:40 +00002420 index = srcu_read_lock(&fs_info->subvol_srcu);
2421
2422 root = btrfs_read_fs_root_no_name(fs_info, &key);
2423 if (IS_ERR(root)) {
2424 srcu_read_unlock(&fs_info->subvol_srcu, index);
2425 if (PTR_ERR(root) == -ENOENT)
2426 return 0;
2427 return PTR_ERR(root);
2428 }
Liu Bo38c227d2013-01-29 03:18:40 +00002429
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002430 if (btrfs_root_readonly(root)) {
2431 srcu_read_unlock(&fs_info->subvol_srcu, index);
2432 return 0;
2433 }
2434
Liu Bo38c227d2013-01-29 03:18:40 +00002435 /* step 2: get inode */
2436 key.objectid = backref->inum;
2437 key.type = BTRFS_INODE_ITEM_KEY;
2438 key.offset = 0;
2439
2440 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2441 if (IS_ERR(inode)) {
2442 srcu_read_unlock(&fs_info->subvol_srcu, index);
2443 return 0;
2444 }
2445
2446 srcu_read_unlock(&fs_info->subvol_srcu, index);
2447
2448 /* step 3: relink backref */
2449 lock_start = backref->file_pos;
2450 lock_end = backref->file_pos + backref->num_bytes - 1;
2451 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002452 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002453
2454 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2455 if (ordered) {
2456 btrfs_put_ordered_extent(ordered);
2457 goto out_unlock;
2458 }
2459
2460 trans = btrfs_join_transaction(root);
2461 if (IS_ERR(trans)) {
2462 ret = PTR_ERR(trans);
2463 goto out_unlock;
2464 }
2465
2466 key.objectid = backref->inum;
2467 key.type = BTRFS_EXTENT_DATA_KEY;
2468 key.offset = backref->file_pos;
2469
2470 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2471 if (ret < 0) {
2472 goto out_free_path;
2473 } else if (ret > 0) {
2474 ret = 0;
2475 goto out_free_path;
2476 }
2477
2478 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2479 struct btrfs_file_extent_item);
2480
2481 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2482 backref->generation)
2483 goto out_free_path;
2484
2485 btrfs_release_path(path);
2486
2487 start = backref->file_pos;
2488 if (backref->extent_offset < old->extent_offset + old->offset)
2489 start += old->extent_offset + old->offset -
2490 backref->extent_offset;
2491
2492 len = min(backref->extent_offset + backref->num_bytes,
2493 old->extent_offset + old->offset + old->len);
2494 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2495
2496 ret = btrfs_drop_extents(trans, root, inode, start,
2497 start + len, 1);
2498 if (ret)
2499 goto out_free_path;
2500again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002501 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002502 key.type = BTRFS_EXTENT_DATA_KEY;
2503 key.offset = start;
2504
Liu Boa09a0a72013-03-11 09:20:58 +00002505 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002506 if (merge) {
2507 struct btrfs_file_extent_item *fi;
2508 u64 extent_len;
2509 struct btrfs_key found_key;
2510
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002511 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002512 if (ret < 0)
2513 goto out_free_path;
2514
2515 path->slots[0]--;
2516 leaf = path->nodes[0];
2517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2518
2519 fi = btrfs_item_ptr(leaf, path->slots[0],
2520 struct btrfs_file_extent_item);
2521 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2522
Liu Bo116e0022013-08-02 16:30:40 +08002523 if (extent_len + found_key.offset == start &&
2524 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002525 btrfs_set_file_extent_num_bytes(leaf, fi,
2526 extent_len + len);
2527 btrfs_mark_buffer_dirty(leaf);
2528 inode_add_bytes(inode, len);
2529
2530 ret = 1;
2531 goto out_free_path;
2532 } else {
2533 merge = false;
2534 btrfs_release_path(path);
2535 goto again;
2536 }
2537 }
2538
2539 ret = btrfs_insert_empty_item(trans, root, path, &key,
2540 sizeof(*extent));
2541 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002542 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002543 goto out_free_path;
2544 }
2545
2546 leaf = path->nodes[0];
2547 item = btrfs_item_ptr(leaf, path->slots[0],
2548 struct btrfs_file_extent_item);
2549 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2550 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2551 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2552 btrfs_set_file_extent_num_bytes(leaf, item, len);
2553 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2554 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2555 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2556 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2557 btrfs_set_file_extent_encryption(leaf, item, 0);
2558 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2559
2560 btrfs_mark_buffer_dirty(leaf);
2561 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002562 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002563
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002564 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002565 new->disk_len, 0,
2566 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002567 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002568 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002569 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002570 goto out_free_path;
2571 }
2572
2573 ret = 1;
2574out_free_path:
2575 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002576 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002577 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002578out_unlock:
2579 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2580 &cached, GFP_NOFS);
2581 iput(inode);
2582 return ret;
2583}
2584
Liu Bo6f519562013-10-29 10:45:05 +08002585static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2586{
2587 struct old_sa_defrag_extent *old, *tmp;
2588
2589 if (!new)
2590 return;
2591
2592 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002593 kfree(old);
2594 }
2595 kfree(new);
2596}
2597
Liu Bo38c227d2013-01-29 03:18:40 +00002598static void relink_file_extents(struct new_sa_defrag_extent *new)
2599{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002600 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002601 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002602 struct sa_defrag_extent_backref *backref;
2603 struct sa_defrag_extent_backref *prev = NULL;
2604 struct inode *inode;
2605 struct btrfs_root *root;
2606 struct rb_node *node;
2607 int ret;
2608
2609 inode = new->inode;
2610 root = BTRFS_I(inode)->root;
2611
2612 path = btrfs_alloc_path();
2613 if (!path)
2614 return;
2615
2616 if (!record_extent_backrefs(path, new)) {
2617 btrfs_free_path(path);
2618 goto out;
2619 }
2620 btrfs_release_path(path);
2621
2622 while (1) {
2623 node = rb_first(&new->root);
2624 if (!node)
2625 break;
2626 rb_erase(node, &new->root);
2627
2628 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2629
2630 ret = relink_extent_backref(path, prev, backref);
2631 WARN_ON(ret < 0);
2632
2633 kfree(prev);
2634
2635 if (ret == 1)
2636 prev = backref;
2637 else
2638 prev = NULL;
2639 cond_resched();
2640 }
2641 kfree(prev);
2642
2643 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002644out:
Liu Bo6f519562013-10-29 10:45:05 +08002645 free_sa_defrag_extent(new);
2646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002647 atomic_dec(&fs_info->defrag_running);
2648 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002649}
2650
2651static struct new_sa_defrag_extent *
2652record_old_file_extents(struct inode *inode,
2653 struct btrfs_ordered_extent *ordered)
2654{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002656 struct btrfs_root *root = BTRFS_I(inode)->root;
2657 struct btrfs_path *path;
2658 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002659 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002660 struct new_sa_defrag_extent *new;
2661 int ret;
2662
2663 new = kmalloc(sizeof(*new), GFP_NOFS);
2664 if (!new)
2665 return NULL;
2666
2667 new->inode = inode;
2668 new->file_pos = ordered->file_offset;
2669 new->len = ordered->len;
2670 new->bytenr = ordered->start;
2671 new->disk_len = ordered->disk_len;
2672 new->compress_type = ordered->compress_type;
2673 new->root = RB_ROOT;
2674 INIT_LIST_HEAD(&new->head);
2675
2676 path = btrfs_alloc_path();
2677 if (!path)
2678 goto out_kfree;
2679
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002680 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002681 key.type = BTRFS_EXTENT_DATA_KEY;
2682 key.offset = new->file_pos;
2683
2684 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2685 if (ret < 0)
2686 goto out_free_path;
2687 if (ret > 0 && path->slots[0] > 0)
2688 path->slots[0]--;
2689
2690 /* find out all the old extents for the file range */
2691 while (1) {
2692 struct btrfs_file_extent_item *extent;
2693 struct extent_buffer *l;
2694 int slot;
2695 u64 num_bytes;
2696 u64 offset;
2697 u64 end;
2698 u64 disk_bytenr;
2699 u64 extent_offset;
2700
2701 l = path->nodes[0];
2702 slot = path->slots[0];
2703
2704 if (slot >= btrfs_header_nritems(l)) {
2705 ret = btrfs_next_leaf(root, path);
2706 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002707 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002708 else if (ret > 0)
2709 break;
2710 continue;
2711 }
2712
2713 btrfs_item_key_to_cpu(l, &key, slot);
2714
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002715 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002716 break;
2717 if (key.type != BTRFS_EXTENT_DATA_KEY)
2718 break;
2719 if (key.offset >= new->file_pos + new->len)
2720 break;
2721
2722 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2723
2724 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2725 if (key.offset + num_bytes < new->file_pos)
2726 goto next;
2727
2728 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2729 if (!disk_bytenr)
2730 goto next;
2731
2732 extent_offset = btrfs_file_extent_offset(l, extent);
2733
2734 old = kmalloc(sizeof(*old), GFP_NOFS);
2735 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002736 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002737
2738 offset = max(new->file_pos, key.offset);
2739 end = min(new->file_pos + new->len, key.offset + num_bytes);
2740
2741 old->bytenr = disk_bytenr;
2742 old->extent_offset = extent_offset;
2743 old->offset = offset - key.offset;
2744 old->len = end - offset;
2745 old->new = new;
2746 old->count = 0;
2747 list_add_tail(&old->list, &new->head);
2748next:
2749 path->slots[0]++;
2750 cond_resched();
2751 }
2752
2753 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002754 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002755
2756 return new;
2757
Liu Bo38c227d2013-01-29 03:18:40 +00002758out_free_path:
2759 btrfs_free_path(path);
2760out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002761 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002762 return NULL;
2763}
2764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002765static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002766 u64 start, u64 len)
2767{
2768 struct btrfs_block_group_cache *cache;
2769
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002770 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002771 ASSERT(cache);
2772
2773 spin_lock(&cache->lock);
2774 cache->delalloc_bytes -= len;
2775 spin_unlock(&cache->lock);
2776
2777 btrfs_put_block_group(cache);
2778}
2779
Chris Masond352ac62008-09-29 15:18:18 -04002780/* as ordered data IO finishes, this gets called so we can finish
2781 * an ordered extent if the range of bytes in the file it covers are
2782 * fully written.
2783 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002784static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785{
Josef Bacik5fd02042012-05-02 14:00:54 -04002786 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002787 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002789 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002790 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002791 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002792 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002793 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002794 int ret = 0;
2795 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002796 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002797 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002798
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002799 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002800
Josef Bacik5fd02042012-05-02 14:00:54 -04002801 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2802 ret = -EIO;
2803 goto out;
2804 }
2805
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002806 btrfs_free_io_failure_record(BTRFS_I(inode),
2807 ordered_extent->file_offset,
2808 ordered_extent->file_offset +
2809 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002810
Josef Bacik77cef2e2013-08-29 13:57:21 -04002811 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2812 truncated = true;
2813 logical_len = ordered_extent->truncated_len;
2814 /* Truncated the entire extent, don't bother adding */
2815 if (!logical_len)
2816 goto out;
2817 }
2818
Yan, Zhengc2167752009-11-12 09:34:21 +00002819 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002821
2822 /*
2823 * For mwrite(mmap + memset to write) case, we still reserve
2824 * space for NOCOW range.
2825 * As NOCOW won't cause a new delayed ref, just free the space
2826 */
2827 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2828 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002829 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2830 if (nolock)
2831 trans = btrfs_join_transaction_nolock(root);
2832 else
2833 trans = btrfs_join_transaction(root);
2834 if (IS_ERR(trans)) {
2835 ret = PTR_ERR(trans);
2836 trans = NULL;
2837 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002838 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002839 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002840 ret = btrfs_update_inode_fallback(trans, root, inode);
2841 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002842 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002843 goto out;
2844 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002845
Josef Bacik2ac55d42010-02-03 19:33:23 +00002846 lock_extent_bits(io_tree, ordered_extent->file_offset,
2847 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002848 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002849
Liu Bo38c227d2013-01-29 03:18:40 +00002850 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2851 ordered_extent->file_offset + ordered_extent->len - 1,
2852 EXTENT_DEFRAG, 1, cached_state);
2853 if (ret) {
2854 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002855 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002856 /* the inode is shared */
2857 new = record_old_file_extents(inode, ordered_extent);
2858
2859 clear_extent_bit(io_tree, ordered_extent->file_offset,
2860 ordered_extent->file_offset + ordered_extent->len - 1,
2861 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2862 }
2863
Josef Bacik0cb59c92010-07-02 12:14:14 -04002864 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002865 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002866 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002867 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002868 if (IS_ERR(trans)) {
2869 ret = PTR_ERR(trans);
2870 trans = NULL;
2871 goto out_unlock;
2872 }
Chris Masona79b7d42014-05-22 16:18:52 -07002873
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002875
Chris Masonc8b97812008-10-29 14:49:59 -04002876 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002877 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002878 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002879 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002880 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002881 ordered_extent->file_offset,
2882 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002883 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002884 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002885 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002886 ret = insert_reserved_file_extent(trans, inode,
2887 ordered_extent->file_offset,
2888 ordered_extent->start,
2889 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002890 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002891 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002892 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002893 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002894 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002895 ordered_extent->start,
2896 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002897 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002898 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2899 ordered_extent->file_offset, ordered_extent->len,
2900 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002901 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002902 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002903 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002904 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002905
David Sterbadf9f6282017-02-10 19:35:37 +01002906 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002907
Josef Bacik6c760c02012-11-09 10:53:21 -05002908 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2909 ret = btrfs_update_inode_fallback(trans, root, inode);
2910 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002911 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002912 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002913 }
2914 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002915out_unlock:
2916 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2917 ordered_extent->file_offset +
2918 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002919out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002921 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2922 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002923 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002924 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002925
Josef Bacik77cef2e2013-08-29 13:57:21 -04002926 if (ret || truncated) {
2927 u64 start, end;
2928
2929 if (truncated)
2930 start = ordered_extent->file_offset + logical_len;
2931 else
2932 start = ordered_extent->file_offset;
2933 end = ordered_extent->file_offset + ordered_extent->len - 1;
2934 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2935
2936 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002937 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002938
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 /*
2940 * If the ordered extent had an IOERR or something else went
2941 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 * back to the allocator. We only free the extent in the
2943 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002944 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002945 if ((ret || !logical_len) &&
2946 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002947 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002948 btrfs_free_reserved_extent(fs_info,
2949 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002950 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002951 }
2952
2953
Josef Bacik5fd02042012-05-02 14:00:54 -04002954 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002955 * This needs to be done to make sure anybody waiting knows we are done
2956 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002957 */
2958 btrfs_remove_ordered_extent(inode, ordered_extent);
2959
Liu Bo38c227d2013-01-29 03:18:40 +00002960 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002961 if (new) {
2962 if (ret) {
2963 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002964 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002965 } else {
2966 relink_file_extents(new);
2967 }
2968 }
Liu Bo38c227d2013-01-29 03:18:40 +00002969
Chris Masone6dcd2d2008-07-17 12:53:50 -04002970 /* once for us */
2971 btrfs_put_ordered_extent(ordered_extent);
2972 /* once for the tree */
2973 btrfs_put_ordered_extent(ordered_extent);
2974
Josef Bacik5fd02042012-05-02 14:00:54 -04002975 return ret;
2976}
2977
2978static void finish_ordered_fn(struct btrfs_work *work)
2979{
2980 struct btrfs_ordered_extent *ordered_extent;
2981 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2982 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002983}
2984
Christoph Hellwigb2950862008-12-02 09:54:17 -05002985static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002986 struct extent_state *state, int uptodate)
2987{
Josef Bacik5fd02042012-05-02 14:00:54 -04002988 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002989 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002990 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002991 struct btrfs_workqueue *wq;
2992 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002993
liubo1abe9b82011-03-24 11:18:59 +00002994 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2995
Chris Mason8b62b722009-09-02 16:53:46 -04002996 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002997 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2998 end - start + 1, uptodate))
2999 return 0;
3000
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003001 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003003 func = btrfs_freespace_write_helper;
3004 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003005 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003006 func = btrfs_endio_write_helper;
3007 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003008
Liu Bo9e0af232014-08-15 23:36:53 +08003009 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3010 NULL);
3011 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003012
3013 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003014}
3015
Miao Xiedc380ae2014-09-12 18:43:55 +08003016static int __readpage_endio_check(struct inode *inode,
3017 struct btrfs_io_bio *io_bio,
3018 int icsum, struct page *page,
3019 int pgoff, u64 start, size_t len)
3020{
3021 char *kaddr;
3022 u32 csum_expected;
3023 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003024
3025 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3026
3027 kaddr = kmap_atomic(page);
3028 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003029 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003030 if (csum != csum_expected)
3031 goto zeroit;
3032
3033 kunmap_atomic(kaddr);
3034 return 0;
3035zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003036 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003037 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003038 memset(kaddr + pgoff, 1, len);
3039 flush_dcache_page(page);
3040 kunmap_atomic(kaddr);
3041 if (csum_expected == 0)
3042 return 0;
3043 return -EIO;
3044}
3045
Chris Masond352ac62008-09-29 15:18:18 -04003046/*
Chris Masond352ac62008-09-29 15:18:18 -04003047 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003048 * if there's a match, we allow the bio to finish. If not, the code in
3049 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003050 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003051static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3052 u64 phy_offset, struct page *page,
3053 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003054{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003055 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003056 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003058 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003059
Chris Masond20f7042008-12-08 16:58:54 -05003060 if (PageChecked(page)) {
3061 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003062 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003063 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003064
3065 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003066 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003067
Yan Zheng17d217f2008-12-12 10:03:38 -05003068 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003069 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003070 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003071 return 0;
3072 }
3073
Miao Xiefacc8a222013-07-25 19:22:34 +08003074 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003075 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3076 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003077}
Chris Masonb888db22007-08-27 16:49:44 -04003078
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003079void btrfs_add_delayed_iput(struct inode *inode)
3080{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003081 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003082 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003083
3084 if (atomic_add_unless(&inode->i_count, -1, 1))
3085 return;
3086
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003087 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003088 if (binode->delayed_iput_count == 0) {
3089 ASSERT(list_empty(&binode->delayed_iput));
3090 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3091 } else {
3092 binode->delayed_iput_count++;
3093 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094 spin_unlock(&fs_info->delayed_iput_lock);
3095}
3096
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003097void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003098{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003099
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003100 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003101 while (!list_empty(&fs_info->delayed_iputs)) {
3102 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003103
David Sterba8089fe62015-11-19 14:15:51 +01003104 inode = list_first_entry(&fs_info->delayed_iputs,
3105 struct btrfs_inode, delayed_iput);
3106 if (inode->delayed_iput_count) {
3107 inode->delayed_iput_count--;
3108 list_move_tail(&inode->delayed_iput,
3109 &fs_info->delayed_iputs);
3110 } else {
3111 list_del_init(&inode->delayed_iput);
3112 }
3113 spin_unlock(&fs_info->delayed_iput_lock);
3114 iput(&inode->vfs_inode);
3115 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003116 }
David Sterba8089fe62015-11-19 14:15:51 +01003117 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003118}
3119
Yan, Zhengd68fc572010-05-16 10:49:58 -04003120/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003121 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003122 * files in the subvolume, it removes orphan item and frees block_rsv
3123 * structure.
3124 */
3125void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3126 struct btrfs_root *root)
3127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003129 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130 int ret;
3131
Josef Bacik8a35d952012-05-23 14:26:42 -04003132 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3134 return;
3135
Josef Bacik90290e12011-12-02 15:44:12 -05003136 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003137 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003138 spin_unlock(&root->orphan_lock);
3139 return;
3140 }
3141
3142 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3143 spin_unlock(&root->orphan_lock);
3144 return;
3145 }
3146
3147 block_rsv = root->orphan_block_rsv;
3148 root->orphan_block_rsv = NULL;
3149 spin_unlock(&root->orphan_lock);
3150
Miao Xie27cdeb72014-04-02 19:51:05 +08003151 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003152 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003153 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003155 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003156 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003157 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003158 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3159 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 }
3161
Josef Bacik90290e12011-12-02 15:44:12 -05003162 if (block_rsv) {
3163 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003164 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003165 }
3166}
3167
3168/*
Josef Bacik7b128762008-07-24 12:17:14 -04003169 * This creates an orphan entry for the given inode in case something goes
3170 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003171 *
3172 * NOTE: caller of this function should reserve 5 units of metadata for
3173 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003174 */
3175int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3176{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003177 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003178 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 struct btrfs_block_rsv *block_rsv = NULL;
3180 int reserve = 0;
3181 int insert = 0;
3182 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003183
Yan, Zhengd68fc572010-05-16 10:49:58 -04003184 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003185 block_rsv = btrfs_alloc_block_rsv(fs_info,
3186 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003187 if (!block_rsv)
3188 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003189 }
3190
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 spin_lock(&root->orphan_lock);
3192 if (!root->orphan_block_rsv) {
3193 root->orphan_block_rsv = block_rsv;
3194 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003195 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196 block_rsv = NULL;
3197 }
Josef Bacik7b128762008-07-24 12:17:14 -04003198
Josef Bacik8a35d952012-05-23 14:26:42 -04003199 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3200 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201#if 0
3202 /*
3203 * For proper ENOSPC handling, we should do orphan
3204 * cleanup when mounting. But this introduces backward
3205 * compatibility issue.
3206 */
3207 if (!xchg(&root->orphan_item_inserted, 1))
3208 insert = 2;
3209 else
3210 insert = 1;
3211#endif
3212 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003213 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 }
Josef Bacik7b128762008-07-24 12:17:14 -04003215
Josef Bacik72ac3c02012-05-23 14:13:11 -04003216 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3217 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003218 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003220
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 /* grab metadata reservation from transaction handle */
3222 if (reserve) {
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02003223 ret = btrfs_orphan_reserve_metadata(trans, BTRFS_I(inode));
Josef Bacik3b6571c2016-05-27 13:03:04 -04003224 ASSERT(!ret);
3225 if (ret) {
3226 atomic_dec(&root->orphan_inodes);
3227 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3228 &BTRFS_I(inode)->runtime_flags);
3229 if (insert)
3230 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3231 &BTRFS_I(inode)->runtime_flags);
3232 return ret;
3233 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003234 }
3235
3236 /* insert an orphan item to track this unlinked/truncated file */
3237 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003238 ret = btrfs_insert_orphan_item(trans, root,
3239 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003240 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003241 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003242 if (reserve) {
3243 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3244 &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov703b3912017-02-20 13:50:40 +02003245 btrfs_orphan_release_metadata(BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 }
3247 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003248 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3249 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003250 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003251 return ret;
3252 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003253 }
3254 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 }
3256
3257 /* insert an orphan item to track subvolume contains orphan files */
3258 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003259 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003261 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003262 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003263 return ret;
3264 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 }
3266 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003267}
3268
3269/*
3270 * We have done the truncate/delete so we can go ahead and remove the orphan
3271 * item for this particular inode.
3272 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003273static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3274 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003275{
3276 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 int delete_item = 0;
3278 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003279 int ret = 0;
3280
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003282 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3283 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003285
Josef Bacik72ac3c02012-05-23 14:13:11 -04003286 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3287 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 spin_unlock(&root->orphan_lock);
3290
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003291 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003293 if (trans)
3294 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003295 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003296 }
Josef Bacik7b128762008-07-24 12:17:14 -04003297
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003298 if (release_rsv)
Nikolay Borisov703b3912017-02-20 13:50:40 +02003299 btrfs_orphan_release_metadata(BTRFS_I(inode));
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003300
Josef Bacik4ef31a42013-08-13 14:10:08 -04003301 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003302}
3303
3304/*
3305 * this cleans up any orphans that may be left on the list from the last use
3306 * of this root.
3307 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003308int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003310 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003311 struct btrfs_path *path;
3312 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003313 struct btrfs_key key, found_key;
3314 struct btrfs_trans_handle *trans;
3315 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003316 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003317 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3318
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003320 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003321
3322 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003323 if (!path) {
3324 ret = -ENOMEM;
3325 goto out;
3326 }
David Sterbae4058b52015-11-27 16:31:35 +01003327 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003328
3329 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003330 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003331 key.offset = (u64)-1;
3332
Josef Bacik7b128762008-07-24 12:17:14 -04003333 while (1) {
3334 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003335 if (ret < 0)
3336 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003337
3338 /*
3339 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003340 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003341 * find the key and see if we have stuff that matches
3342 */
3343 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003344 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003345 if (path->slots[0] == 0)
3346 break;
3347 path->slots[0]--;
3348 }
3349
3350 /* pull out the item */
3351 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003352 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3353
3354 /* make sure the item matches what we want */
3355 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3356 break;
David Sterba962a2982014-06-04 18:41:45 +02003357 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003358 break;
3359
3360 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003361 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003362
3363 /*
3364 * this is where we are basically btrfs_lookup, without the
3365 * crossing root thing. we store the inode number in the
3366 * offset of the orphan item.
3367 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003368
3369 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003370 btrfs_err(fs_info,
3371 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003372 ret = -EINVAL;
3373 goto out;
3374 }
3375
3376 last_objectid = found_key.offset;
3377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003378 found_key.objectid = found_key.offset;
3379 found_key.type = BTRFS_INODE_ITEM_KEY;
3380 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303382 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003383 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003384 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003385
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003386 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003387 struct btrfs_root *dead_root;
3388 struct btrfs_fs_info *fs_info = root->fs_info;
3389 int is_dead_root = 0;
3390
3391 /*
3392 * this is an orphan in the tree root. Currently these
3393 * could come from 2 sources:
3394 * a) a snapshot deletion in progress
3395 * b) a free space cache inode
3396 * We need to distinguish those two, as the snapshot
3397 * orphan must not get deleted.
3398 * find_dead_roots already ran before us, so if this
3399 * is a snapshot deletion, we should find the root
3400 * in the dead_roots list
3401 */
3402 spin_lock(&fs_info->trans_lock);
3403 list_for_each_entry(dead_root, &fs_info->dead_roots,
3404 root_list) {
3405 if (dead_root->root_key.objectid ==
3406 found_key.objectid) {
3407 is_dead_root = 1;
3408 break;
3409 }
3410 }
3411 spin_unlock(&fs_info->trans_lock);
3412 if (is_dead_root) {
3413 /* prevent this orphan from being found again */
3414 key.offset = found_key.objectid - 1;
3415 continue;
3416 }
3417 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003418 /*
3419 * Inode is already gone but the orphan item is still there,
3420 * kill the orphan item.
3421 */
Filipe Manana67710892016-06-06 11:51:25 +01003422 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003423 trans = btrfs_start_transaction(root, 1);
3424 if (IS_ERR(trans)) {
3425 ret = PTR_ERR(trans);
3426 goto out;
3427 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003428 btrfs_debug(fs_info, "auto deleting %Lu",
3429 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003430 ret = btrfs_del_orphan_item(trans, root,
3431 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003432 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003433 if (ret)
3434 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003435 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003436 }
Josef Bacik7b128762008-07-24 12:17:14 -04003437
Josef Bacik7b128762008-07-24 12:17:14 -04003438 /*
3439 * add this inode to the orphan list so btrfs_orphan_del does
3440 * the proper thing when we hit it
3441 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003442 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3443 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003444 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Josef Bacik7b128762008-07-24 12:17:14 -04003446 /* if we have links, this was a truncate, lets do that */
3447 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303448 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003449 iput(inode);
3450 continue;
3451 }
Josef Bacik7b128762008-07-24 12:17:14 -04003452 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003453
3454 /* 1 for the orphan item deletion. */
3455 trans = btrfs_start_transaction(root, 1);
3456 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003457 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003458 ret = PTR_ERR(trans);
3459 goto out;
3460 }
3461 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003462 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003463 if (ret) {
3464 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003465 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003466 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003467
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003469 if (ret)
3470 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003471 } else {
3472 nr_unlink++;
3473 }
3474
3475 /* this will do delete_inode and everything for us */
3476 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003477 if (ret)
3478 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003479 }
Miao Xie3254c872011-11-10 20:45:05 -05003480 /* release the path since we're done with it */
3481 btrfs_release_path(path);
3482
Yan, Zhengd68fc572010-05-16 10:49:58 -04003483 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3484
3485 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003486 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003487 (u64)-1);
3488
Miao Xie27cdeb72014-04-02 19:51:05 +08003489 if (root->orphan_block_rsv ||
3490 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003491 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003493 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003494 }
Josef Bacik7b128762008-07-24 12:17:14 -04003495
3496 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003497 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003498 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003499 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500
3501out:
3502 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003503 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003504 btrfs_free_path(path);
3505 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003506}
3507
Chris Masond352ac62008-09-29 15:18:18 -04003508/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003509 * very simple check to peek ahead in the leaf looking for xattrs. If we
3510 * don't find any xattrs, we know there can't be any acls.
3511 *
3512 * slot is the slot the inode is in, objectid is the objectid of the inode
3513 */
3514static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003515 int slot, u64 objectid,
3516 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003517{
3518 u32 nritems = btrfs_header_nritems(leaf);
3519 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003520 static u64 xattr_access = 0;
3521 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003522 int scanned = 0;
3523
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003525 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3526 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3527 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3528 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003529 }
3530
Chris Mason46a53cc2009-04-27 11:47:50 -04003531 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003532 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003533 while (slot < nritems) {
3534 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3535
3536 /* we found a different objectid, there must not be acls */
3537 if (found_key.objectid != objectid)
3538 return 0;
3539
3540 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003542 if (*first_xattr_slot == -1)
3543 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003544 if (found_key.offset == xattr_access ||
3545 found_key.offset == xattr_default)
3546 return 1;
3547 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003548
3549 /*
3550 * we found a key greater than an xattr key, there can't
3551 * be any acls later on
3552 */
3553 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3554 return 0;
3555
3556 slot++;
3557 scanned++;
3558
3559 /*
3560 * it goes inode, inode backrefs, xattrs, extents,
3561 * so if there are a ton of hard links to an inode there can
3562 * be a lot of backrefs. Don't waste time searching too hard,
3563 * this is just an optimization
3564 */
3565 if (scanned >= 8)
3566 break;
3567 }
3568 /* we hit the end of the leaf before we found an xattr or
3569 * something larger than an xattr. We have to assume the inode
3570 * has acls
3571 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003572 if (*first_xattr_slot == -1)
3573 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003574 return 1;
3575}
3576
3577/*
Chris Masond352ac62008-09-29 15:18:18 -04003578 * read an inode from the btree into the in-memory inode
3579 */
Filipe Manana67710892016-06-06 11:51:25 +01003580static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003583 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003584 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003585 struct btrfs_inode_item *inode_item;
3586 struct btrfs_root *root = BTRFS_I(inode)->root;
3587 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003588 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003589 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003590 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003591 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003592 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003593 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003594
3595 ret = btrfs_fill_inode(inode, &rdev);
3596 if (!ret)
3597 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003598
3599 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003600 if (!path) {
3601 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003602 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003603 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003604
Chris Mason39279cc2007-06-12 06:35:45 -04003605 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003606
Chris Mason39279cc2007-06-12 06:35:45 -04003607 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003608 if (ret) {
3609 if (ret > 0)
3610 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003611 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003612 }
Chris Mason39279cc2007-06-12 06:35:45 -04003613
Chris Mason5f39d392007-10-15 16:14:19 -04003614 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003615
3616 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003617 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003618
Chris Mason5f39d392007-10-15 16:14:19 -04003619 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3620 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003621 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003622 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003623 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3624 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003625 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003626
David Sterbaa937b972014-12-12 17:39:12 +01003627 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3628 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003629
David Sterbaa937b972014-12-12 17:39:12 +01003630 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3631 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003632
David Sterbaa937b972014-12-12 17:39:12 +01003633 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3634 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
chandan r9cc97d62012-07-04 12:48:07 +05303636 BTRFS_I(inode)->i_otime.tv_sec =
3637 btrfs_timespec_sec(leaf, &inode_item->otime);
3638 BTRFS_I(inode)->i_otime.tv_nsec =
3639 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003640
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003641 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003642 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003643 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3644
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003645 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003646 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003647 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003648 rdev = btrfs_inode_rdev(leaf, inode_item);
3649
Josef Bacikaec74772008-07-24 12:12:38 -04003650 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003651 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003652
3653cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003654 /*
3655 * If we were modified in the current generation and evicted from memory
3656 * and then re-read we need to do a full sync since we don't have any
3657 * idea about which extents were modified before we were evicted from
3658 * cache.
3659 *
3660 * This is required for both inode re-read from disk and delayed inode
3661 * in delayed_nodes_tree.
3662 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003664 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3665 &BTRFS_I(inode)->runtime_flags);
3666
Filipe Mananabde6c242015-07-24 00:00:19 +01003667 /*
3668 * We don't persist the id of the transaction where an unlink operation
3669 * against the inode was last made. So here we assume the inode might
3670 * have been evicted, and therefore the exact value of last_unlink_trans
3671 * lost, and set it to last_trans to avoid metadata inconsistencies
3672 * between the inode and its parent if the inode is fsync'ed and the log
3673 * replayed. For example, in the scenario:
3674 *
3675 * touch mydir/foo
3676 * ln mydir/foo mydir/bar
3677 * sync
3678 * unlink mydir/bar
3679 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3680 * xfs_io -c fsync mydir/foo
3681 * <power failure>
3682 * mount fs, triggers fsync log replay
3683 *
3684 * We must make sure that when we fsync our inode foo we also log its
3685 * parent inode, otherwise after log replay the parent still has the
3686 * dentry with the "bar" name but our inode foo has a link count of 1
3687 * and doesn't have an inode ref with the name "bar" anymore.
3688 *
3689 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003690 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003691 * transaction commits on fsync if our inode is a directory, or if our
3692 * inode is not a directory, logging its parent unnecessarily.
3693 */
3694 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3695
Miao Xie67de1172013-12-26 13:07:06 +08003696 path->slots[0]++;
3697 if (inode->i_nlink != 1 ||
3698 path->slots[0] >= btrfs_header_nritems(leaf))
3699 goto cache_acl;
3700
3701 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003702 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003703 goto cache_acl;
3704
3705 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3706 if (location.type == BTRFS_INODE_REF_KEY) {
3707 struct btrfs_inode_ref *ref;
3708
3709 ref = (struct btrfs_inode_ref *)ptr;
3710 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3711 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3712 struct btrfs_inode_extref *extref;
3713
3714 extref = (struct btrfs_inode_extref *)ptr;
3715 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3716 extref);
3717 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003718cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003719 /*
3720 * try to precache a NULL acl entry for files that don't have
3721 * any xattrs or acls
3722 */
Li Zefan33345d012011-04-20 10:31:50 +08003723 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003724 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725 if (first_xattr_slot != -1) {
3726 path->slots[0] = first_xattr_slot;
3727 ret = btrfs_load_inode_props(inode, path);
3728 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003729 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003730 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003731 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003732 root->root_key.objectid, ret);
3733 }
3734 btrfs_free_path(path);
3735
Al Viro72c04902009-06-24 16:58:48 -04003736 if (!maybe_acls)
3737 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003738
Chris Mason39279cc2007-06-12 06:35:45 -04003739 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003740 case S_IFREG:
3741 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003742 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 inode->i_fop = &btrfs_file_operations;
3744 inode->i_op = &btrfs_file_inode_operations;
3745 break;
3746 case S_IFDIR:
3747 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003748 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003749 break;
3750 case S_IFLNK:
3751 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003752 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003753 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3754 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003755 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003756 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003757 init_special_inode(inode, inode->i_mode, rdev);
3758 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003760
3761 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003762 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003763
3764make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003765 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003766 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003767 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003768}
3769
Chris Masond352ac62008-09-29 15:18:18 -04003770/*
3771 * given a leaf and an inode, copy the inode fields into the leaf
3772 */
Chris Masone02119d2008-09-05 16:13:11 -04003773static void fill_inode_item(struct btrfs_trans_handle *trans,
3774 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003775 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003776 struct inode *inode)
3777{
Liu Bo51fab692012-12-27 09:01:21 +00003778 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003779
Liu Bo51fab692012-12-27 09:01:21 +00003780 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
Liu Bo51fab692012-12-27 09:01:21 +00003782 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3783 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3784 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3785 &token);
3786 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3787 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
David Sterbaa937b972014-12-12 17:39:12 +01003794 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003795 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_mtime.tv_nsec, &token);
3798
David Sterbaa937b972014-12-12 17:39:12 +01003799 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003800 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003801 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003802 inode->i_ctime.tv_nsec, &token);
3803
chandan r9cc97d62012-07-04 12:48:07 +05303804 btrfs_set_token_timespec_sec(leaf, &item->otime,
3805 BTRFS_I(inode)->i_otime.tv_sec, &token);
3806 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3807 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3808
Liu Bo51fab692012-12-27 09:01:21 +00003809 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3810 &token);
3811 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3812 &token);
3813 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3814 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3815 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3816 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3817 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003818}
3819
Chris Masond352ac62008-09-29 15:18:18 -04003820/*
3821 * copy everything in the in-memory inode into the btree.
3822 */
Chris Mason21151332011-11-10 20:39:08 -05003823static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003824 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003825{
3826 struct btrfs_inode_item *inode_item;
3827 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003828 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003829 int ret;
3830
3831 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003832 if (!path)
3833 return -ENOMEM;
3834
Chris Masonb9473432009-03-13 11:00:37 -04003835 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003836 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3837 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003838 if (ret) {
3839 if (ret > 0)
3840 ret = -ENOENT;
3841 goto failed;
3842 }
3843
Chris Mason5f39d392007-10-15 16:14:19 -04003844 leaf = path->nodes[0];
3845 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003846 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003847
Chris Masone02119d2008-09-05 16:13:11 -04003848 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003849 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003850 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003851 ret = 0;
3852failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003853 btrfs_free_path(path);
3854 return ret;
3855}
3856
Chris Masond352ac62008-09-29 15:18:18 -04003857/*
Chris Mason21151332011-11-10 20:39:08 -05003858 * copy everything in the in-memory inode into the btree.
3859 */
3860noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3861 struct btrfs_root *root, struct inode *inode)
3862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003863 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003864 int ret;
3865
3866 /*
3867 * If the inode is a free space inode, we can deadlock during commit
3868 * if we put it into the delayed code.
3869 *
3870 * The data relocation inode should also be directly updated
3871 * without delay
3872 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003873 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003874 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003875 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003876 btrfs_update_root_times(trans, root);
3877
Chris Mason21151332011-11-10 20:39:08 -05003878 ret = btrfs_delayed_update_inode(trans, root, inode);
3879 if (!ret)
3880 btrfs_set_inode_last_trans(trans, inode);
3881 return ret;
3882 }
3883
3884 return btrfs_update_inode_item(trans, root, inode);
3885}
3886
Josef Bacikbe6aef62012-10-22 15:43:12 -04003887noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3888 struct btrfs_root *root,
3889 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003890{
3891 int ret;
3892
3893 ret = btrfs_update_inode(trans, root, inode);
3894 if (ret == -ENOSPC)
3895 return btrfs_update_inode_item(trans, root, inode);
3896 return ret;
3897}
3898
3899/*
Chris Masond352ac62008-09-29 15:18:18 -04003900 * unlink helper that gets used here in inode.c and in the tree logging
3901 * recovery code. It remove a link in a directory with a given name, and
3902 * also drops the back refs in the inode to the directory
3903 */
Al Viro92986792011-03-04 17:14:37 +00003904static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3905 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003906 struct btrfs_inode *dir,
3907 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003908 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003909{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003910 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003913 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003915 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003916 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003917 u64 ino = btrfs_ino(inode);
3918 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003919
3920 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003921 if (!path) {
3922 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003923 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003924 }
3925
Chris Masonb9473432009-03-13 11:00:37 -04003926 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003927 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003928 name, name_len, -1);
3929 if (IS_ERR(di)) {
3930 ret = PTR_ERR(di);
3931 goto err;
3932 }
3933 if (!di) {
3934 ret = -ENOENT;
3935 goto err;
3936 }
Chris Mason5f39d392007-10-15 16:14:19 -04003937 leaf = path->nodes[0];
3938 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003939 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003940 if (ret)
3941 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003942 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003943
Miao Xie67de1172013-12-26 13:07:06 +08003944 /*
3945 * If we don't have dir index, we have to get it by looking up
3946 * the inode ref, since we get the inode ref, remove it directly,
3947 * it is unnecessary to do delayed deletion.
3948 *
3949 * But if we have dir index, needn't search inode ref to get it.
3950 * Since the inode ref is close to the inode item, it is better
3951 * that we delay to delete it, and just do this deletion when
3952 * we update the inode item.
3953 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003954 if (inode->dir_index) {
3955 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003956 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003957 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003958 goto skip_backref;
3959 }
3960 }
3961
Li Zefan33345d012011-04-20 10:31:50 +08003962 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3963 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003964 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003965 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003966 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003967 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003968 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003969 goto err;
3970 }
Miao Xie67de1172013-12-26 13:07:06 +08003971skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003972 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003974 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003975 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 }
Chris Mason39279cc2007-06-12 06:35:45 -04003977
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003978 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3979 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003980 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003981 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 goto err;
3983 }
Chris Masone02119d2008-09-05 16:13:11 -04003984
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003985 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3986 index);
Chris Mason6418c962010-10-30 07:34:24 -04003987 if (ret == -ENOENT)
3988 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003989 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003990 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003991err:
3992 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003993 if (ret)
3994 goto out;
3995
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003996 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003997 inode_inc_iversion(&inode->vfs_inode);
3998 inode_inc_iversion(&dir->vfs_inode);
3999 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4000 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4001 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004002out:
Chris Mason39279cc2007-06-12 06:35:45 -04004003 return ret;
4004}
4005
Al Viro92986792011-03-04 17:14:37 +00004006int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4007 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004008 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004009 const char *name, int name_len)
4010{
4011 int ret;
4012 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4013 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004014 drop_nlink(&inode->vfs_inode);
4015 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004016 }
4017 return ret;
4018}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019
4020/*
4021 * helper to start transaction for unlink and rmdir.
4022 *
Josef Bacikd52be812013-05-29 14:54:47 -04004023 * unlink and rmdir are special in btrfs, they do not always free space, so
4024 * if we cannot make our reservations the normal way try and see if there is
4025 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4026 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027 */
Josef Bacikd52be812013-05-29 14:54:47 -04004028static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004030 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031
Josef Bacike70bea52011-10-11 14:18:24 -04004032 /*
4033 * 1 for the possible orphan item
4034 * 1 for the dir item
4035 * 1 for the dir index
4036 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004037 * 1 for the inode
4038 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004039 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040}
4041
Chris Mason39279cc2007-06-12 06:35:45 -04004042static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4043{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004045 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004046 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004047 int ret;
4048
Josef Bacikd52be812013-05-29 14:54:47 -04004049 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004050 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004051 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004052
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004053 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4054 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004055
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004056 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4057 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4058 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004059 if (ret)
4060 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004061
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004063 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004064 if (ret)
4065 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004066 }
Josef Bacik7b128762008-07-24 12:17:14 -04004067
Tsutomu Itohb5324022011-07-19 07:27:20 +00004068out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004069 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004070 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004071 return ret;
4072}
4073
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004074int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4075 struct btrfs_root *root,
4076 struct inode *dir, u64 objectid,
4077 const char *name, int name_len)
4078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004079 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004080 struct btrfs_path *path;
4081 struct extent_buffer *leaf;
4082 struct btrfs_dir_item *di;
4083 struct btrfs_key key;
4084 u64 index;
4085 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004086 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087
4088 path = btrfs_alloc_path();
4089 if (!path)
4090 return -ENOMEM;
4091
Li Zefan33345d012011-04-20 10:31:50 +08004092 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (IS_ERR_OR_NULL(di)) {
4095 if (!di)
4096 ret = -ENOENT;
4097 else
4098 ret = PTR_ERR(di);
4099 goto out;
4100 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 leaf = path->nodes[0];
4103 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4104 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4105 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004107 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 goto out;
4109 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004110 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004112 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4113 root->root_key.objectid, dir_ino,
4114 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 goto out;
4119 }
Li Zefan33345d012011-04-20 10:31:50 +08004120 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (IS_ERR_OR_NULL(di)) {
4123 if (!di)
4124 ret = -ENOENT;
4125 else
4126 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 goto out;
4129 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130
4131 leaf = path->nodes[0];
4132 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004133 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134 index = key.offset;
4135 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004136 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004137
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004138 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 goto out;
4142 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004143
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004144 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004145 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004146 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004147 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004149 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004151 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153}
4154
Chris Mason39279cc2007-06-12 06:35:45 -04004155static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4156{
David Howells2b0143b2015-03-17 22:25:59 +00004157 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004158 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004159 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004161 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004162
David Sterbab3ae2442012-09-13 16:04:34 -06004163 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004164 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004165 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004166 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004167
Josef Bacikd52be812013-05-29 14:54:47 -04004168 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004170 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004171
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004172 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 err = btrfs_unlink_subvol(trans, root, dir,
4174 BTRFS_I(inode)->location.objectid,
4175 dentry->d_name.name,
4176 dentry->d_name.len);
4177 goto out;
4178 }
4179
Josef Bacik7b128762008-07-24 12:17:14 -04004180 err = btrfs_orphan_add(trans, inode);
4181 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004183
Filipe Manana44f714d2016-06-06 16:11:13 +01004184 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4185
Chris Mason39279cc2007-06-12 06:35:45 -04004186 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004187 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4188 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4189 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004190 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004191 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004192 /*
4193 * Propagate the last_unlink_trans value of the deleted dir to
4194 * its parent directory. This is to prevent an unrecoverable
4195 * log tree in the case we do something like this:
4196 * 1) create dir foo
4197 * 2) create snapshot under dir foo
4198 * 3) delete the snapshot
4199 * 4) rmdir foo
4200 * 5) mkdir foo
4201 * 6) fsync foo or some file inside foo
4202 */
4203 if (last_unlink_trans >= trans->transid)
4204 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4205 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004207 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004208 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004209
Chris Mason39279cc2007-06-12 06:35:45 -04004210 return err;
4211}
4212
Chris Mason28f75a02015-02-04 06:59:29 -08004213static int truncate_space_check(struct btrfs_trans_handle *trans,
4214 struct btrfs_root *root,
4215 u64 bytes_deleted)
4216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004218 int ret;
4219
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004220 /*
4221 * This is only used to apply pressure to the enospc system, we don't
4222 * intend to use this reservation at all.
4223 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004224 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 bytes_deleted *= fs_info->nodesize;
4226 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004227 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004228 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004229 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004230 trans->transid,
4231 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004232 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004233 }
Chris Mason28f75a02015-02-04 06:59:29 -08004234 return ret;
4235
4236}
4237
Filipe Manana0305cd52015-10-16 12:34:25 +01004238static int truncate_inline_extent(struct inode *inode,
4239 struct btrfs_path *path,
4240 struct btrfs_key *found_key,
4241 const u64 item_end,
4242 const u64 new_size)
4243{
4244 struct extent_buffer *leaf = path->nodes[0];
4245 int slot = path->slots[0];
4246 struct btrfs_file_extent_item *fi;
4247 u32 size = (u32)(new_size - found_key->offset);
4248 struct btrfs_root *root = BTRFS_I(inode)->root;
4249
4250 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4251
4252 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4253 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004254 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004255
4256 /*
4257 * Zero out the remaining of the last page of our inline extent,
4258 * instead of directly truncating our inline extent here - that
4259 * would be much more complex (decompressing all the data, then
4260 * compressing the truncated data, which might be bigger than
4261 * the size of the inline extent, resize the extent, etc).
4262 * We release the path because to get the page we might need to
4263 * read the extent item from disk (data not in the page cache).
4264 */
4265 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304266 return btrfs_truncate_block(inode, offset, page_end - offset,
4267 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004268 }
4269
4270 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4271 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004272 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004273
4274 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4275 inode_sub_bytes(inode, item_end + 1 - new_size);
4276
4277 return 0;
4278}
4279
Chris Mason323ac952008-10-01 19:05:46 -04004280/*
Chris Mason39279cc2007-06-12 06:35:45 -04004281 * this can truncate away extent items, csum items and directory items.
4282 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004283 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004284 *
4285 * csum items that cross the new i_size are truncated to the new size
4286 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004287 *
4288 * min_type is the minimum key type to truncate down to. If set to 0, this
4289 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004290 */
Yan, Zheng80825102009-11-12 09:35:36 +00004291int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4292 struct btrfs_root *root,
4293 struct inode *inode,
4294 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004296 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004298 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004300 struct btrfs_key key;
4301 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004302 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004303 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004304 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004305 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004306 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004307 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004308 int found_extent;
4309 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004310 int pending_del_nr = 0;
4311 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004312 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004313 int ret;
4314 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004315 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004316 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004317 bool be_nice = 0;
4318 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004319 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004320
4321 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004322
Chris Mason28ed1342014-12-17 09:41:04 -08004323 /*
4324 * for non-free space inodes and ref cows, we want to back off from
4325 * time to time
4326 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004327 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004328 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4329 be_nice = 1;
4330
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004331 path = btrfs_alloc_path();
4332 if (!path)
4333 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004334 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004335
Josef Bacik5dc562c2012-08-17 13:14:17 -04004336 /*
4337 * We want to drop from the next block forward in case this new size is
4338 * not block aligned since we will be keeping the last block of the
4339 * extent just the way it is.
4340 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004341 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004342 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004343 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004345 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004346
Miao Xie16cdcec2011-04-22 18:12:22 +08004347 /*
4348 * This function is also used to drop the items in the log tree before
4349 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4350 * it is used to drop the loged items. So we shouldn't kill the delayed
4351 * items.
4352 */
4353 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004354 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004355
Li Zefan33345d012011-04-20 10:31:50 +08004356 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004358 key.type = (u8)-1;
4359
Chris Mason85e21ba2008-01-29 15:11:36 -05004360search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004361 /*
4362 * with a 16K leaf size and 128MB extents, you can actually queue
4363 * up a huge file in a single leaf. Most of the time that
4364 * bytes_deleted is > 0, it will be huge by the time we get here
4365 */
Byongho Leeee221842015-12-15 01:42:10 +09004366 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004367 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004368 err = -EAGAIN;
4369 goto error;
4370 }
4371 }
4372
4373
Chris Masonb9473432009-03-13 11:00:37 -04004374 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004376 if (ret < 0) {
4377 err = ret;
4378 goto out;
4379 }
Chris Masond3977122009-01-05 21:25:51 -05004380
Chris Mason85e21ba2008-01-29 15:11:36 -05004381 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004382 /* there are no items in the tree for us to truncate, we're
4383 * done
4384 */
Yan, Zheng80825102009-11-12 09:35:36 +00004385 if (path->slots[0] == 0)
4386 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004387 path->slots[0]--;
4388 }
4389
Chris Masond3977122009-01-05 21:25:51 -05004390 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004391 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004392 leaf = path->nodes[0];
4393 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004394 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004395
Li Zefan33345d012011-04-20 10:31:50 +08004396 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004397 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004398
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004400 break;
4401
Chris Mason5f39d392007-10-15 16:14:19 -04004402 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004404 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004405 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004406 extent_type = btrfs_file_extent_type(leaf, fi);
4407 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004408 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004409 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004410 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004411 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004412 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004413 }
Yan008630c2007-11-07 13:31:09 -05004414 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004415 }
Yan, Zheng80825102009-11-12 09:35:36 +00004416 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004417 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004418 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004419 if (item_end < new_size) {
4420 /*
4421 * With NO_HOLES mode, for the following mapping
4422 *
4423 * [0-4k][hole][8k-12k]
4424 *
4425 * if truncating isize down to 6k, it ends up
4426 * isize being 8k.
4427 */
4428 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4429 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004430 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004431 }
Yan, Zheng80825102009-11-12 09:35:36 +00004432 if (found_key.offset >= new_size)
4433 del_item = 1;
4434 else
4435 del_item = 0;
4436 }
Chris Mason39279cc2007-06-12 06:35:45 -04004437 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004438 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004439 if (found_type != BTRFS_EXTENT_DATA_KEY)
4440 goto delete;
4441
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004442 if (del_item)
4443 last_size = found_key.offset;
4444 else
4445 last_size = new_size;
4446
Chris Mason179e29e2007-11-01 11:28:41 -04004447 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004448 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004449 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004450 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004451 u64 orig_num_bytes =
4452 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004453 extent_num_bytes = ALIGN(new_size -
4454 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004455 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004456 btrfs_set_file_extent_num_bytes(leaf, fi,
4457 extent_num_bytes);
4458 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004459 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004460 if (test_bit(BTRFS_ROOT_REF_COWS,
4461 &root->state) &&
4462 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004463 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004464 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004465 } else {
Chris Masondb945352007-10-15 16:15:53 -04004466 extent_num_bytes =
4467 btrfs_file_extent_disk_num_bytes(leaf,
4468 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004469 extent_offset = found_key.offset -
4470 btrfs_file_extent_offset(leaf, fi);
4471
Chris Mason39279cc2007-06-12 06:35:45 -04004472 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004473 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004474 if (extent_start != 0) {
4475 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004476 if (test_bit(BTRFS_ROOT_REF_COWS,
4477 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004478 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
4480 }
Chris Mason90692182008-02-08 13:49:28 -05004481 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004482 /*
4483 * we can't truncate inline items that have had
4484 * special encodings
4485 */
4486 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004487 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4488 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004489
4490 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004491 * Need to release path in order to truncate a
4492 * compressed extent. So delete any accumulated
4493 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004494 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004495 if (btrfs_file_extent_compression(leaf, fi) !=
4496 BTRFS_COMPRESS_NONE && pending_del_nr) {
4497 err = btrfs_del_items(trans, root, path,
4498 pending_del_slot,
4499 pending_del_nr);
4500 if (err) {
4501 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004502 err);
4503 goto error;
4504 }
4505 pending_del_nr = 0;
4506 }
4507
4508 err = truncate_inline_extent(inode, path,
4509 &found_key,
4510 item_end,
4511 new_size);
4512 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004513 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004514 goto error;
4515 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004516 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4517 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004518 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004519 }
Chris Mason39279cc2007-06-12 06:35:45 -04004520 }
Chris Mason179e29e2007-11-01 11:28:41 -04004521delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004522 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 if (!pending_del_nr) {
4524 /* no pending yet, add ourselves */
4525 pending_del_slot = path->slots[0];
4526 pending_del_nr = 1;
4527 } else if (pending_del_nr &&
4528 path->slots[0] + 1 == pending_del_slot) {
4529 /* hop on the pending chunk */
4530 pending_del_nr++;
4531 pending_del_slot = path->slots[0];
4532 } else {
Chris Masond3977122009-01-05 21:25:51 -05004533 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004534 }
Chris Mason39279cc2007-06-12 06:35:45 -04004535 } else {
4536 break;
4537 }
Chris Mason28f75a02015-02-04 06:59:29 -08004538 should_throttle = 0;
4539
Miao Xie27cdeb72014-04-02 19:51:05 +08004540 if (found_extent &&
4541 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004542 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004543 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004544 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004545 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004546 extent_num_bytes, 0,
4547 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004548 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004549 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004550 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4551 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004552 trans->delayed_ref_updates * 2,
4553 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004554 if (be_nice) {
4555 if (truncate_space_check(trans, root,
4556 extent_num_bytes)) {
4557 should_end = 1;
4558 }
4559 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004560 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004561 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004562 }
Chris Mason39279cc2007-06-12 06:35:45 -04004563 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004564
Yan, Zheng80825102009-11-12 09:35:36 +00004565 if (found_type == BTRFS_INODE_ITEM_KEY)
4566 break;
4567
4568 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004569 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004570 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004571 if (pending_del_nr) {
4572 ret = btrfs_del_items(trans, root, path,
4573 pending_del_slot,
4574 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004575 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004576 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004577 goto error;
4578 }
Yan, Zheng80825102009-11-12 09:35:36 +00004579 pending_del_nr = 0;
4580 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004581 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004582 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004583 unsigned long updates = trans->delayed_ref_updates;
4584 if (updates) {
4585 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004586 ret = btrfs_run_delayed_refs(trans,
4587 fs_info,
4588 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004589 if (ret && !err)
4590 err = ret;
4591 }
4592 }
Chris Mason28f75a02015-02-04 06:59:29 -08004593 /*
4594 * if we failed to refill our space rsv, bail out
4595 * and let the transaction restart
4596 */
4597 if (should_end) {
4598 err = -EAGAIN;
4599 goto error;
4600 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004601 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004602 } else {
4603 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004604 }
Chris Mason39279cc2007-06-12 06:35:45 -04004605 }
Yan, Zheng80825102009-11-12 09:35:36 +00004606out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004607 if (pending_del_nr) {
4608 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4609 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004610 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004611 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004612 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004613error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004614 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004615 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004616
Chris Mason39279cc2007-06-12 06:35:45 -04004617 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004618
Liu Bo19fd2df2016-12-01 13:01:02 -08004619 if (err == 0) {
4620 /* only inline file may have last_size != new_size */
4621 if (new_size >= fs_info->sectorsize ||
4622 new_size > fs_info->max_inline)
4623 ASSERT(last_size == new_size);
4624 }
4625
Byongho Leeee221842015-12-15 01:42:10 +09004626 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004627 unsigned long updates = trans->delayed_ref_updates;
4628 if (updates) {
4629 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004630 ret = btrfs_run_delayed_refs(trans, fs_info,
4631 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004632 if (ret && !err)
4633 err = ret;
4634 }
4635 }
Yan, Zheng80825102009-11-12 09:35:36 +00004636 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004637}
4638
Chris Masona52d9a82007-08-27 16:49:44 -04004639/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304640 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004641 * @inode - inode that we're zeroing
4642 * @from - the offset to start zeroing
4643 * @len - the length to zero, 0 to zero the entire range respective to the
4644 * offset
4645 * @front - zero up to the offset instead of from the offset on
4646 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304647 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004648 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004649 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304650int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004651 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004652{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004653 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004654 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004655 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4656 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004657 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004658 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004660 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304661 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004662 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004663 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004664 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304665 u64 block_start;
4666 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004667
Josef Bacik2aaa6652012-08-29 14:27:18 -04004668 if ((offset & (blocksize - 1)) == 0 &&
4669 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004670 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304671
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004672 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304673 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004674 if (ret)
4675 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004676
Chris Mason211c17f2008-05-15 09:13:45 -04004677again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004678 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004679 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004680 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304681 round_down(from, blocksize),
4682 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004683 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004684 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004685 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304687 block_start = round_down(from, blocksize);
4688 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004689
Chris Masona52d9a82007-08-27 16:49:44 -04004690 if (!PageUptodate(page)) {
4691 ret = btrfs_readpage(NULL, page);
4692 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004693 if (page->mapping != mapping) {
4694 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004695 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004696 goto again;
4697 }
Chris Masona52d9a82007-08-27 16:49:44 -04004698 if (!PageUptodate(page)) {
4699 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004700 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004701 }
4702 }
Chris Mason211c17f2008-05-15 09:13:45 -04004703 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004704
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004706 set_page_extent_mapped(page);
4707
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304708 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004709 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304710 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004711 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004712 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004713 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004714 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004715 btrfs_put_ordered_extent(ordered);
4716 goto again;
4717 }
4718
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004720 EXTENT_DIRTY | EXTENT_DELALLOC |
4721 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004722 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004723
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304724 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004725 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004726 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004728 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004729 goto out_unlock;
4730 }
4731
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004733 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004735 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004736 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304737 memset(kaddr + (block_start - page_offset(page)),
4738 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004739 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 memset(kaddr + (block_start - page_offset(page)) + offset,
4741 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004742 flush_dcache_page(page);
4743 kunmap(page);
4744 }
Chris Mason247e7432008-07-17 12:53:51 -04004745 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004746 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004748 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004749
Chris Mason89642222008-07-24 09:41:53 -04004750out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004751 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304752 btrfs_delalloc_release_space(inode, block_start,
4753 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004754 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004755 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004756out:
4757 return ret;
4758}
4759
Josef Bacik16e75492013-10-22 12:18:51 -04004760static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4761 u64 offset, u64 len)
4762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004764 struct btrfs_trans_handle *trans;
4765 int ret;
4766
4767 /*
4768 * Still need to make sure the inode looks like it's been updated so
4769 * that any holes get logged if we fsync.
4770 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4772 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004773 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4774 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4775 return 0;
4776 }
4777
4778 /*
4779 * 1 - for the one we're dropping
4780 * 1 - for the one we're adding
4781 * 1 - for updating the inode.
4782 */
4783 trans = btrfs_start_transaction(root, 3);
4784 if (IS_ERR(trans))
4785 return PTR_ERR(trans);
4786
4787 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4788 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004789 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004790 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004791 return ret;
4792 }
4793
David Sterbaf85b7372017-01-20 14:54:07 +01004794 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4795 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004796 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004797 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004798 else
4799 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004800 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004801 return ret;
4802}
4803
Josef Bacik695a0d02011-03-04 15:46:53 -05004804/*
4805 * This function puts in dummy file extents for the area we're creating a hole
4806 * for. So if we are truncating this file to a larger size we need to insert
4807 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4808 * the range between oldsize and size
4809 */
Josef Bacika41ad392011-01-31 15:30:16 -05004810int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004813 struct btrfs_root *root = BTRFS_I(inode)->root;
4814 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004815 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004816 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004817 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4819 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004820 u64 last_byte;
4821 u64 cur_offset;
4822 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004823 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004824
Josef Bacika71754f2013-06-17 17:14:39 -04004825 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 * If our size started in the middle of a block we need to zero out the
4827 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004828 * expose stale data.
4829 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004831 if (err)
4832 return err;
4833
Yan Zheng9036c102008-10-30 14:19:41 -04004834 if (size <= hole_start)
4835 return 0;
4836
Yan Zheng9036c102008-10-30 14:19:41 -04004837 while (1) {
4838 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004839
David Sterbaff13db42015-12-03 14:30:40 +01004840 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004841 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004842 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004843 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004844 if (!ordered)
4845 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004846 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4847 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004848 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004849 btrfs_put_ordered_extent(ordered);
4850 }
4851
Yan Zheng9036c102008-10-30 14:19:41 -04004852 cur_offset = hole_start;
4853 while (1) {
4854 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4855 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004856 if (IS_ERR(em)) {
4857 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004858 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004859 break;
4860 }
Yan Zheng9036c102008-10-30 14:19:41 -04004861 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004862 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004863 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004864 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004865 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004866
Josef Bacik16e75492013-10-22 12:18:51 -04004867 err = maybe_insert_hole(root, inode, cur_offset,
4868 hole_size);
4869 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004870 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004871 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004872 cur_offset + hole_size - 1, 0);
4873 hole_em = alloc_extent_map();
4874 if (!hole_em) {
4875 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4876 &BTRFS_I(inode)->runtime_flags);
4877 goto next;
4878 }
4879 hole_em->start = cur_offset;
4880 hole_em->len = hole_size;
4881 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004882
Josef Bacik5dc562c2012-08-17 13:14:17 -04004883 hole_em->block_start = EXTENT_MAP_HOLE;
4884 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004885 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004886 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004888 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004890
4891 while (1) {
4892 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004893 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004894 write_unlock(&em_tree->lock);
4895 if (err != -EEXIST)
4896 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004897 btrfs_drop_extent_cache(BTRFS_I(inode),
4898 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004899 cur_offset +
4900 hole_size - 1, 0);
4901 }
4902 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004903 }
Josef Bacik16e75492013-10-22 12:18:51 -04004904next:
Yan Zheng9036c102008-10-30 14:19:41 -04004905 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004906 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004907 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004908 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004909 break;
4910 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004911 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004912 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4913 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004914 return err;
4915}
4916
Eric Sandeen3972f262013-01-12 02:57:22 +00004917static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004918{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004919 struct btrfs_root *root = BTRFS_I(inode)->root;
4920 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004921 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004922 loff_t newsize = attr->ia_size;
4923 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004924 int ret;
4925
Eric Sandeen3972f262013-01-12 02:57:22 +00004926 /*
4927 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4928 * special case where we need to update the times despite not having
4929 * these flags set. For all other operations the VFS set these flags
4930 * explicitly if it wants a timestamp update.
4931 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004932 if (newsize != oldsize) {
4933 inode_inc_iversion(inode);
4934 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4935 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004936 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004937 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004938
Josef Bacika41ad392011-01-31 15:30:16 -05004939 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004940 /*
4941 * Don't do an expanding truncate while snapshoting is ongoing.
4942 * This is to ensure the snapshot captures a fully consistent
4943 * state of this file - if the snapshot captures this expanding
4944 * truncation, it must capture all writes that happened before
4945 * this truncation.
4946 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004947 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004948 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 if (ret) {
4950 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004951 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 }
Yan, Zheng80825102009-11-12 09:35:36 +00004953
Miao Xief4a2f4c2011-12-14 20:12:01 -05004954 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004955 if (IS_ERR(trans)) {
4956 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004958 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004959
4960 i_size_write(inode, newsize);
4961 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304962 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004963 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004964 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004965 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004966 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004967
Josef Bacika41ad392011-01-31 15:30:16 -05004968 /*
4969 * We're truncating a file that used to have good data down to
4970 * zero. Make sure it gets into the ordered flush list so that
4971 * any new writes get down to disk quickly.
4972 */
4973 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004974 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4975 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004976
Josef Bacikf3fe8202013-01-07 17:03:21 -05004977 /*
4978 * 1 for the orphan item we're going to add
4979 * 1 for the orphan item deletion.
4980 */
4981 trans = btrfs_start_transaction(root, 2);
4982 if (IS_ERR(trans))
4983 return PTR_ERR(trans);
4984
4985 /*
4986 * We need to do this in case we fail at _any_ point during the
4987 * actual truncate. Once we do the truncate_setsize we could
4988 * invalidate pages which forces any outstanding ordered io to
4989 * be instantly completed which will give us extents that need
4990 * to be truncated. If we fail to get an orphan inode down we
4991 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004992 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004993 * will be consistent.
4994 */
4995 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004996 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004997 if (ret)
4998 return ret;
4999
Josef Bacika41ad392011-01-31 15:30:16 -05005000 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5001 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005002
5003 /* Disable nonlocked read DIO to avoid the end less truncate */
5004 btrfs_inode_block_unlocked_dio(inode);
5005 inode_dio_wait(inode);
5006 btrfs_inode_resume_unlocked_dio(inode);
5007
Josef Bacika41ad392011-01-31 15:30:16 -05005008 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005009 if (ret && inode->i_nlink) {
5010 int err;
5011
Liu Bo19fd2df2016-12-01 13:01:02 -08005012 /* To get a stable disk_i_size */
5013 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5014 if (err) {
5015 btrfs_orphan_del(NULL, inode);
5016 return err;
5017 }
5018
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005019 /*
5020 * failed to truncate, disk_i_size is only adjusted down
5021 * as we remove extents, so it should represent the true
5022 * size of the inode, so reset the in memory size and
5023 * delete our orphan entry.
5024 */
5025 trans = btrfs_join_transaction(root);
5026 if (IS_ERR(trans)) {
5027 btrfs_orphan_del(NULL, inode);
5028 return ret;
5029 }
5030 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5031 err = btrfs_orphan_del(trans, inode);
5032 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005033 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005034 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005035 }
Yan, Zheng80825102009-11-12 09:35:36 +00005036 }
5037
Josef Bacika41ad392011-01-31 15:30:16 -05005038 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005039}
5040
Chris Mason39279cc2007-06-12 06:35:45 -04005041static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5042{
David Howells2b0143b2015-03-17 22:25:59 +00005043 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005044 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005045 int err;
5046
Li Zefanb83cc962010-12-20 16:04:08 +08005047 if (btrfs_root_readonly(root))
5048 return -EROFS;
5049
Jan Kara31051c82016-05-26 16:55:18 +02005050 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005051 if (err)
5052 return err;
5053
Chris Mason5a3f23d2009-03-31 13:27:11 -04005054 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005055 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005056 if (err)
5057 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005058 }
Yan Zheng9036c102008-10-30 14:19:41 -04005059
Christoph Hellwig10257742010-06-04 11:30:02 +02005060 if (attr->ia_valid) {
5061 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005062 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005063 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005064
Josef Bacik22c44fe2011-11-30 10:45:38 -05005065 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005066 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005067 }
5068
Chris Mason39279cc2007-06-12 06:35:45 -04005069 return err;
5070}
Chris Mason61295eb2008-01-14 16:24:38 -05005071
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005072/*
5073 * While truncating the inode pages during eviction, we get the VFS calling
5074 * btrfs_invalidatepage() against each page of the inode. This is slow because
5075 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5076 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5077 * extent_state structures over and over, wasting lots of time.
5078 *
5079 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5080 * those expensive operations on a per page basis and do only the ordered io
5081 * finishing, while we release here the extent_map and extent_state structures,
5082 * without the excessive merging and splitting.
5083 */
5084static void evict_inode_truncate_pages(struct inode *inode)
5085{
5086 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5087 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5088 struct rb_node *node;
5089
5090 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005091 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005092
5093 write_lock(&map_tree->lock);
5094 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5095 struct extent_map *em;
5096
5097 node = rb_first(&map_tree->map);
5098 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005099 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5100 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005101 remove_extent_mapping(map_tree, em);
5102 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005103 if (need_resched()) {
5104 write_unlock(&map_tree->lock);
5105 cond_resched();
5106 write_lock(&map_tree->lock);
5107 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005108 }
5109 write_unlock(&map_tree->lock);
5110
Filipe Manana6ca07092015-05-26 00:55:42 +01005111 /*
5112 * Keep looping until we have no more ranges in the io tree.
5113 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005114 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5115 * still in progress (unlocked the pages in the bio but did not yet
5116 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005117 * ranges can still be locked and eviction started because before
5118 * submitting those bios, which are executed by a separate task (work
5119 * queue kthread), inode references (inode->i_count) were not taken
5120 * (which would be dropped in the end io callback of each bio).
5121 * Therefore here we effectively end up waiting for those bios and
5122 * anyone else holding locked ranges without having bumped the inode's
5123 * reference count - if we don't do it, when they access the inode's
5124 * io_tree to unlock a range it may be too late, leading to an
5125 * use-after-free issue.
5126 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005127 spin_lock(&io_tree->lock);
5128 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5129 struct extent_state *state;
5130 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005131 u64 start;
5132 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005133
5134 node = rb_first(&io_tree->state);
5135 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005136 start = state->start;
5137 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005138 spin_unlock(&io_tree->lock);
5139
David Sterbaff13db42015-12-03 14:30:40 +01005140 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005141
5142 /*
5143 * If still has DELALLOC flag, the extent didn't reach disk,
5144 * and its reserved space won't be freed by delayed_ref.
5145 * So we need to free its reserved space here.
5146 * (Refer to comment in btrfs_invalidatepage, case 2)
5147 *
5148 * Note, end is the bytenr of last byte, so we need + 1 here.
5149 */
5150 if (state->state & EXTENT_DELALLOC)
5151 btrfs_qgroup_free_data(inode, start, end - start + 1);
5152
Filipe Manana6ca07092015-05-26 00:55:42 +01005153 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005154 EXTENT_LOCKED | EXTENT_DIRTY |
5155 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5156 EXTENT_DEFRAG, 1, 1,
5157 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005158
Filipe Manana7064dd52014-08-08 02:47:05 +01005159 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005160 spin_lock(&io_tree->lock);
5161 }
5162 spin_unlock(&io_tree->lock);
5163}
5164
Al Virobd555972010-06-07 11:35:40 -04005165void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005166{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005168 struct btrfs_trans_handle *trans;
5169 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005170 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005171 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005172 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005173 int ret;
5174
liubo1abe9b82011-03-24 11:18:59 +00005175 trace_btrfs_inode_evict(inode);
5176
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005177 if (!root) {
5178 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5179 return;
5180 }
5181
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005182 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005183
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005184 evict_inode_truncate_pages(inode);
5185
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005186 if (inode->i_nlink &&
5187 ((btrfs_root_refs(&root->root_item) != 0 &&
5188 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005189 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005190 goto no_delete;
5191
Chris Mason39279cc2007-06-12 06:35:45 -04005192 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005193 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005194 goto no_delete;
5195 }
Al Virobd555972010-06-07 11:35:40 -04005196 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005197 if (!special_file(inode->i_mode))
5198 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005199
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005200 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005202 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005203 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005204 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005205 goto no_delete;
5206 }
5207
Yan, Zheng76dda932009-09-21 16:00:26 -04005208 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005209 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5210 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005211 goto no_delete;
5212 }
5213
Nikolay Borisovaa790212017-01-10 20:35:40 +02005214 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005215 if (ret) {
5216 btrfs_orphan_del(NULL, inode);
5217 goto no_delete;
5218 }
5219
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005220 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005221 if (!rsv) {
5222 btrfs_orphan_del(NULL, inode);
5223 goto no_delete;
5224 }
Josef Bacik4a338542011-08-29 11:01:31 -04005225 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005226 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005227 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005228
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005229 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005230
Josef Bacik4289a662011-08-05 13:22:24 -04005231 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005232 * This is a bit simpler than btrfs_truncate since we've already
5233 * reserved our space for our orphan item in the unlink, so we just
5234 * need to reserve some slack space in case we add bytes and update
5235 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005236 */
Yan, Zheng80825102009-11-12 09:35:36 +00005237 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005238 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5239 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005240
Josef Bacik726c35f2011-09-26 15:46:06 -04005241 /*
5242 * Try and steal from the global reserve since we will
5243 * likely not use this space anyway, we want to try as
5244 * hard as possible to get this to work.
5245 */
5246 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005247 steal_from_global++;
5248 else
5249 steal_from_global = 0;
5250 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005251
Josef Bacik3bce8762015-02-24 12:35:51 -08005252 /*
5253 * steal_from_global == 0: we reserved stuff, hooray!
5254 * steal_from_global == 1: we didn't reserve stuff, boo!
5255 * steal_from_global == 2: we've committed, still not a lot of
5256 * room but maybe we'll have room in the global reserve this
5257 * time.
5258 * steal_from_global == 3: abandon all hope!
5259 */
5260 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 btrfs_warn(fs_info,
5262 "Could not get space for a delete, will truncate on mount %d",
5263 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005264 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005265 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005266 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005267 }
5268
Miao Xie0e8c36a2012-12-19 06:59:51 +00005269 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005270 if (IS_ERR(trans)) {
5271 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005272 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005273 goto no_delete;
5274 }
5275
Josef Bacik3bce8762015-02-24 12:35:51 -08005276 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005277 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005278 * sure there is room to do it, if not we need to commit and try
5279 * again.
5280 */
5281 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005282 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005283 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005284 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005285 else
5286 ret = -ENOSPC;
5287 }
5288
5289 /*
5290 * Couldn't steal from the global reserve, we have too much
5291 * pending stuff built up, commit the transaction and try it
5292 * again.
5293 */
5294 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005295 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005296 if (ret) {
5297 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005298 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005299 goto no_delete;
5300 }
5301 continue;
5302 } else {
5303 steal_from_global = 0;
5304 }
5305
Josef Bacik4289a662011-08-05 13:22:24 -04005306 trans->block_rsv = rsv;
5307
Yan, Zhengd68fc572010-05-16 10:49:58 -04005308 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005309 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005310 break;
5311
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005312 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005313 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005314 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005315 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005316 }
5317
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005318 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005319
Josef Bacik4ef31a42013-08-13 14:10:08 -04005320 /*
5321 * Errors here aren't a big deal, it just means we leave orphan items
5322 * in the tree. They will be cleaned up on the next mount.
5323 */
Yan, Zheng80825102009-11-12 09:35:36 +00005324 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005325 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005326 btrfs_orphan_del(trans, inode);
5327 } else {
5328 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005329 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005330
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005331 trans->block_rsv = &fs_info->trans_block_rsv;
5332 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005333 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005334 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005335
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005336 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005337 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005338no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005339 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005340 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005341}
5342
5343/*
5344 * this returns the key found in the dir entry in the location pointer.
5345 * If no dir entries were found, location->objectid is 0.
5346 */
5347static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5348 struct btrfs_key *location)
5349{
5350 const char *name = dentry->d_name.name;
5351 int namelen = dentry->d_name.len;
5352 struct btrfs_dir_item *di;
5353 struct btrfs_path *path;
5354 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005355 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005356
5357 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005358 if (!path)
5359 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005360
David Sterbaf85b7372017-01-20 14:54:07 +01005361 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5362 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005363 if (IS_ERR(di))
5364 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005365
David Sterbac7040052011-04-19 18:00:01 +02005366 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005367 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005368
Chris Mason5f39d392007-10-15 16:14:19 -04005369 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005370out:
Chris Mason39279cc2007-06-12 06:35:45 -04005371 btrfs_free_path(path);
5372 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005373out_err:
5374 location->objectid = 0;
5375 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005376}
5377
5378/*
5379 * when we hit a tree root in a directory, the btrfs part of the inode
5380 * needs to be changed to reflect the root directory of the tree root. This
5381 * is kind of like crossing a mount point.
5382 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005383static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 struct inode *dir,
5385 struct dentry *dentry,
5386 struct btrfs_key *location,
5387 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005388{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 struct btrfs_path *path;
5390 struct btrfs_root *new_root;
5391 struct btrfs_root_ref *ref;
5392 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005393 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 int ret;
5395 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005396
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005397 path = btrfs_alloc_path();
5398 if (!path) {
5399 err = -ENOMEM;
5400 goto out;
5401 }
Chris Mason39279cc2007-06-12 06:35:45 -04005402
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005404 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5405 key.type = BTRFS_ROOT_REF_KEY;
5406 key.offset = location->objectid;
5407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005408 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 if (ret) {
5410 if (ret < 0)
5411 err = ret;
5412 goto out;
5413 }
Chris Mason39279cc2007-06-12 06:35:45 -04005414
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005415 leaf = path->nodes[0];
5416 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005417 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5419 goto out;
5420
5421 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5422 (unsigned long)(ref + 1),
5423 dentry->d_name.len);
5424 if (ret)
5425 goto out;
5426
David Sterbab3b4aa72011-04-21 01:20:15 +02005427 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 if (IS_ERR(new_root)) {
5431 err = PTR_ERR(new_root);
5432 goto out;
5433 }
5434
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005435 *sub_root = new_root;
5436 location->objectid = btrfs_root_dirid(&new_root->root_item);
5437 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005438 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005439 err = 0;
5440out:
5441 btrfs_free_path(path);
5442 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005443}
5444
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005445static void inode_tree_add(struct inode *inode)
5446{
5447 struct btrfs_root *root = BTRFS_I(inode)->root;
5448 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005449 struct rb_node **p;
5450 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005451 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005452 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453
Al Viro1d3382cb2010-10-23 15:19:20 -04005454 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005455 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005456 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005457 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005458 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005459 while (*p) {
5460 parent = *p;
5461 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5462
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005463 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005464 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005465 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005466 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005467 else {
5468 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005469 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005470 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005471 RB_CLEAR_NODE(parent);
5472 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005473 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005474 }
5475 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005476 rb_link_node(new, parent, p);
5477 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 spin_unlock(&root->inode_lock);
5479}
5480
5481static void inode_tree_del(struct inode *inode)
5482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005487 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005488 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005490 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005492 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005493 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005494
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005495 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005496 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005497 spin_lock(&root->inode_lock);
5498 empty = RB_EMPTY_ROOT(&root->inode_tree);
5499 spin_unlock(&root->inode_lock);
5500 if (empty)
5501 btrfs_add_dead_root(root);
5502 }
5503}
5504
Jeff Mahoney143bede2012-03-01 14:56:26 +01005505void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005507 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005508 struct rb_node *node;
5509 struct rb_node *prev;
5510 struct btrfs_inode *entry;
5511 struct inode *inode;
5512 u64 objectid = 0;
5513
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005514 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005515 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005516
5517 spin_lock(&root->inode_lock);
5518again:
5519 node = root->inode_tree.rb_node;
5520 prev = NULL;
5521 while (node) {
5522 prev = node;
5523 entry = rb_entry(node, struct btrfs_inode, rb_node);
5524
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005525 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005527 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005528 node = node->rb_right;
5529 else
5530 break;
5531 }
5532 if (!node) {
5533 while (prev) {
5534 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005535 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005536 node = prev;
5537 break;
5538 }
5539 prev = rb_next(prev);
5540 }
5541 }
5542 while (node) {
5543 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005544 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005545 inode = igrab(&entry->vfs_inode);
5546 if (inode) {
5547 spin_unlock(&root->inode_lock);
5548 if (atomic_read(&inode->i_count) > 1)
5549 d_prune_aliases(inode);
5550 /*
Al Viro45321ac2010-06-07 13:43:19 -04005551 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005552 * the inode cache when its usage count
5553 * hits zero.
5554 */
5555 iput(inode);
5556 cond_resched();
5557 spin_lock(&root->inode_lock);
5558 goto again;
5559 }
5560
5561 if (cond_resched_lock(&root->inode_lock))
5562 goto again;
5563
5564 node = rb_next(node);
5565 }
5566 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567}
5568
Chris Masone02119d2008-09-05 16:13:11 -04005569static int btrfs_init_locked_inode(struct inode *inode, void *p)
5570{
5571 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005572 inode->i_ino = args->location->objectid;
5573 memcpy(&BTRFS_I(inode)->location, args->location,
5574 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005575 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005576 return 0;
5577}
5578
5579static int btrfs_find_actor(struct inode *inode, void *opaque)
5580{
5581 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005582 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005583 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005584}
5585
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005587 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005589{
5590 struct inode *inode;
5591 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005592 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005593
Chris Mason90d3e592014-01-09 17:28:00 -08005594 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005595 args.root = root;
5596
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005597 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005598 btrfs_init_locked_inode,
5599 (void *)&args);
5600 return inode;
5601}
5602
Balaji Rao1a54ef82008-07-21 02:01:04 +05305603/* Get an inode object given its location and corresponding root.
5604 * Returns in *is_new if the inode was read from disk
5605 */
5606struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005607 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305608{
5609 struct inode *inode;
5610
Chris Mason90d3e592014-01-09 17:28:00 -08005611 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305612 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305614
5615 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005616 int ret;
5617
5618 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005619 if (!is_bad_inode(inode)) {
5620 inode_tree_add(inode);
5621 unlock_new_inode(inode);
5622 if (new)
5623 *new = 1;
5624 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005625 unlock_new_inode(inode);
5626 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005627 ASSERT(ret < 0);
5628 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005629 }
5630 }
5631
Balaji Rao1a54ef82008-07-21 02:01:04 +05305632 return inode;
5633}
5634
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635static struct inode *new_simple_dir(struct super_block *s,
5636 struct btrfs_key *key,
5637 struct btrfs_root *root)
5638{
5639 struct inode *inode = new_inode(s);
5640
5641 if (!inode)
5642 return ERR_PTR(-ENOMEM);
5643
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644 BTRFS_I(inode)->root = root;
5645 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005646 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005647
5648 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005649 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005650 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 inode->i_fop = &simple_dir_operations;
5652 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005653 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305654 inode->i_atime = inode->i_mtime;
5655 inode->i_ctime = inode->i_mtime;
5656 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657
5658 return inode;
5659}
5660
Chris Mason3de45862008-11-17 21:02:50 -05005661struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005662{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005663 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005664 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005665 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 struct btrfs_root *sub_root = root;
5667 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005668 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005669 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005670
5671 if (dentry->d_name.len > BTRFS_NAME_LEN)
5672 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005673
Jeff Layton39e3c952012-11-28 11:30:53 -05005674 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005675 if (ret < 0)
5676 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005677
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005679 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005680
5681 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005682 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005683 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005684 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685
5686 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5687
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005688 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005689 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005690 &location, &sub_root);
5691 if (ret < 0) {
5692 if (ret != -ENOENT)
5693 inode = ERR_PTR(ret);
5694 else
5695 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5696 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005697 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005698 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005699 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005700
Julia Lawall34d19ba2011-01-24 19:55:19 +00005701 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005702 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005703 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005704 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005705 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005706 if (ret) {
5707 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005708 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005709 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005710 }
5711
Chris Mason3de45862008-11-17 21:02:50 -05005712 return inode;
5713}
5714
Nick Pigginfe15ce42011-01-07 17:49:23 +11005715static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005716{
5717 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005718 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005719
Li Zefan848cce02012-02-21 17:04:28 +08005720 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005721 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005722
Li Zefan848cce02012-02-21 17:04:28 +08005723 if (inode) {
5724 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005725 if (btrfs_root_refs(&root->root_item) == 0)
5726 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005727
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005728 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005729 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005730 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005731 return 0;
5732}
5733
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005734static void btrfs_dentry_release(struct dentry *dentry)
5735{
Daeseok Youn944a4512014-04-14 15:37:02 +09005736 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005737}
5738
Chris Mason3de45862008-11-17 21:02:50 -05005739static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005740 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005741{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005742 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005743
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005744 inode = btrfs_lookup_dentry(dir, dentry);
5745 if (IS_ERR(inode)) {
5746 if (PTR_ERR(inode) == -ENOENT)
5747 inode = NULL;
5748 else
5749 return ERR_CAST(inode);
5750 }
5751
Al Viro41d28bc2014-10-12 22:24:21 -04005752 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005753}
5754
Miao Xie16cdcec2011-04-22 18:12:22 +08005755unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005756 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5757};
5758
Al Viro9cdda8d2013-05-22 16:48:09 -04005759static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005760{
Al Viro9cdda8d2013-05-22 16:48:09 -04005761 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005763 struct btrfs_root *root = BTRFS_I(inode)->root;
5764 struct btrfs_item *item;
5765 struct btrfs_dir_item *di;
5766 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005767 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005769 struct list_head ins_list;
5770 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005771 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005772 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005774 unsigned char d_type;
5775 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005776 char tmp_name[32];
5777 char *name_ptr;
5778 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005779 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005780 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005781
Al Viro9cdda8d2013-05-22 16:48:09 -04005782 if (!dir_emit_dots(file, ctx))
5783 return 0;
5784
David Woodhouse49593bf2008-08-17 17:08:36 +01005785 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005786 if (!path)
5787 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005788
David Sterbae4058b52015-11-27 16:31:35 +01005789 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005790
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005791 INIT_LIST_HEAD(&ins_list);
5792 INIT_LIST_HEAD(&del_list);
5793 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005794
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005795 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005796 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005797 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005798
Chris Mason39279cc2007-06-12 06:35:45 -04005799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5800 if (ret < 0)
5801 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005802
5803 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005804 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005805 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005806 if (slot >= btrfs_header_nritems(leaf)) {
5807 ret = btrfs_next_leaf(root, path);
5808 if (ret < 0)
5809 goto err;
5810 else if (ret > 0)
5811 break;
5812 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005813 }
Chris Mason3de45862008-11-17 21:02:50 -05005814
Ross Kirkdd3cc162013-09-16 15:58:09 +01005815 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005816 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5817
5818 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005819 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005820 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005821 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005822 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005823 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005824 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005825 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005826
Al Viro9cdda8d2013-05-22 16:48:09 -04005827 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005828
Chris Mason39279cc2007-06-12 06:35:45 -04005829 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005830 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005831 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005832
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005833 name_len = btrfs_dir_name_len(leaf, di);
5834 if (name_len <= sizeof(tmp_name)) {
5835 name_ptr = tmp_name;
5836 } else {
5837 name_ptr = kmalloc(name_len, GFP_KERNEL);
5838 if (!name_ptr) {
5839 ret = -ENOMEM;
5840 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005841 }
Chris Mason39279cc2007-06-12 06:35:45 -04005842 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005843 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5844 name_len);
5845
5846 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5847 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5848
5849 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5850 d_type);
5851
5852 if (name_ptr != tmp_name)
5853 kfree(name_ptr);
5854
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005855 if (over)
5856 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005857 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005858next:
5859 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005860 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005861
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005862 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005863 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005864 goto nopos;
5865
Zach Browndb62efb2013-07-11 16:19:42 -07005866 /*
5867 * Stop new entries from being returned after we return the last
5868 * entry.
5869 *
5870 * New directory entries are assigned a strictly increasing
5871 * offset. This means that new entries created during readdir
5872 * are *guaranteed* to be seen in the future by that readdir.
5873 * This has broken buggy programs which operate on names as
5874 * they're returned by readdir. Until we re-use freed offsets
5875 * we have this hack to stop new entries from being returned
5876 * under the assumption that they'll never reach this huge
5877 * offset.
5878 *
5879 * This is being careful not to overflow 32bit loff_t unless the
5880 * last entry requires it because doing so has broken 32bit apps
5881 * in the past.
5882 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005883 if (ctx->pos >= INT_MAX)
5884 ctx->pos = LLONG_MAX;
5885 else
5886 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005887nopos:
5888 ret = 0;
5889err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005890 if (put)
5891 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005892 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 return ret;
5894}
5895
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005896int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005897{
5898 struct btrfs_root *root = BTRFS_I(inode)->root;
5899 struct btrfs_trans_handle *trans;
5900 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005901 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005902
Josef Bacik72ac3c02012-05-23 14:13:11 -04005903 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005904 return 0;
5905
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005906 if (btrfs_fs_closing(root->fs_info) &&
5907 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005908 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005909
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005910 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005911 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005912 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005913 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005914 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005915 if (IS_ERR(trans))
5916 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005917 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005918 }
5919 return ret;
5920}
5921
5922/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005923 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005924 * inode changes. But, it is most likely to find the inode in cache.
5925 * FIXME, needs more benchmarking...there are no reasons other than performance
5926 * to keep or drop this code.
5927 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005928static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005929{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005931 struct btrfs_root *root = BTRFS_I(inode)->root;
5932 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005933 int ret;
5934
Josef Bacik72ac3c02012-05-23 14:13:11 -04005935 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005936 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005937
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005938 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005939 if (IS_ERR(trans))
5940 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005941
5942 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005943 if (ret && ret == -ENOSPC) {
5944 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005945 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005946 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005947 if (IS_ERR(trans))
5948 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005949
Chris Mason94b60442010-05-26 11:02:00 -04005950 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005951 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005952 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005953 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005954 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005955
5956 return ret;
5957}
5958
5959/*
5960 * This is a copy of file_update_time. We need this so we can return error on
5961 * ENOSPC for updating the inode in the case of file write and mmap writes.
5962 */
Josef Bacike41f9412012-03-26 09:46:47 -04005963static int btrfs_update_time(struct inode *inode, struct timespec *now,
5964 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005965{
Alexander Block2bc5565282012-06-15 09:49:33 +02005966 struct btrfs_root *root = BTRFS_I(inode)->root;
5967
5968 if (btrfs_root_readonly(root))
5969 return -EROFS;
5970
Josef Bacike41f9412012-03-26 09:46:47 -04005971 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005972 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005973 if (flags & S_CTIME)
5974 inode->i_ctime = *now;
5975 if (flags & S_MTIME)
5976 inode->i_mtime = *now;
5977 if (flags & S_ATIME)
5978 inode->i_atime = *now;
5979 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005980}
5981
Chris Masond352ac62008-09-29 15:18:18 -04005982/*
5983 * find the highest existing sequence number in a directory
5984 * and then set the in-memory index_cnt variable to reflect
5985 * free sequence numbers
5986 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005987static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005988{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005989 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005990 struct btrfs_key key, found_key;
5991 struct btrfs_path *path;
5992 struct extent_buffer *leaf;
5993 int ret;
5994
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005995 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005996 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005997 key.offset = (u64)-1;
5998
5999 path = btrfs_alloc_path();
6000 if (!path)
6001 return -ENOMEM;
6002
6003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6004 if (ret < 0)
6005 goto out;
6006 /* FIXME: we should be able to handle this */
6007 if (ret == 0)
6008 goto out;
6009 ret = 0;
6010
6011 /*
6012 * MAGIC NUMBER EXPLANATION:
6013 * since we search a directory based on f_pos we have to start at 2
6014 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6015 * else has to start at 2
6016 */
6017 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006018 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006019 goto out;
6020 }
6021
6022 path->slots[0]--;
6023
6024 leaf = path->nodes[0];
6025 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6026
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006027 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006028 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006029 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006030 goto out;
6031 }
6032
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006033 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006034out:
6035 btrfs_free_path(path);
6036 return ret;
6037}
6038
Chris Masond352ac62008-09-29 15:18:18 -04006039/*
6040 * helper to find a free sequence number in a given directory. This current
6041 * code is very simple, later versions will do smarter things in the btree
6042 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006043int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006044{
6045 int ret = 0;
6046
Nikolay Borisov877574e2017-02-20 13:50:33 +02006047 if (dir->index_cnt == (u64)-1) {
6048 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006049 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006050 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006051 if (ret)
6052 return ret;
6053 }
Josef Bacikaec74772008-07-24 12:12:38 -04006054 }
6055
Nikolay Borisov877574e2017-02-20 13:50:33 +02006056 *index = dir->index_cnt;
6057 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006058
6059 return ret;
6060}
6061
Chris Masonb0d5d102014-09-08 13:08:51 -07006062static int btrfs_insert_inode_locked(struct inode *inode)
6063{
6064 struct btrfs_iget_args args;
6065 args.location = &BTRFS_I(inode)->location;
6066 args.root = BTRFS_I(inode)->root;
6067
6068 return insert_inode_locked4(inode,
6069 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6070 btrfs_find_actor, &args);
6071}
6072
Chris Mason39279cc2007-06-12 06:35:45 -04006073static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006075 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006076 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006077 u64 ref_objectid, u64 objectid,
6078 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006079{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006080 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006081 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006082 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006083 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006084 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006085 struct btrfs_inode_ref *ref;
6086 struct btrfs_key key[2];
6087 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006088 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006089 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006090 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006091
Chris Mason5f39d392007-10-15 16:14:19 -04006092 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006093 if (!path)
6094 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006096 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006097 if (!inode) {
6098 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006099 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006100 }
Chris Mason39279cc2007-06-12 06:35:45 -04006101
Li Zefan581bb052011-04-20 10:06:11 +08006102 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006103 * O_TMPFILE, set link count to 0, so that after this point,
6104 * we fill in an inode item with the correct link count.
6105 */
6106 if (!name)
6107 set_nlink(inode, 0);
6108
6109 /*
Li Zefan581bb052011-04-20 10:06:11 +08006110 * we have to initialize this early, so we can reclaim the inode
6111 * number if we fail afterwards in this function.
6112 */
6113 inode->i_ino = objectid;
6114
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006115 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006116 trace_btrfs_inode_request(dir);
6117
Nikolay Borisov877574e2017-02-20 13:50:33 +02006118 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006119 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006120 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006121 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006122 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006123 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006124 } else if (dir) {
6125 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006126 }
6127 /*
6128 * index_cnt is ignored for everything but a dir,
6129 * btrfs_get_inode_index_count has an explanation for the magic
6130 * number
6131 */
6132 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006133 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006134 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006135 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006136 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006137
Josef Bacik5dc562c2012-08-17 13:14:17 -04006138 /*
6139 * We could have gotten an inode number from somebody who was fsynced
6140 * and then removed in this same transaction, so let's just set full
6141 * sync since it will be a full sync anyway and this will blow away the
6142 * old info in the log.
6143 */
6144 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6145
Chris Mason9c583092008-01-29 15:15:18 -05006146 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006147 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006148 key[0].offset = 0;
6149
Chris Mason9c583092008-01-29 15:15:18 -05006150 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006151
6152 if (name) {
6153 /*
6154 * Start new inodes with an inode_ref. This is slightly more
6155 * efficient for small numbers of hard links since they will
6156 * be packed into one item. Extended refs will kick in if we
6157 * add more hard links than can fit in the ref item.
6158 */
6159 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006160 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006161 key[1].offset = ref_objectid;
6162
6163 sizes[1] = name_len + sizeof(*ref);
6164 }
Chris Mason9c583092008-01-29 15:15:18 -05006165
Chris Masonb0d5d102014-09-08 13:08:51 -07006166 location = &BTRFS_I(inode)->location;
6167 location->objectid = objectid;
6168 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006169 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006170
6171 ret = btrfs_insert_inode_locked(inode);
6172 if (ret < 0)
6173 goto fail;
6174
Chris Masonb9473432009-03-13 11:00:37 -04006175 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006176 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006177 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006178 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006179
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006180 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006181 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306182
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006183 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306184 inode->i_atime = inode->i_mtime;
6185 inode->i_ctime = inode->i_mtime;
6186 BTRFS_I(inode)->i_otime = inode->i_mtime;
6187
Chris Mason5f39d392007-10-15 16:14:19 -04006188 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6189 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006190 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006191 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006192 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006193
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006194 if (name) {
6195 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6196 struct btrfs_inode_ref);
6197 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6198 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6199 ptr = (unsigned long)(ref + 1);
6200 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6201 }
Chris Mason9c583092008-01-29 15:15:18 -05006202
Chris Mason5f39d392007-10-15 16:14:19 -04006203 btrfs_mark_buffer_dirty(path->nodes[0]);
6204 btrfs_free_path(path);
6205
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006206 btrfs_inherit_iflags(inode, dir);
6207
Al Viro569254b2011-07-24 17:08:40 -04006208 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006209 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006210 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006211 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006212 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6213 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006214 }
6215
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006216 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006217
6218 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006219 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006220
Alexander Block8ea05e32012-07-25 17:35:53 +02006221 btrfs_update_root_times(trans, root);
6222
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006223 ret = btrfs_inode_inherit_props(trans, inode, dir);
6224 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006225 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006226 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006227 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006228
Chris Mason39279cc2007-06-12 06:35:45 -04006229 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006230
6231fail_unlock:
6232 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006233fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006234 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006235 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006236 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006237 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006238 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006239}
6240
6241static inline u8 btrfs_inode_type(struct inode *inode)
6242{
6243 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6244}
6245
Chris Masond352ac62008-09-29 15:18:18 -04006246/*
6247 * utility function to add 'inode' into 'parent_inode' with
6248 * a give name and a given sequence number.
6249 * if 'add_backref' is true, also insert a backref from the
6250 * inode to the parent directory.
6251 */
Chris Masone02119d2008-09-05 16:13:11 -04006252int btrfs_add_link(struct btrfs_trans_handle *trans,
6253 struct inode *parent_inode, struct inode *inode,
6254 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006255{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006257 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006258 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006259 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006260 u64 ino = btrfs_ino(BTRFS_I(inode));
6261 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006262
Li Zefan33345d012011-04-20 10:31:50 +08006263 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006264 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6265 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006266 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006267 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006268 key.offset = 0;
6269 }
Chris Mason39279cc2007-06-12 06:35:45 -04006270
Li Zefan33345d012011-04-20 10:31:50 +08006271 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006272 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6273 root->root_key.objectid, parent_ino,
6274 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006275 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006276 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6277 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006278 }
6279
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 /* Nothing to clean up yet */
6281 if (ret)
6282 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006283
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +02006285 BTRFS_I(parent_inode), &key,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006286 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006287 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006288 goto fail_dir_item;
6289 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006290 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006291 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006292 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006293
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006294 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006295 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006296 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006297 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006298 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006299 ret = btrfs_update_inode(trans, root, parent_inode);
6300 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006301 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006302 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006303
6304fail_dir_item:
6305 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6306 u64 local_index;
6307 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006308 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6309 root->root_key.objectid, parent_ino,
6310 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006311
6312 } else if (add_backref) {
6313 u64 local_index;
6314 int err;
6315
6316 err = btrfs_del_inode_ref(trans, root, name, name_len,
6317 ino, parent_ino, &local_index);
6318 }
6319 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006320}
6321
6322static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006323 struct inode *dir, struct dentry *dentry,
6324 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006325{
Josef Bacika1b075d2010-11-19 20:36:11 +00006326 int err = btrfs_add_link(trans, dir, inode,
6327 dentry->d_name.name, dentry->d_name.len,
6328 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006329 if (err > 0)
6330 err = -EEXIST;
6331 return err;
6332}
6333
Josef Bacik618e21d2007-07-11 10:18:17 -04006334static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006335 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006336{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006337 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006338 struct btrfs_trans_handle *trans;
6339 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006340 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006341 int err;
6342 int drop_inode = 0;
6343 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006344 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006345
Josef Bacik9ed74f22009-09-11 16:12:44 -04006346 /*
6347 * 2 for inode item and ref
6348 * 2 for dir items
6349 * 1 for xattr if selinux is on
6350 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006351 trans = btrfs_start_transaction(root, 5);
6352 if (IS_ERR(trans))
6353 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006354
Li Zefan581bb052011-04-20 10:06:11 +08006355 err = btrfs_find_free_ino(root, &objectid);
6356 if (err)
6357 goto out_unlock;
6358
Josef Bacikaec74772008-07-24 12:12:38 -04006359 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006360 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6361 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006362 if (IS_ERR(inode)) {
6363 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006364 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006365 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006366
Casey Schauflerad19db72011-12-15 10:09:07 -05006367 /*
6368 * If the active LSM wants to access the inode during
6369 * d_instantiate it needs these. Smack checks to see
6370 * if the filesystem supports xattrs by looking at the
6371 * ops vector.
6372 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006373 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006374 init_special_inode(inode, inode->i_mode, rdev);
6375
6376 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006377 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006378 goto out_unlock_inode;
6379
6380 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6381 if (err) {
6382 goto out_unlock_inode;
6383 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006384 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006386 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006387 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006388
Josef Bacik618e21d2007-07-11 10:18:17 -04006389out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006390 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006391 btrfs_balance_delayed_items(fs_info);
6392 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006393 if (drop_inode) {
6394 inode_dec_link_count(inode);
6395 iput(inode);
6396 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006397 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006398
6399out_unlock_inode:
6400 drop_inode = 1;
6401 unlock_new_inode(inode);
6402 goto out_unlock;
6403
Josef Bacik618e21d2007-07-11 10:18:17 -04006404}
6405
Chris Mason39279cc2007-06-12 06:35:45 -04006406static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006407 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006408{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006409 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006410 struct btrfs_trans_handle *trans;
6411 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006412 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006413 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006415 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006416 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006417
Josef Bacik9ed74f22009-09-11 16:12:44 -04006418 /*
6419 * 2 for inode item and ref
6420 * 2 for dir items
6421 * 1 for xattr if selinux is on
6422 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006423 trans = btrfs_start_transaction(root, 5);
6424 if (IS_ERR(trans))
6425 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006426
Li Zefan581bb052011-04-20 10:06:11 +08006427 err = btrfs_find_free_ino(root, &objectid);
6428 if (err)
6429 goto out_unlock;
6430
Josef Bacikaec74772008-07-24 12:12:38 -04006431 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006432 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6433 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006434 if (IS_ERR(inode)) {
6435 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006436 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006437 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006438 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006439 /*
6440 * If the active LSM wants to access the inode during
6441 * d_instantiate it needs these. Smack checks to see
6442 * if the filesystem supports xattrs by looking at the
6443 * ops vector.
6444 */
6445 inode->i_fop = &btrfs_file_operations;
6446 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006447 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006448
6449 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6450 if (err)
6451 goto out_unlock_inode;
6452
6453 err = btrfs_update_inode(trans, root, inode);
6454 if (err)
6455 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006456
Josef Bacika1b075d2010-11-19 20:36:11 +00006457 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006458 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006459 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006460
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006461 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006462 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463 d_instantiate(dentry, inode);
6464
Chris Mason39279cc2007-06-12 06:35:45 -04006465out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006466 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006467 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006468 inode_dec_link_count(inode);
6469 iput(inode);
6470 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006471 btrfs_balance_delayed_items(fs_info);
6472 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006474
6475out_unlock_inode:
6476 unlock_new_inode(inode);
6477 goto out_unlock;
6478
Chris Mason39279cc2007-06-12 06:35:45 -04006479}
6480
6481static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6482 struct dentry *dentry)
6483{
Filipe Manana271dba452016-01-05 16:24:05 +00006484 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006485 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006486 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006488 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006489 int err;
6490 int drop_inode = 0;
6491
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006492 /* do not allow sys_link's with other subvols of the same device */
6493 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006494 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006495
Mark Fashehf1863732012-08-08 11:32:27 -07006496 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006497 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006498
Nikolay Borisov877574e2017-02-20 13:50:33 +02006499 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006500 if (err)
6501 goto fail;
6502
Yan, Zhenga22285a2010-05-16 10:48:46 -04006503 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006504 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006506 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006507 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006508 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006509 if (IS_ERR(trans)) {
6510 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006511 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006512 goto fail;
6513 }
Chris Mason5f39d392007-10-15 16:14:19 -04006514
Miao Xie67de1172013-12-26 13:07:06 +08006515 /* There are several dir indexes for this inode, clear the cache. */
6516 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006517 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006518 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006519 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006520 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006521 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006522
Josef Bacika1b075d2010-11-19 20:36:11 +00006523 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006524
Yan, Zhenga5719522009-09-24 09:17:31 -04006525 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006526 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006527 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006528 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006529 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006530 if (err)
6531 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006532 if (inode->i_nlink == 1) {
6533 /*
6534 * If new hard link count is 1, it's a file created
6535 * with open(2) O_TMPFILE flag.
6536 */
6537 err = btrfs_orphan_del(trans, inode);
6538 if (err)
6539 goto fail;
6540 }
Al Viro08c422c2011-12-23 07:58:13 -05006541 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006542 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006543 }
Chris Mason39279cc2007-06-12 06:35:45 -04006544
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006545 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006546fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006547 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006548 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 if (drop_inode) {
6550 inode_dec_link_count(inode);
6551 iput(inode);
6552 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006553 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006554 return err;
6555}
6556
Al Viro18bb1db2011-07-26 01:41:39 -04006557static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006558{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006559 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006560 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006561 struct btrfs_trans_handle *trans;
6562 struct btrfs_root *root = BTRFS_I(dir)->root;
6563 int err = 0;
6564 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006565 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006566 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006567
Josef Bacik9ed74f22009-09-11 16:12:44 -04006568 /*
6569 * 2 items for inode and ref
6570 * 2 items for dir items
6571 * 1 for xattr if selinux is on
6572 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 trans = btrfs_start_transaction(root, 5);
6574 if (IS_ERR(trans))
6575 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006576
Li Zefan581bb052011-04-20 10:06:11 +08006577 err = btrfs_find_free_ino(root, &objectid);
6578 if (err)
6579 goto out_fail;
6580
Josef Bacikaec74772008-07-24 12:12:38 -04006581 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006582 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6583 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006584 if (IS_ERR(inode)) {
6585 err = PTR_ERR(inode);
6586 goto out_fail;
6587 }
Chris Mason5f39d392007-10-15 16:14:19 -04006588
Chris Mason39279cc2007-06-12 06:35:45 -04006589 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006590 /* these must be set before we unlock the inode */
6591 inode->i_op = &btrfs_dir_inode_operations;
6592 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006593
Eric Paris2a7dba32011-02-01 11:05:39 -05006594 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006595 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006596 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006597
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006598 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 err = btrfs_update_inode(trans, root, inode);
6600 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006602
Josef Bacika1b075d2010-11-19 20:36:11 +00006603 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6604 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006606 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006607
Chris Mason39279cc2007-06-12 06:35:45 -04006608 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006609 /*
6610 * mkdir is special. We're unlocking after we call d_instantiate
6611 * to avoid a race with nfsd calling d_instantiate.
6612 */
6613 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006614 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006615
6616out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006618 if (drop_on_err) {
6619 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006621 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 btrfs_balance_delayed_items(fs_info);
6623 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006625
6626out_fail_inode:
6627 unlock_new_inode(inode);
6628 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006629}
6630
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006631/* Find next extent map of a given extent map, caller needs to ensure locks */
6632static struct extent_map *next_extent_map(struct extent_map *em)
6633{
6634 struct rb_node *next;
6635
6636 next = rb_next(&em->rb_node);
6637 if (!next)
6638 return NULL;
6639 return container_of(next, struct extent_map, rb_node);
6640}
6641
6642static struct extent_map *prev_extent_map(struct extent_map *em)
6643{
6644 struct rb_node *prev;
6645
6646 prev = rb_prev(&em->rb_node);
6647 if (!prev)
6648 return NULL;
6649 return container_of(prev, struct extent_map, rb_node);
6650}
6651
Chris Masond352ac62008-09-29 15:18:18 -04006652/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006653 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006654 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006655 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006656 */
Chris Mason3b951512008-04-17 11:29:12 -04006657static int merge_extent_mapping(struct extent_map_tree *em_tree,
6658 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006659 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006660 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006661{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006662 struct extent_map *prev;
6663 struct extent_map *next;
6664 u64 start;
6665 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006666 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006667
Chris Masone6dcd2d2008-07-17 12:53:50 -04006668 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006669
6670 if (existing->start > map_start) {
6671 next = existing;
6672 prev = prev_extent_map(next);
6673 } else {
6674 prev = existing;
6675 next = next_extent_map(prev);
6676 }
6677
6678 start = prev ? extent_map_end(prev) : em->start;
6679 start = max_t(u64, start, em->start);
6680 end = next ? next->start : extent_map_end(em);
6681 end = min_t(u64, end, extent_map_end(em));
6682 start_diff = start - em->start;
6683 em->start = start;
6684 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006685 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6686 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006687 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006688 em->block_len -= start_diff;
6689 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006690 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006691}
6692
Chris Masonc8b97812008-10-29 14:49:59 -04006693static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006694 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006695 size_t pg_offset, u64 extent_offset,
6696 struct btrfs_file_extent_item *item)
6697{
6698 int ret;
6699 struct extent_buffer *leaf = path->nodes[0];
6700 char *tmp;
6701 size_t max_size;
6702 unsigned long inline_size;
6703 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006704 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006705
6706 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006707 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006708 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6709 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006710 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006711 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006712 if (!tmp)
6713 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006714 ptr = btrfs_file_extent_inline_start(item);
6715
6716 read_extent_buffer(leaf, tmp, ptr, inline_size);
6717
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006718 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006719 ret = btrfs_decompress(compress_type, tmp, page,
6720 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006721 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006722 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006723}
6724
Chris Masond352ac62008-09-29 15:18:18 -04006725/*
6726 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006727 * the ugly parts come from merging extents from the disk with the in-ram
6728 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006729 * where the in-ram extents might be locked pending data=ordered completion.
6730 *
6731 * This also copies inline extents directly into the page.
6732 */
Chris Masond3977122009-01-05 21:25:51 -05006733
Chris Masona52d9a82007-08-27 16:49:44 -04006734struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006735 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006736 int create)
6737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006739 int ret;
6740 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 u64 extent_start = 0;
6742 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006743 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006744 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006745 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct btrfs_root *root = BTRFS_I(inode)->root;
6747 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006748 struct extent_buffer *leaf;
6749 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006750 struct extent_map *em = NULL;
6751 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006752 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006753 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006754 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006755
Chris Masona52d9a82007-08-27 16:49:44 -04006756again:
Chris Mason890871b2009-09-02 16:24:52 -04006757 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006758 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006759 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006760 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006761 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006762
Chris Masona52d9a82007-08-27 16:49:44 -04006763 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006764 if (em->start > start || em->start + em->len <= start)
6765 free_extent_map(em);
6766 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006767 free_extent_map(em);
6768 else
6769 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006770 }
David Sterba172ddd62011-04-21 00:48:27 +02006771 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006772 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006773 err = -ENOMEM;
6774 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006775 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006776 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006777 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006778 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006779 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006780 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006781
6782 if (!path) {
6783 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006784 if (!path) {
6785 err = -ENOMEM;
6786 goto out;
6787 }
6788 /*
6789 * Chances are we'll be called again, so go ahead and do
6790 * readahead
6791 */
David Sterbae4058b52015-11-27 16:31:35 +01006792 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006793 }
6794
Chris Mason179e29e2007-11-01 11:28:41 -04006795 ret = btrfs_lookup_file_extent(trans, root, path,
6796 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006797 if (ret < 0) {
6798 err = ret;
6799 goto out;
6800 }
6801
6802 if (ret != 0) {
6803 if (path->slots[0] == 0)
6804 goto not_found;
6805 path->slots[0]--;
6806 }
6807
Chris Mason5f39d392007-10-15 16:14:19 -04006808 leaf = path->nodes[0];
6809 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006810 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006811 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006812 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006813 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006814 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006815 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006816 /*
6817 * If we backup past the first extent we want to move forward
6818 * and see if there is an extent in front of us, otherwise we'll
6819 * say there is a hole for our whole search range which can
6820 * cause problems.
6821 */
6822 extent_end = start;
6823 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006824 }
6825
Chris Mason5f39d392007-10-15 16:14:19 -04006826 found_type = btrfs_file_extent_type(leaf, item);
6827 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006828 if (found_type == BTRFS_FILE_EXTENT_REG ||
6829 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006830 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006831 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006832 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6833 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006834 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006835 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006836 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006837 }
Josef Bacik25a50342013-10-14 12:08:38 -04006838next:
Yan Zheng9036c102008-10-30 14:19:41 -04006839 if (start >= extent_end) {
6840 path->slots[0]++;
6841 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6842 ret = btrfs_next_leaf(root, path);
6843 if (ret < 0) {
6844 err = ret;
6845 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006846 }
Yan Zheng9036c102008-10-30 14:19:41 -04006847 if (ret > 0)
6848 goto not_found;
6849 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006850 }
Yan Zheng9036c102008-10-30 14:19:41 -04006851 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6852 if (found_key.objectid != objectid ||
6853 found_key.type != BTRFS_EXTENT_DATA_KEY)
6854 goto not_found;
6855 if (start + len <= found_key.offset)
6856 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006857 if (start > found_key.offset)
6858 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006859 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006860 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006861 em->len = found_key.offset - start;
6862 goto not_found_em;
6863 }
6864
Filipe Manana7ffbb592014-06-09 03:48:05 +01006865 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6866
Yan Zhengd899e052008-10-30 14:25:28 -04006867 if (found_type == BTRFS_FILE_EXTENT_REG ||
6868 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006869 goto insert;
6870 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006871 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006872 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006873 size_t size;
6874 size_t extent_offset;
6875 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006876
Filipe Manana7ffbb592014-06-09 03:48:05 +01006877 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006878 goto out;
Yan689f9342007-10-29 11:41:07 -04006879
Chris Mason514ac8a2014-01-03 21:07:00 -08006880 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006881 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006882 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6883 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006884 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006885 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006886 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006887 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006888 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006889 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006890 if (btrfs_file_extent_compression(leaf, item) !=
6891 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006892 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006893 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006894 if (ret) {
6895 err = ret;
6896 goto out;
6897 }
Chris Masonc8b97812008-10-29 14:49:59 -04006898 } else {
6899 map = kmap(page);
6900 read_extent_buffer(leaf, map + pg_offset, ptr,
6901 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006902 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006903 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006904 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006905 copy_size);
6906 }
Chris Masonc8b97812008-10-29 14:49:59 -04006907 kunmap(page);
6908 }
Chris Mason179e29e2007-11-01 11:28:41 -04006909 flush_dcache_page(page);
6910 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006911 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006912 if (!trans) {
6913 kunmap(page);
6914 free_extent_map(em);
6915 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006916
David Sterbab3b4aa72011-04-21 01:20:15 +02006917 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006918 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006919
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006920 if (IS_ERR(trans))
6921 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006922 goto again;
6923 }
Chris Masonc8b97812008-10-29 14:49:59 -04006924 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006925 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006926 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006927 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006928 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006929 }
Chris Masond1310b22008-01-24 16:13:08 -05006930 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006931 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006932 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 }
6934not_found:
6935 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006936 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006937 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006938not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006939 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006940 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006941insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006942 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006943 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006944 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006945 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6946 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006947 err = -EIO;
6948 goto out;
6949 }
Chris Masond1310b22008-01-24 16:13:08 -05006950
6951 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006952 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006953 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006954 /* it is possible that someone inserted the extent into the tree
6955 * while we had the lock dropped. It is also possible that
6956 * an overlapping map exists in the tree
6957 */
Chris Masona52d9a82007-08-27 16:49:44 -04006958 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006959 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006960
6961 ret = 0;
6962
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006963 existing = search_extent_mapping(em_tree, start, len);
6964 /*
6965 * existing will always be non-NULL, since there must be
6966 * extent causing the -EEXIST.
6967 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006968 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006969 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006970 em->block_start == existing->block_start) {
6971 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006972 * The existing extent map already encompasses the
6973 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006974 */
6975 free_extent_map(em);
6976 em = existing;
6977 err = 0;
6978
6979 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006980 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006981 /*
6982 * The existing extent map is the one nearest to
6983 * the [start, start + len) range which overlaps
6984 */
6985 err = merge_extent_mapping(em_tree, existing,
6986 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006987 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006988 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006989 free_extent_map(em);
6990 em = NULL;
6991 }
6992 } else {
6993 free_extent_map(em);
6994 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006995 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006996 }
Chris Masona52d9a82007-08-27 16:49:44 -04006997 }
Chris Mason890871b2009-09-02 16:24:52 -04006998 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006999out:
liubo1abe9b82011-03-24 11:18:59 +00007000
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007001 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00007002
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007003 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007005 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007006 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007007 err = ret;
7008 }
Chris Masona52d9a82007-08-27 16:49:44 -04007009 if (err) {
7010 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007011 return ERR_PTR(err);
7012 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007013 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007014 return em;
7015}
7016
Chris Masonec29ed52011-02-23 16:23:20 -05007017struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7018 size_t pg_offset, u64 start, u64 len,
7019 int create)
7020{
7021 struct extent_map *em;
7022 struct extent_map *hole_em = NULL;
7023 u64 range_start = start;
7024 u64 end;
7025 u64 found;
7026 u64 found_end;
7027 int err = 0;
7028
7029 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7030 if (IS_ERR(em))
7031 return em;
7032 if (em) {
7033 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007034 * if our em maps to
7035 * - a hole or
7036 * - a pre-alloc extent,
7037 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007038 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007039 if (em->block_start != EXTENT_MAP_HOLE &&
7040 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007041 return em;
7042 else
7043 hole_em = em;
7044 }
7045
7046 /* check to see if we've wrapped (len == -1 or similar) */
7047 end = start + len;
7048 if (end < start)
7049 end = (u64)-1;
7050 else
7051 end -= 1;
7052
7053 em = NULL;
7054
7055 /* ok, we didn't find anything, lets look for delalloc */
7056 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7057 end, len, EXTENT_DELALLOC, 1);
7058 found_end = range_start + found;
7059 if (found_end < range_start)
7060 found_end = (u64)-1;
7061
7062 /*
7063 * we didn't find anything useful, return
7064 * the original results from get_extent()
7065 */
7066 if (range_start > end || found_end <= start) {
7067 em = hole_em;
7068 hole_em = NULL;
7069 goto out;
7070 }
7071
7072 /* adjust the range_start to make sure it doesn't
7073 * go backwards from the start they passed in
7074 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307075 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007076 found = found_end - range_start;
7077
7078 if (found > 0) {
7079 u64 hole_start = start;
7080 u64 hole_len = len;
7081
David Sterba172ddd62011-04-21 00:48:27 +02007082 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007083 if (!em) {
7084 err = -ENOMEM;
7085 goto out;
7086 }
7087 /*
7088 * when btrfs_get_extent can't find anything it
7089 * returns one huge hole
7090 *
7091 * make sure what it found really fits our range, and
7092 * adjust to make sure it is based on the start from
7093 * the caller
7094 */
7095 if (hole_em) {
7096 u64 calc_end = extent_map_end(hole_em);
7097
7098 if (calc_end <= start || (hole_em->start > end)) {
7099 free_extent_map(hole_em);
7100 hole_em = NULL;
7101 } else {
7102 hole_start = max(hole_em->start, start);
7103 hole_len = calc_end - hole_start;
7104 }
7105 }
7106 em->bdev = NULL;
7107 if (hole_em && range_start > hole_start) {
7108 /* our hole starts before our delalloc, so we
7109 * have to return just the parts of the hole
7110 * that go until the delalloc starts
7111 */
7112 em->len = min(hole_len,
7113 range_start - hole_start);
7114 em->start = hole_start;
7115 em->orig_start = hole_start;
7116 /*
7117 * don't adjust block start at all,
7118 * it is fixed at EXTENT_MAP_HOLE
7119 */
7120 em->block_start = hole_em->block_start;
7121 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007122 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7123 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007124 } else {
7125 em->start = range_start;
7126 em->len = found;
7127 em->orig_start = range_start;
7128 em->block_start = EXTENT_MAP_DELALLOC;
7129 em->block_len = found;
7130 }
7131 } else if (hole_em) {
7132 return hole_em;
7133 }
7134out:
7135
7136 free_extent_map(hole_em);
7137 if (err) {
7138 free_extent_map(em);
7139 return ERR_PTR(err);
7140 }
7141 return em;
7142}
7143
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007144static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7145 const u64 start,
7146 const u64 len,
7147 const u64 orig_start,
7148 const u64 block_start,
7149 const u64 block_len,
7150 const u64 orig_block_len,
7151 const u64 ram_bytes,
7152 const int type)
7153{
7154 struct extent_map *em = NULL;
7155 int ret;
7156
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007157 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007158 em = create_io_em(inode, start, len, orig_start,
7159 block_start, block_len, orig_block_len,
7160 ram_bytes,
7161 BTRFS_COMPRESS_NONE, /* compress_type */
7162 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007163 if (IS_ERR(em))
7164 goto out;
7165 }
7166 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7167 len, block_len, type);
7168 if (ret) {
7169 if (em) {
7170 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007171 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007172 start + len - 1, 0);
7173 }
7174 em = ERR_PTR(ret);
7175 }
7176 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007177
7178 return em;
7179}
7180
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7182 u64 start, u64 len)
7183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007186 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 struct btrfs_key ins;
7188 u64 alloc_hint;
7189 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007192 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007193 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007194 if (ret)
7195 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007196
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007197 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7198 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007199 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007200 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007201 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007202 btrfs_free_reserved_extent(fs_info, ins.objectid,
7203 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007204
Josef Bacik4b46fce2010-05-23 11:00:55 -04007205 return em;
7206}
7207
Chris Mason46bfbb52010-05-26 11:04:10 -04007208/*
7209 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7210 * block must be cow'd
7211 */
Josef Bacik00361582013-08-14 14:02:47 -04007212noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007213 u64 *orig_start, u64 *orig_block_len,
7214 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007215{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007217 struct btrfs_path *path;
7218 int ret;
7219 struct extent_buffer *leaf;
7220 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007221 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 struct btrfs_file_extent_item *fi;
7223 struct btrfs_key key;
7224 u64 disk_bytenr;
7225 u64 backref_offset;
7226 u64 extent_end;
7227 u64 num_bytes;
7228 int slot;
7229 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007230 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007231
Chris Mason46bfbb52010-05-26 11:04:10 -04007232 path = btrfs_alloc_path();
7233 if (!path)
7234 return -ENOMEM;
7235
David Sterbaf85b7372017-01-20 14:54:07 +01007236 ret = btrfs_lookup_file_extent(NULL, root, path,
7237 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007238 if (ret < 0)
7239 goto out;
7240
7241 slot = path->slots[0];
7242 if (ret == 1) {
7243 if (slot == 0) {
7244 /* can't find the item, must cow */
7245 ret = 0;
7246 goto out;
7247 }
7248 slot--;
7249 }
7250 ret = 0;
7251 leaf = path->nodes[0];
7252 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007253 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007254 key.type != BTRFS_EXTENT_DATA_KEY) {
7255 /* not our file or wrong item type, must cow */
7256 goto out;
7257 }
7258
7259 if (key.offset > offset) {
7260 /* Wrong offset, must cow */
7261 goto out;
7262 }
7263
7264 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7265 found_type = btrfs_file_extent_type(leaf, fi);
7266 if (found_type != BTRFS_FILE_EXTENT_REG &&
7267 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7268 /* not a regular extent, must cow */
7269 goto out;
7270 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007271
7272 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7273 goto out;
7274
Miao Xiee77751a2013-12-27 21:11:50 +08007275 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7276 if (extent_end <= offset)
7277 goto out;
7278
Chris Mason46bfbb52010-05-26 11:04:10 -04007279 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007280 if (disk_bytenr == 0)
7281 goto out;
7282
7283 if (btrfs_file_extent_compression(leaf, fi) ||
7284 btrfs_file_extent_encryption(leaf, fi) ||
7285 btrfs_file_extent_other_encoding(leaf, fi))
7286 goto out;
7287
Chris Mason46bfbb52010-05-26 11:04:10 -04007288 backref_offset = btrfs_file_extent_offset(leaf, fi);
7289
Josef Bacik7ee9e442013-06-21 16:37:03 -04007290 if (orig_start) {
7291 *orig_start = key.offset - backref_offset;
7292 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7293 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7294 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007295
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007296 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007297 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007298
7299 num_bytes = min(offset + *len, extent_end) - offset;
7300 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7301 u64 range_end;
7302
Jeff Mahoneyda170662016-06-15 09:22:56 -04007303 range_end = round_up(offset + num_bytes,
7304 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007305 ret = test_range_bit(io_tree, offset, range_end,
7306 EXTENT_DELALLOC, 0, NULL);
7307 if (ret) {
7308 ret = -EAGAIN;
7309 goto out;
7310 }
7311 }
7312
Josef Bacik1bda19e2013-10-18 12:10:36 -04007313 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007314
7315 /*
7316 * look for other files referencing this extent, if we
7317 * find any we must cow
7318 */
Josef Bacik00361582013-08-14 14:02:47 -04007319
Liu Boe4c3b2d2017-01-30 12:25:28 -08007320 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007321 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007322 if (ret) {
7323 ret = 0;
7324 goto out;
7325 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007326
7327 /*
7328 * adjust disk_bytenr and num_bytes to cover just the bytes
7329 * in this extent we are about to write. If there
7330 * are any csums in that range we have to cow in order
7331 * to keep the csums correct
7332 */
7333 disk_bytenr += backref_offset;
7334 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007335 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7336 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007337 /*
7338 * all of the above have passed, it is safe to overwrite this extent
7339 * without cow
7340 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007341 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007342 ret = 1;
7343out:
7344 btrfs_free_path(path);
7345 return ret;
7346}
7347
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007348bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7349{
7350 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7351 int found = false;
7352 void **pagep = NULL;
7353 struct page *page = NULL;
7354 int start_idx;
7355 int end_idx;
7356
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007357 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007358
7359 /*
7360 * end is the last byte in the last page. end == start is legal
7361 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007362 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007363
7364 rcu_read_lock();
7365
7366 /* Most of the code in this while loop is lifted from
7367 * find_get_page. It's been modified to begin searching from a
7368 * page and return just the first page found in that range. If the
7369 * found idx is less than or equal to the end idx then we know that
7370 * a page exists. If no pages are found or if those pages are
7371 * outside of the range then we're fine (yay!) */
7372 while (page == NULL &&
7373 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7374 page = radix_tree_deref_slot(pagep);
7375 if (unlikely(!page))
7376 break;
7377
7378 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007379 if (radix_tree_deref_retry(page)) {
7380 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007381 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007382 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007383 /*
7384 * Otherwise, shmem/tmpfs must be storing a swap entry
7385 * here as an exceptional entry: so return it without
7386 * attempting to raise page count.
7387 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007388 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007389 break; /* TODO: Is this relevant for this use case? */
7390 }
7391
Filipe Manana91405152014-06-05 13:22:24 +01007392 if (!page_cache_get_speculative(page)) {
7393 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007394 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007395 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007396
7397 /*
7398 * Has the page moved?
7399 * This is part of the lockless pagecache protocol. See
7400 * include/linux/pagemap.h for details.
7401 */
7402 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007403 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007404 page = NULL;
7405 }
7406 }
7407
7408 if (page) {
7409 if (page->index <= end_idx)
7410 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007411 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007412 }
7413
7414 rcu_read_unlock();
7415 return found;
7416}
7417
Josef Bacikeb838e72012-07-31 16:28:48 -04007418static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7419 struct extent_state **cached_state, int writing)
7420{
7421 struct btrfs_ordered_extent *ordered;
7422 int ret = 0;
7423
7424 while (1) {
7425 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007426 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 /*
7428 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007429 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007430 * extents in this range.
7431 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007432 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007433 lockend - lockstart + 1);
7434
7435 /*
7436 * We need to make sure there are no buffered pages in this
7437 * range either, we could have raced between the invalidate in
7438 * generic_file_direct_write and locking the extent. The
7439 * invalidate needs to happen so that reads after a write do not
7440 * get stale data.
7441 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007442 if (!ordered &&
7443 (!writing ||
7444 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007445 break;
7446
7447 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7448 cached_state, GFP_NOFS);
7449
7450 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007451 /*
7452 * If we are doing a DIO read and the ordered extent we
7453 * found is for a buffered write, we can not wait for it
7454 * to complete and retry, because if we do so we can
7455 * deadlock with concurrent buffered writes on page
7456 * locks. This happens only if our DIO read covers more
7457 * than one extent map, if at this point has already
7458 * created an ordered extent for a previous extent map
7459 * and locked its range in the inode's io tree, and a
7460 * concurrent write against that previous extent map's
7461 * range and this range started (we unlock the ranges
7462 * in the io tree only when the bios complete and
7463 * buffered writes always lock pages before attempting
7464 * to lock range in the io tree).
7465 */
7466 if (writing ||
7467 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7468 btrfs_start_ordered_extent(inode, ordered, 1);
7469 else
7470 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007471 btrfs_put_ordered_extent(ordered);
7472 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007473 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007474 * We could trigger writeback for this range (and wait
7475 * for it to complete) and then invalidate the pages for
7476 * this range (through invalidate_inode_pages2_range()),
7477 * but that can lead us to a deadlock with a concurrent
7478 * call to readpages() (a buffered read or a defrag call
7479 * triggered a readahead) on a page lock due to an
7480 * ordered dio extent we created before but did not have
7481 * yet a corresponding bio submitted (whence it can not
7482 * complete), which makes readpages() wait for that
7483 * ordered extent to complete while holding a lock on
7484 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007485 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007486 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 }
7488
Filipe Mananaade77022016-02-18 14:28:55 +00007489 if (ret)
7490 break;
7491
Josef Bacikeb838e72012-07-31 16:28:48 -04007492 cond_resched();
7493 }
7494
7495 return ret;
7496}
7497
Liu Bo6f9994d2017-01-31 07:50:22 -08007498/* The callers of this must take lock_extent() */
7499static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7500 u64 orig_start, u64 block_start,
7501 u64 block_len, u64 orig_block_len,
7502 u64 ram_bytes, int compress_type,
7503 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007504{
7505 struct extent_map_tree *em_tree;
7506 struct extent_map *em;
7507 struct btrfs_root *root = BTRFS_I(inode)->root;
7508 int ret;
7509
Liu Bo6f9994d2017-01-31 07:50:22 -08007510 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7511 type == BTRFS_ORDERED_COMPRESSED ||
7512 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007513 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007514
Josef Bacik69ffb542012-09-11 15:40:07 -04007515 em_tree = &BTRFS_I(inode)->extent_tree;
7516 em = alloc_extent_map();
7517 if (!em)
7518 return ERR_PTR(-ENOMEM);
7519
7520 em->start = start;
7521 em->orig_start = orig_start;
7522 em->len = len;
7523 em->block_len = block_len;
7524 em->block_start = block_start;
7525 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007526 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007527 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007528 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007529 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007530 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007531 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007532 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007533 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7534 em->compress_type = compress_type;
7535 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007536
7537 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007538 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007539 em->start + em->len - 1, 0);
7540 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007541 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007542 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007543 /*
7544 * The caller has taken lock_extent(), who could race with us
7545 * to add em?
7546 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007547 } while (ret == -EEXIST);
7548
7549 if (ret) {
7550 free_extent_map(em);
7551 return ERR_PTR(ret);
7552 }
7553
Liu Bo6f9994d2017-01-31 07:50:22 -08007554 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007555 return em;
7556}
7557
Filipe Manana9c9464c2015-11-04 09:52:04 +00007558static void adjust_dio_outstanding_extents(struct inode *inode,
7559 struct btrfs_dio_data *dio_data,
7560 const u64 len)
7561{
David Sterba823bb202017-01-04 11:09:51 +01007562 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007563
Filipe Manana9c9464c2015-11-04 09:52:04 +00007564 /*
7565 * If we have an outstanding_extents count still set then we're
7566 * within our reservation, otherwise we need to adjust our inode
7567 * counter appropriately.
7568 */
Liu Boc2931662016-12-22 17:13:54 -08007569 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007570 dio_data->outstanding_extents -= num_extents;
7571 } else {
Liu Boc2931662016-12-22 17:13:54 -08007572 /*
7573 * If dio write length has been split due to no large enough
7574 * contiguous space, we need to compensate our inode counter
7575 * appropriately.
7576 */
7577 u64 num_needed = num_extents - dio_data->outstanding_extents;
7578
Filipe Manana9c9464c2015-11-04 09:52:04 +00007579 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007580 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007581 spin_unlock(&BTRFS_I(inode)->lock);
7582 }
7583}
7584
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7586 struct buffer_head *bh_result, int create)
7587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007588 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007589 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307591 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007592 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007593 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007594 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007595 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007596 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007597
Miao Xie172a5042013-02-21 02:48:22 -07007598 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007599 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007600 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007601 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007602
Josef Bacikc3298612012-08-03 16:49:19 -04007603 lockstart = start;
7604 lockend = start + len - 1;
7605
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007606 if (current->journal_info) {
7607 /*
7608 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007609 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007610 * confused.
7611 */
chandan50745b02015-08-28 21:10:13 +05307612 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007613 current->journal_info = NULL;
7614 }
7615
Josef Bacikeb838e72012-07-31 16:28:48 -04007616 /*
7617 * If this errors out it's because we couldn't invalidate pagecache for
7618 * this range and we need to fallback to buffered.
7619 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007620 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7621 create)) {
7622 ret = -ENOTBLK;
7623 goto err;
7624 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007625
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 if (IS_ERR(em)) {
7628 ret = PTR_ERR(em);
7629 goto unlock_err;
7630 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007631
7632 /*
7633 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7634 * io. INLINE is special, and we could probably kludge it in here, but
7635 * it's still buffered so for safety lets just fall back to the generic
7636 * buffered path.
7637 *
7638 * For COMPRESSED we _have_ to read the entire extent in so we can
7639 * decompress it, so there will be buffering required no matter what we
7640 * do, so go ahead and fallback to buffered.
7641 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007642 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007643 * to buffered IO. Don't blame me, this is the price we pay for using
7644 * the generic code.
7645 */
7646 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7647 em->block_start == EXTENT_MAP_INLINE) {
7648 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007649 ret = -ENOTBLK;
7650 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651 }
7652
7653 /* Just a good old fashioned hole, return */
7654 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7655 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7656 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 }
7659
7660 /*
7661 * We don't allocate a new extent in the following cases
7662 *
7663 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7664 * existing extent.
7665 * 2) The extent is marked as PREALLOC. We're good to go here and can
7666 * just use the extent.
7667 *
7668 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007669 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 len = min(len, em->len - (start - em->start));
7671 lockstart = start + len;
7672 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007673 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674
7675 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7676 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7677 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007678 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007679 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680
7681 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7682 type = BTRFS_ORDERED_PREALLOC;
7683 else
7684 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007685 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007687
Josef Bacik00361582013-08-14 14:02:47 -04007688 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007689 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007690 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007691 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007692
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007693 em2 = btrfs_create_dio_extent(inode, start, len,
7694 orig_start, block_start,
7695 len, orig_block_len,
7696 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007697 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007698 if (type == BTRFS_ORDERED_PREALLOC) {
7699 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007700 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007701 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007702 if (em2 && IS_ERR(em2)) {
7703 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007704 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007705 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007706 /*
7707 * For inode marked NODATACOW or extent marked PREALLOC,
7708 * use the existing or preallocated extent, so does not
7709 * need to adjust btrfs_space_info's bytes_may_use.
7710 */
7711 btrfs_free_reserved_data_space_noquota(inode,
7712 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007713 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007714 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007715 }
Josef Bacik00361582013-08-14 14:02:47 -04007716
Chris Mason46bfbb52010-05-26 11:04:10 -04007717 /*
7718 * this will cow the extent, reset the len in case we changed
7719 * it above
7720 */
7721 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007722 free_extent_map(em);
7723 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007724 if (IS_ERR(em)) {
7725 ret = PTR_ERR(em);
7726 goto unlock_err;
7727 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007728 len = min(len, em->len - (start - em->start));
7729unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7731 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007732 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007733 bh_result->b_bdev = em->bdev;
7734 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007735 if (create) {
7736 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7737 set_buffer_new(bh_result);
7738
7739 /*
7740 * Need to update the i_size under the extent lock so buffered
7741 * readers will get the updated i_size when we unlock.
7742 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007743 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007744 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007745
Filipe Manana9c9464c2015-11-04 09:52:04 +00007746 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307747 WARN_ON(dio_data->reserve < len);
7748 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007749 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307750 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007751 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007752
Josef Bacikeb838e72012-07-31 16:28:48 -04007753 /*
7754 * In the case of write we need to clear and unlock the entire range,
7755 * in the case of read we need to unlock only the end area that we
7756 * aren't using if there is any left over space.
7757 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007758 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007759 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7760 lockend, unlock_bits, 1, 0,
7761 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007762 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007763 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007764 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007765
Josef Bacik4b46fce2010-05-23 11:00:55 -04007766 free_extent_map(em);
7767
7768 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007769
7770unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7772 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007773err:
chandan50745b02015-08-28 21:10:13 +05307774 if (dio_data)
7775 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007776 /*
7777 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7778 * write less data then expected, so that we don't underflow our inode's
7779 * outstanding extents counter.
7780 */
7781 if (create && dio_data)
7782 adjust_dio_outstanding_extents(inode, dio_data, len);
7783
Josef Bacikeb838e72012-07-31 16:28:48 -04007784 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785}
7786
Miao Xie8b110e32014-09-12 18:44:03 +08007787static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007788 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007789{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007790 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007791 int ret;
7792
Mike Christie37226b22016-06-05 14:31:52 -05007793 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007794
7795 bio_get(bio);
7796
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007797 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007798 if (ret)
7799 goto err;
7800
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007801 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007802err:
7803 bio_put(bio);
7804 return ret;
7805}
7806
7807static int btrfs_check_dio_repairable(struct inode *inode,
7808 struct bio *failed_bio,
7809 struct io_failure_record *failrec,
7810 int failed_mirror)
7811{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007813 int num_copies;
7814
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007815 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007816 if (num_copies == 1) {
7817 /*
7818 * we only have a single copy of the data, so don't bother with
7819 * all the retry and error correction code that follows. no
7820 * matter what the error is, it is very likely to persist.
7821 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007822 btrfs_debug(fs_info,
7823 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7824 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007825 return 0;
7826 }
7827
7828 failrec->failed_mirror = failed_mirror;
7829 failrec->this_mirror++;
7830 if (failrec->this_mirror == failed_mirror)
7831 failrec->this_mirror++;
7832
7833 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007834 btrfs_debug(fs_info,
7835 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7836 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007837 return 0;
7838 }
7839
7840 return 1;
7841}
7842
7843static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307844 struct page *page, unsigned int pgoff,
7845 u64 start, u64 end, int failed_mirror,
7846 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007847{
7848 struct io_failure_record *failrec;
7849 struct bio *bio;
7850 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007851 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007852 int ret;
7853
Mike Christie37226b22016-06-05 14:31:52 -05007854 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007855
7856 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7857 if (ret)
7858 return ret;
7859
7860 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7861 failed_mirror);
7862 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007863 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007864 return -EIO;
7865 }
7866
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307867 if ((failed_bio->bi_vcnt > 1)
7868 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007869 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007870 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007871
7872 isector = start - btrfs_io_bio(failed_bio)->logical;
7873 isector >>= inode->i_sb->s_blocksize_bits;
7874 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307875 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007877 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007878 return -EIO;
7879 }
Mike Christie37226b22016-06-05 14:31:52 -05007880 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007881
7882 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7883 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7884 read_mode, failrec->this_mirror, failrec->in_validation);
7885
Mike Christie81a75f672016-06-05 14:31:54 -05007886 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007888 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007889 bio_put(bio);
7890 }
7891
7892 return ret;
7893}
7894
7895struct btrfs_retry_complete {
7896 struct completion done;
7897 struct inode *inode;
7898 u64 start;
7899 int uptodate;
7900};
7901
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007902static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007903{
7904 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307905 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007906 struct bio_vec *bvec;
7907 int i;
7908
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007909 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007910 goto end;
7911
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307912 ASSERT(bio->bi_vcnt == 1);
7913 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007914 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307915
Miao Xie8b110e32014-09-12 18:44:03 +08007916 done->uptodate = 1;
7917 bio_for_each_segment_all(bvec, bio, i)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007918 clean_io_failure(BTRFS_I(done->inode), done->start, bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007919end:
7920 complete(&done->done);
7921 bio_put(bio);
7922}
7923
7924static int __btrfs_correct_data_nocsum(struct inode *inode,
7925 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007928 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007929 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007930 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 unsigned int pgoff;
7932 u32 sectorsize;
7933 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007934 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007935 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007936
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007938 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939
Miao Xiec1dc0892014-09-12 18:43:56 +08007940 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007941 done.inode = inode;
7942
7943 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307944 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7945 pgoff = bvec->bv_offset;
7946
7947next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007948 done.uptodate = 0;
7949 done.start = start;
7950 init_completion(&done.done);
7951
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7953 pgoff, start, start + sectorsize - 1,
7954 io_bio->mirror_num,
7955 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007956 if (ret)
7957 return ret;
7958
7959 wait_for_completion(&done.done);
7960
7961 if (!done.uptodate) {
7962 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307963 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007964 }
7965
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307966 start += sectorsize;
7967
7968 if (nr_sectors--) {
7969 pgoff += sectorsize;
7970 goto next_block_or_try_again;
7971 }
Miao Xie8b110e32014-09-12 18:44:03 +08007972 }
7973
7974 return 0;
7975}
7976
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007977static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007978{
7979 struct btrfs_retry_complete *done = bio->bi_private;
7980 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007982 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007984 int uptodate;
7985 int ret;
7986 int i;
7987
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007988 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007989 goto end;
7990
7991 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992
7993 start = done->start;
7994
7995 ASSERT(bio->bi_vcnt == 1);
7996 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007997 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998
Miao Xie8b110e32014-09-12 18:44:03 +08007999 bio_for_each_segment_all(bvec, bio, i) {
8000 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 bvec->bv_page, bvec->bv_offset,
8002 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008003 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02008004 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008006 else
8007 uptodate = 0;
8008 }
8009
8010 done->uptodate = uptodate;
8011end:
8012 complete(&done->done);
8013 bio_put(bio);
8014}
8015
8016static int __btrfs_subio_endio_read(struct inode *inode,
8017 struct btrfs_io_bio *io_bio, int err)
8018{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008020 struct bio_vec *bvec;
8021 struct btrfs_retry_complete done;
8022 u64 start;
8023 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 u32 sectorsize;
8025 int nr_sectors;
8026 unsigned int pgoff;
8027 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008028 int i;
8029 int ret;
8030
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008032 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033
Miao Xie8b110e32014-09-12 18:44:03 +08008034 err = 0;
8035 start = io_bio->logical;
8036 done.inode = inode;
8037
Miao Xiec1dc0892014-09-12 18:43:56 +08008038 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8040
8041 pgoff = bvec->bv_offset;
8042next_block:
8043 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8044 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8045 bvec->bv_page, pgoff, start,
8046 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008047 if (likely(!ret))
8048 goto next;
8049try_again:
8050 done.uptodate = 0;
8051 done.start = start;
8052 init_completion(&done.done);
8053
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308054 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8055 pgoff, start, start + sectorsize - 1,
8056 io_bio->mirror_num,
8057 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008058 if (ret) {
8059 err = ret;
8060 goto next;
8061 }
8062
8063 wait_for_completion(&done.done);
8064
8065 if (!done.uptodate) {
8066 /* We might have another mirror, so try again */
8067 goto try_again;
8068 }
8069next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 offset += sectorsize;
8071 start += sectorsize;
8072
8073 ASSERT(nr_sectors);
8074
8075 if (--nr_sectors) {
8076 pgoff += sectorsize;
8077 goto next_block;
8078 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008079 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008080
8081 return err;
8082}
8083
Miao Xie8b110e32014-09-12 18:44:03 +08008084static int btrfs_subio_endio_read(struct inode *inode,
8085 struct btrfs_io_bio *io_bio, int err)
8086{
8087 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8088
8089 if (skip_csum) {
8090 if (unlikely(err))
8091 return __btrfs_correct_data_nocsum(inode, io_bio);
8092 else
8093 return 0;
8094 } else {
8095 return __btrfs_subio_endio_read(inode, io_bio, err);
8096 }
8097}
8098
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008099static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008100{
8101 struct btrfs_dio_private *dip = bio->bi_private;
8102 struct inode *inode = dip->inode;
8103 struct bio *dio_bio;
8104 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008105 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008106
Miao Xie8b110e32014-09-12 18:44:03 +08008107 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8108 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008109
Josef Bacik4b46fce2010-05-23 11:00:55 -04008110 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008111 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008112 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008113
Josef Bacik4b46fce2010-05-23 11:00:55 -04008114 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008115
Filipe Manana1636d1d2016-02-15 16:20:26 +00008116 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008117 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008118
8119 if (io_bio->end_io)
8120 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008121 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008122}
8123
Filipe Manana14543772015-11-24 16:23:54 +00008124static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8125 const u64 offset,
8126 const u64 bytes,
8127 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008130 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008131 u64 ordered_offset = offset;
8132 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008133 int ret;
8134
Chris Mason163cf092010-11-28 19:56:33 -05008135again:
8136 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8137 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008138 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008139 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008140 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008141 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008142
Liu Bo9e0af232014-08-15 23:36:53 +08008143 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8144 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008145 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008146out_test:
8147 /*
8148 * our bio might span multiple ordered extents. If we haven't
8149 * completed the accounting for the whole dio, go back and try again
8150 */
Filipe Manana14543772015-11-24 16:23:54 +00008151 if (ordered_offset < offset + bytes) {
8152 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008153 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008154 goto again;
8155 }
Filipe Manana14543772015-11-24 16:23:54 +00008156}
8157
8158static void btrfs_endio_direct_write(struct bio *bio)
8159{
8160 struct btrfs_dio_private *dip = bio->bi_private;
8161 struct bio *dio_bio = dip->dio_bio;
8162
8163 btrfs_endio_direct_write_update_ordered(dip->inode,
8164 dip->logical_offset,
8165 dip->bytes,
8166 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167
Josef Bacik4b46fce2010-05-23 11:00:55 -04008168 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008169
Filipe Manana1636d1d2016-02-15 16:20:26 +00008170 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008171 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008172 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008173}
8174
Mike Christie81a75f672016-06-05 14:31:54 -05008175static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008176 struct bio *bio, int mirror_num,
8177 unsigned long bio_flags, u64 offset)
8178{
8179 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008180 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008181 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008182 return 0;
8183}
8184
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008185static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008186{
8187 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008188 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008189
Miao Xie8b110e32014-09-12 18:44:03 +08008190 if (err)
8191 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008192 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008193 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8194 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008195 (unsigned long long)bio->bi_iter.bi_sector,
8196 bio->bi_iter.bi_size, err);
8197
8198 if (dip->subio_endio)
8199 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008200
8201 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008202 dip->errors = 1;
8203
8204 /*
8205 * before atomic variable goto zero, we must make sure
8206 * dip->errors is perceived to be set.
8207 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008208 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008209 }
8210
8211 /* if there are more bios still pending for this dio, just exit */
8212 if (!atomic_dec_and_test(&dip->pending_bios))
8213 goto out;
8214
Chris Mason9be33952013-05-17 18:30:14 -04008215 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008216 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008217 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008218 dip->dio_bio->bi_error = 0;
8219 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008220 }
8221out:
8222 bio_put(bio);
8223}
8224
8225static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8226 u64 first_sector, gfp_t gfp_flags)
8227{
Chris Masonda2f0f72015-07-02 13:57:22 -07008228 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008229 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008230 if (bio)
8231 bio_associate_current(bio);
8232 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008233}
8234
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008235static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008236 struct btrfs_dio_private *dip,
8237 struct bio *bio,
8238 u64 file_offset)
8239{
8240 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8241 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8242 int ret;
8243
8244 /*
8245 * We load all the csum data we need when we submit
8246 * the first bio to reduce the csum tree search and
8247 * contention.
8248 */
8249 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008250 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008251 file_offset);
8252 if (ret)
8253 return ret;
8254 }
8255
8256 if (bio == dip->orig_bio)
8257 return 0;
8258
8259 file_offset -= dip->logical_offset;
8260 file_offset >>= inode->i_sb->s_blocksize_bits;
8261 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8262
8263 return 0;
8264}
8265
Miao Xiee65e1532010-11-22 03:04:43 +00008266static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008267 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008268 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008269{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008271 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008272 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008273 int ret;
8274
Josef Bacikb812ce22012-11-16 13:56:32 -05008275 if (async_submit)
8276 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8277
Miao Xiee65e1532010-11-22 03:04:43 +00008278 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008279
8280 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008281 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008282 if (ret)
8283 goto err;
8284 }
Miao Xiee65e1532010-11-22 03:04:43 +00008285
Josef Bacik1ae39932011-04-06 14:41:34 -04008286 if (skip_sum)
8287 goto map;
8288
8289 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008290 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8291 file_offset,
8292 __btrfs_submit_bio_start_direct_io,
8293 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008294 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008295 } else if (write) {
8296 /*
8297 * If we aren't doing async submit, calculate the csum of the
8298 * bio now.
8299 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008300 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008301 if (ret)
8302 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008303 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008304 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008305 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008306 if (ret)
8307 goto err;
8308 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008309map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008310 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008311err:
8312 bio_put(bio);
8313 return ret;
8314}
8315
Mike Christie81a75f672016-06-05 14:31:54 -05008316static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008317 int skip_sum)
8318{
8319 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008321 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008322 struct bio *bio;
8323 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008324 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008325 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008326 u64 file_offset = dip->logical_offset;
8327 u64 submit_len = 0;
8328 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008329 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008330 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308331 int nr_sectors;
8332 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008333 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008334
Kent Overstreet4f024f32013-10-11 15:44:27 -07008335 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008336 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8337 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008338 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008339 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008340
Kent Overstreet4f024f32013-10-11 15:44:27 -07008341 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008342 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008343 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008344 goto submit;
8345 }
8346
David Woodhouse53b381b2013-01-29 18:40:14 -05008347 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008348 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008349 async_submit = 0;
8350 else
8351 async_submit = 1;
8352
Josef Bacik02f57c72011-04-06 14:25:44 -04008353 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8354 if (!bio)
8355 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008356
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008357 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008358 bio->bi_private = dip;
8359 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008360 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008361 atomic_inc(&dip->pending_bios);
8362
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008363 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008364 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308365 i = 0;
8366next_block:
8367 if (unlikely(map_length < submit_len + blocksize ||
8368 bio_add_page(bio, bvec->bv_page, blocksize,
8369 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008370 /*
8371 * inc the count before we submit the bio so
8372 * we know the end IO handler won't happen before
8373 * we inc the count. Otherwise, the dip might get freed
8374 * before we're done setting it up
8375 */
8376 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008377 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008378 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008379 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008380 if (ret) {
8381 bio_put(bio);
8382 atomic_dec(&dip->pending_bios);
8383 goto out_err;
8384 }
8385
Miao Xiee65e1532010-11-22 03:04:43 +00008386 start_sector += submit_len >> 9;
8387 file_offset += submit_len;
8388
8389 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008390
8391 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8392 start_sector, GFP_NOFS);
8393 if (!bio)
8394 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008395 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008396 bio->bi_private = dip;
8397 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008398 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008399
Kent Overstreet4f024f32013-10-11 15:44:27 -07008400 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008401 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008402 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008403 &map_length, NULL, 0);
8404 if (ret) {
8405 bio_put(bio);
8406 goto out_err;
8407 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308408
8409 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008410 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308411 submit_len += blocksize;
8412 if (--nr_sectors) {
8413 i++;
8414 goto next_block;
8415 }
Miao Xiee65e1532010-11-22 03:04:43 +00008416 }
8417 }
8418
Josef Bacik02f57c72011-04-06 14:25:44 -04008419submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008420 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008421 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008422 if (!ret)
8423 return 0;
8424
8425 bio_put(bio);
8426out_err:
8427 dip->errors = 1;
8428 /*
8429 * before atomic variable goto zero, we must
8430 * make sure dip->errors is perceived to be set.
8431 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008432 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008433 if (atomic_dec_and_test(&dip->pending_bios))
8434 bio_io_error(dip->orig_bio);
8435
8436 /* bio_end_io() will handle error, so we needn't return it */
8437 return 0;
8438}
8439
Mike Christie8a4c1e42016-06-05 14:31:50 -05008440static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8441 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008442{
Filipe Manana61de7182015-07-01 12:13:10 +01008443 struct btrfs_dio_private *dip = NULL;
8444 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008445 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008447 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008448 int ret = 0;
8449
8450 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8451
Chris Mason9be33952013-05-17 18:30:14 -04008452 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008453 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008454 ret = -ENOMEM;
8455 goto free_ordered;
8456 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008457
Miao Xiec1dc0892014-09-12 18:43:56 +08008458 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008459 if (!dip) {
8460 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008461 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008462 }
8463
8464 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008465 dip->inode = inode;
8466 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008467 dip->bytes = dio_bio->bi_iter.bi_size;
8468 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008469 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008470 dip->orig_bio = io_bio;
8471 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008472 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008473 btrfs_bio = btrfs_io_bio(io_bio);
8474 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008475
Miao Xiec1dc0892014-09-12 18:43:56 +08008476 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008477 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008478 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008479 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008480 dip->subio_endio = btrfs_subio_endio_read;
8481 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008482
Filipe Mananaf28a4922015-12-08 19:23:20 +00008483 /*
8484 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8485 * even if we fail to submit a bio, because in such case we do the
8486 * corresponding error handling below and it must not be done a second
8487 * time by btrfs_direct_IO().
8488 */
8489 if (write) {
8490 struct btrfs_dio_data *dio_data = current->journal_info;
8491
8492 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8493 dip->bytes;
8494 dio_data->unsubmitted_oe_range_start =
8495 dio_data->unsubmitted_oe_range_end;
8496 }
8497
Mike Christie81a75f672016-06-05 14:31:54 -05008498 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008499 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008500 return;
Chris Mason9be33952013-05-17 18:30:14 -04008501
Miao Xie23ea8e52014-09-12 18:43:54 +08008502 if (btrfs_bio->end_io)
8503 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008504
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505free_ordered:
8506 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008507 * If we arrived here it means either we failed to submit the dip
8508 * or we either failed to clone the dio_bio or failed to allocate the
8509 * dip. If we cloned the dio_bio and allocated the dip, we can just
8510 * call bio_endio against our io_bio so that we get proper resource
8511 * cleanup if we fail to submit the dip, otherwise, we must do the
8512 * same as btrfs_endio_direct_[write|read] because we can't call these
8513 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008514 */
Filipe Manana61de7182015-07-01 12:13:10 +01008515 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008516 io_bio->bi_error = -EIO;
8517 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008518 /*
8519 * The end io callbacks free our dip, do the final put on io_bio
8520 * and all the cleanup and final put for dio_bio (through
8521 * dio_end_io()).
8522 */
8523 dip = NULL;
8524 io_bio = NULL;
8525 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008526 if (write)
8527 btrfs_endio_direct_write_update_ordered(inode,
8528 file_offset,
8529 dio_bio->bi_iter.bi_size,
8530 0);
8531 else
Filipe Manana61de7182015-07-01 12:13:10 +01008532 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8533 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008534
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008535 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008536 /*
8537 * Releases and cleans up our dio_bio, no need to bio_put()
8538 * nor bio_endio()/bio_io_error() against dio_bio.
8539 */
8540 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541 }
Filipe Manana61de7182015-07-01 12:13:10 +01008542 if (io_bio)
8543 bio_put(io_bio);
8544 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008545}
8546
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008547static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8548 struct kiocb *iocb,
8549 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008550{
8551 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008552 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008553 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008554 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008555
8556 if (offset & blocksize_mask)
8557 goto out;
8558
Al Viro28060d52014-03-22 05:15:17 -04008559 if (iov_iter_alignment(iter) & blocksize_mask)
8560 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008561
Al Viro28060d52014-03-22 05:15:17 -04008562 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008563 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008564 return 0;
8565 /*
8566 * Check to make sure we don't have duplicate iov_base's in this
8567 * iovec, if so return EINVAL, otherwise we'll get csum errors
8568 * when reading back.
8569 */
8570 for (seg = 0; seg < iter->nr_segs; seg++) {
8571 for (i = seg + 1; i < iter->nr_segs; i++) {
8572 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008573 goto out;
8574 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008575 }
8576 retval = 0;
8577out:
8578 return retval;
8579}
Josef Bacikeb838e72012-07-31 16:28:48 -04008580
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008581static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008582{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008583 struct file *file = iocb->ki_filp;
8584 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008585 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308586 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008587 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008588 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008589 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008590 bool wakeup = true;
8591 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008592 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008593
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008594 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008595 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008596
Jens Axboefe0f07d2015-04-15 17:05:48 -06008597 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008598 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008599
Josef Bacik0e267c42013-07-02 10:38:02 -04008600 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008601 * The generic stuff only does filemap_write_and_wait_range, which
8602 * isn't enough if we've written compressed pages to this area, so
8603 * we need to flush the dirty pages again to make absolutely sure
8604 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008605 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008606 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008607 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8608 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008609 filemap_fdatawrite_range(inode->i_mapping, offset,
8610 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008611
Omar Sandoval6f673762015-03-16 04:33:52 -07008612 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008613 /*
8614 * If the write DIO is beyond the EOF, we need update
8615 * the isize, but it is protected by i_mutex. So we can
8616 * not unlock the i_mutex at this case.
8617 */
8618 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008619 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008620 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008621 relock = true;
8622 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008623 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008624 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008625 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008626 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008627
8628 /*
8629 * We need to know how many extents we reserved so that we can
8630 * do the accounting properly if we go over the number we
8631 * originally calculated. Abuse current->journal_info for this.
8632 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008633 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008634 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008635 dio_data.unsubmitted_oe_range_start = (u64)offset;
8636 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308637 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308638 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008639 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8640 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008641 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008642 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8643 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008644 }
8645
Omar Sandoval17f8c842015-03-16 04:33:50 -07008646 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008647 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008648 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008649 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008650 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308651 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008652 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008653 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308654 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008655 btrfs_delalloc_release_space(inode, offset,
8656 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008657 /*
8658 * On error we might have left some ordered extents
8659 * without submitting corresponding bios for them, so
8660 * cleanup them up to avoid other tasks getting them
8661 * and waiting for them to complete forever.
8662 */
8663 if (dio_data.unsubmitted_oe_range_start <
8664 dio_data.unsubmitted_oe_range_end)
8665 btrfs_endio_direct_write_update_ordered(inode,
8666 dio_data.unsubmitted_oe_range_start,
8667 dio_data.unsubmitted_oe_range_end -
8668 dio_data.unsubmitted_oe_range_start,
8669 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008670 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008671 btrfs_delalloc_release_space(inode, offset,
8672 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008673 }
Miao Xie38851cc2013-02-08 07:04:11 +00008674out:
Miao Xie2e60a512013-02-08 07:01:08 +00008675 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008676 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008677 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008678 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008679
8680 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008681}
8682
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008683#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8684
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008685static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8686 __u64 start, __u64 len)
8687{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008688 int ret;
8689
8690 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8691 if (ret)
8692 return ret;
8693
Chris Masonec29ed52011-02-23 16:23:20 -05008694 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008695}
8696
Chris Mason9ebefb182007-06-15 13:50:00 -04008697int btrfs_readpage(struct file *file, struct page *page)
8698{
Chris Masond1310b22008-01-24 16:13:08 -05008699 struct extent_io_tree *tree;
8700 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008701 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008702}
Chris Mason1832a6d2007-12-21 16:27:21 -05008703
Chris Mason39279cc2007-06-12 06:35:45 -04008704static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8705{
Chris Masond1310b22008-01-24 16:13:08 -05008706 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008707 struct inode *inode = page->mapping->host;
8708 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008709
8710 if (current->flags & PF_MEMALLOC) {
8711 redirty_page_for_writepage(wbc, page);
8712 unlock_page(page);
8713 return 0;
8714 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008715
8716 /*
8717 * If we are under memory pressure we will call this directly from the
8718 * VM, we need to make sure we have the inode referenced for the ordered
8719 * extent. If not just return like we didn't do anything.
8720 */
8721 if (!igrab(inode)) {
8722 redirty_page_for_writepage(wbc, page);
8723 return AOP_WRITEPAGE_ACTIVATE;
8724 }
Chris Masond1310b22008-01-24 16:13:08 -05008725 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008726 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8727 btrfs_add_delayed_iput(inode);
8728 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008729}
Chris Mason39279cc2007-06-12 06:35:45 -04008730
Eric Sandeen48a3b632013-04-25 20:41:01 +00008731static int btrfs_writepages(struct address_space *mapping,
8732 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008733{
Chris Masond1310b22008-01-24 16:13:08 -05008734 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008735
Chris Masond1310b22008-01-24 16:13:08 -05008736 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008737 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8738}
8739
Chris Mason3ab2fb52007-11-08 10:59:22 -05008740static int
8741btrfs_readpages(struct file *file, struct address_space *mapping,
8742 struct list_head *pages, unsigned nr_pages)
8743{
Chris Masond1310b22008-01-24 16:13:08 -05008744 struct extent_io_tree *tree;
8745 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008746 return extent_readpages(tree, mapping, pages, nr_pages,
8747 btrfs_get_extent);
8748}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008749static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008750{
Chris Masond1310b22008-01-24 16:13:08 -05008751 struct extent_io_tree *tree;
8752 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008753 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008754
Chris Masond1310b22008-01-24 16:13:08 -05008755 tree = &BTRFS_I(page->mapping->host)->io_tree;
8756 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008757 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008758 if (ret == 1) {
8759 ClearPagePrivate(page);
8760 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008761 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008762 }
8763 return ret;
8764}
Chris Mason39279cc2007-06-12 06:35:45 -04008765
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8767{
Chris Mason98509cf2008-09-11 15:51:43 -04008768 if (PageWriteback(page) || PageDirty(page))
8769 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008770 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008771}
8772
Lukas Czernerd47992f2013-05-21 23:17:23 -04008773static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8774 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008775{
Josef Bacik5fd02042012-05-02 14:00:54 -04008776 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008777 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008779 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008780 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008781 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308782 u64 start;
8783 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008784 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008785
Chris Mason8b62b722009-09-02 16:53:46 -04008786 /*
8787 * we have the page locked, so new writeback can't start,
8788 * and the dirty bit won't be cleared while we are here.
8789 *
8790 * Wait for IO on this page so that we can safely clear
8791 * the PagePrivate2 bit and do ordered accounting
8792 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008793 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008794
Josef Bacik5fd02042012-05-02 14:00:54 -04008795 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008796 if (offset) {
8797 btrfs_releasepage(page, GFP_NOFS);
8798 return;
8799 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008800
8801 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008802 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308803again:
8804 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008805 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308806 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008807 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308808 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008809 /*
8810 * IO on this page will never be started, so we need
8811 * to account for any ordered extents now
8812 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008813 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308814 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008815 EXTENT_DIRTY | EXTENT_DELALLOC |
8816 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8817 EXTENT_DEFRAG, 1, 0, &cached_state,
8818 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008819 /*
8820 * whoever cleared the private bit is responsible
8821 * for the finish_ordered_io
8822 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008823 if (TestClearPagePrivate2(page)) {
8824 struct btrfs_ordered_inode_tree *tree;
8825 u64 new_len;
8826
8827 tree = &BTRFS_I(inode)->ordered_tree;
8828
8829 spin_lock_irq(&tree->lock);
8830 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308831 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008832 if (new_len < ordered->truncated_len)
8833 ordered->truncated_len = new_len;
8834 spin_unlock_irq(&tree->lock);
8835
8836 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308837 start,
8838 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008839 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008841 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008842 if (!inode_evicting) {
8843 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308844 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008845 &cached_state);
8846 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308847
8848 start = end + 1;
8849 if (start < page_end)
8850 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008851 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008852
Qu Wenruob9d0b382015-09-29 10:35:16 +08008853 /*
8854 * Qgroup reserved space handler
8855 * Page here will be either
8856 * 1) Already written to disk
8857 * In this case, its reserved space is released from data rsv map
8858 * and will be freed by delayed_ref handler finally.
8859 * So even we call qgroup_free_data(), it won't decrease reserved
8860 * space.
8861 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008862 * This means the reserved space should be freed here. However,
8863 * if a truncate invalidates the page (by clearing PageDirty)
8864 * and the page is accounted for while allocating extent
8865 * in btrfs_check_data_free_space() we let delayed_ref to
8866 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008867 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008868 if (PageDirty(page))
8869 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870 if (!inode_evicting) {
8871 clear_extent_bit(tree, page_start, page_end,
8872 EXTENT_LOCKED | EXTENT_DIRTY |
8873 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8874 EXTENT_DEFRAG, 1, 1,
8875 &cached_state, GFP_NOFS);
8876
8877 __btrfs_releasepage(page, GFP_NOFS);
8878 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879
Chris Mason4a096752008-07-21 10:29:44 -04008880 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008881 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008882 ClearPagePrivate(page);
8883 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008884 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008885 }
Chris Mason39279cc2007-06-12 06:35:45 -04008886}
8887
Chris Mason9ebefb182007-06-15 13:50:00 -04008888/*
8889 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8890 * called from a page fault handler when a page is first dirtied. Hence we must
8891 * be careful to check for EOF conditions here. We set the page up correctly
8892 * for a written page which means we get ENOSPC checking when writing into
8893 * holes and correct delalloc and unwritten extent mapping on filesystems that
8894 * support these features.
8895 *
8896 * We are not allowed to take the i_mutex here so we have to play games to
8897 * protect against truncate races as the page could now be beyond EOF. Because
8898 * vmtruncate() writes the inode size before removing pages, once we have the
8899 * page lock we can determine safely if the page is beyond EOF. If it is not
8900 * beyond EOF, then the page is guaranteed safe against truncation until we
8901 * unlock the page.
8902 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008903int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008904{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008905 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008906 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008907 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8909 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008910 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 char *kaddr;
8912 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008913 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008914 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008915 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308916 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008917 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308919 u64 end;
8920
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008921 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008922
Jan Karab2b5ef52012-06-12 16:20:45 +02008923 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008924 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008925 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308926 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008927
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308928 /*
8929 * Reserving delalloc space after obtaining the page lock can lead to
8930 * deadlock. For example, if a dirty page is locked by this function
8931 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8932 * dirty page write out, then the btrfs_writepage() function could
8933 * end up waiting indefinitely to get a lock on the page currently
8934 * being processed by btrfs_page_mkwrite() function.
8935 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008936 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308937 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008938 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008939 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008940 reserved = 1;
8941 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008942 if (ret) {
8943 if (ret == -ENOMEM)
8944 ret = VM_FAULT_OOM;
8945 else /* -ENOSPC, -EIO, etc */
8946 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008947 if (reserved)
8948 goto out;
8949 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008950 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008951
Nick Piggin56a76f82009-03-31 15:23:23 -07008952 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008954 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008955 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008956
Chris Mason9ebefb182007-06-15 13:50:00 -04008957 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008959 /* page got truncated out from underneath us */
8960 goto out_unlock;
8961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962 wait_on_page_writeback(page);
8963
David Sterbaff13db42015-12-03 14:30:40 +01008964 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965 set_page_extent_mapped(page);
8966
Chris Masoneb84ae02008-07-17 13:53:27 -04008967 /*
8968 * we can't set the delalloc bits if there are pending ordered
8969 * extents. Drop our locks and wait for them to finish
8970 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008971 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8972 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008974 unlock_extent_cached(io_tree, page_start, page_end,
8975 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008977 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 btrfs_put_ordered_extent(ordered);
8979 goto again;
8980 }
8981
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008982 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008983 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008984 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 end = page_start + reserved_space - 1;
8987 spin_lock(&BTRFS_I(inode)->lock);
8988 BTRFS_I(inode)->outstanding_extents++;
8989 spin_unlock(&BTRFS_I(inode)->lock);
8990 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008991 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308992 }
8993 }
8994
Josef Bacikfbf19082009-10-01 17:10:23 -04008995 /*
Liu Bo54160342016-12-13 12:15:19 -08008996 * page_mkwrite gets called when the page is firstly dirtied after it's
8997 * faulted in, but write(2) could also dirty a page and set delalloc
8998 * bits, thus in this case for space account reason, we still need to
8999 * clear any delalloc bits within this page range since we have to
9000 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009001 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309002 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009003 EXTENT_DIRTY | EXTENT_DELALLOC |
9004 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009005 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009006
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309007 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009008 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009009 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009010 unlock_extent_cached(io_tree, page_start, page_end,
9011 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009012 ret = VM_FAULT_SIGBUS;
9013 goto out_unlock;
9014 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009016
9017 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 if (page_start + PAGE_SIZE > size)
9019 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009020 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009022
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009023 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009025 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 flush_dcache_page(page);
9027 kunmap(page);
9028 }
Chris Mason247e7432008-07-17 12:53:51 -04009029 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009030 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009031 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009032
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009033 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009034 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009035 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009036
Josef Bacik2ac55d42010-02-03 19:33:23 +00009037 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009038
9039out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009040 if (!ret) {
9041 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009042 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009043 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009044 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009045out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309046 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009047out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009048 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009049 return ret;
9050}
9051
Josef Bacika41ad392011-01-31 15:30:16 -05009052static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009055 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009056 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009057 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009058 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009059 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009060 u64 mask = fs_info->sectorsize - 1;
9061 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009062
Josef Bacik0ef8b722013-10-25 16:13:35 -04009063 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9064 (u64)-1);
9065 if (ret)
9066 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009067
Josef Bacikfcb80c22011-05-03 10:40:22 -04009068 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009069 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009070 * 3 things going on here
9071 *
9072 * 1) We need to reserve space for our orphan item and the space to
9073 * delete our orphan item. Lord knows we don't want to have a dangling
9074 * orphan item because we didn't reserve space to remove it.
9075 *
9076 * 2) We need to reserve space to update our inode.
9077 *
9078 * 3) We need to have something to cache all the space that is going to
9079 * be free'd up by the truncate operation, but also have some slack
9080 * space reserved in case it uses space during the truncate (thank you
9081 * very much snapshotting).
9082 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009083 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009084 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009085 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009086 * doesn't end up using space reserved for updating the inode or
9087 * removing the orphan item. We also need to be able to stop the
9088 * transaction and start a new one, which means we need to be able to
9089 * update the inode several times, and we have no idea of knowing how
9090 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009091 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009092 * Then there is the orphan item, which does indeed need to be held on
9093 * to for the whole operation, and we need nobody to touch this reserved
9094 * space except the orphan code.
9095 *
9096 * So that leaves us with
9097 *
9098 * 1) root->orphan_block_rsv - for the orphan deletion.
9099 * 2) rsv - for the truncate reservation, which we will steal from the
9100 * transaction reservation.
9101 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9102 * updating the inode.
9103 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009104 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009105 if (!rsv)
9106 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009107 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009108 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009109
Josef Bacik907cbce2011-08-08 13:46:15 -04009110 /*
Josef Bacik07127182011-08-19 10:29:59 -04009111 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009112 * 1 for updating the inode.
9113 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009114 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009115 if (IS_ERR(trans)) {
9116 err = PTR_ERR(trans);
9117 goto out;
9118 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009119
Josef Bacik907cbce2011-08-08 13:46:15 -04009120 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009121 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009122 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009124
Chris Mason5a3f23d2009-03-31 13:27:11 -04009125 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009126 * So if we truncate and then write and fsync we normally would just
9127 * write the extents that changed, which is a problem if we need to
9128 * first truncate that entire inode. So set this flag so we write out
9129 * all of the extents in the inode to the sync log so we're completely
9130 * safe.
9131 */
9132 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009133 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009134
Yan, Zheng80825102009-11-12 09:35:36 +00009135 while (1) {
9136 ret = btrfs_truncate_inode_items(trans, root, inode,
9137 inode->i_size,
9138 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009139 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009140 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009141 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009142 }
Chris Mason39279cc2007-06-12 06:35:45 -04009143
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009144 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009145 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009146 if (ret) {
9147 err = ret;
9148 break;
9149 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009150
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009151 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009152 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009153
9154 trans = btrfs_start_transaction(root, 2);
9155 if (IS_ERR(trans)) {
9156 ret = err = PTR_ERR(trans);
9157 trans = NULL;
9158 break;
9159 }
9160
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009161 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009162 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009163 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009164 BUG_ON(ret); /* shouldn't happen */
9165 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009166 }
9167
9168 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009169 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009170 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009171 if (ret)
9172 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009173 }
9174
Chris Mason917c16b2011-11-08 14:49:59 -05009175 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009176 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009177 ret = btrfs_update_inode(trans, root, inode);
9178 if (ret && !err)
9179 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009180
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009181 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009182 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009183 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009184out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009185 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009186
Josef Bacik3893e332011-01-31 16:03:11 -05009187 if (ret && !err)
9188 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009189
Josef Bacik3893e332011-01-31 16:03:11 -05009190 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009191}
9192
Sven Wegener3b963622008-06-09 21:57:42 -04009193/*
Chris Masond352ac62008-09-29 15:18:18 -04009194 * create a new subvolume directory/inode (helper for the ioctl).
9195 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009196int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009197 struct btrfs_root *new_root,
9198 struct btrfs_root *parent_root,
9199 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009200{
Chris Mason39279cc2007-06-12 06:35:45 -04009201 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009202 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009203 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009204
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009205 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9206 new_dirid, new_dirid,
9207 S_IFDIR | (~current_umask() & S_IRWXUGO),
9208 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009209 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009210 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009211 inode->i_op = &btrfs_dir_inode_operations;
9212 inode->i_fop = &btrfs_dir_file_operations;
9213
Miklos Szeredibfe86842011-10-28 14:13:29 +02009214 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009215 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009216 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009217
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009218 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9219 if (err)
9220 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009221 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009222 new_root->root_key.objectid, err);
9223
Yan, Zheng76dda932009-09-21 16:00:26 -04009224 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009225
Yan, Zheng76dda932009-09-21 16:00:26 -04009226 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009227 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009228}
9229
Chris Mason39279cc2007-06-12 06:35:45 -04009230struct inode *btrfs_alloc_inode(struct super_block *sb)
9231{
9232 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009233 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009234
9235 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9236 if (!ei)
9237 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238
9239 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009241 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009242 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009243 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009245 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246 ei->disk_i_size = 0;
9247 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009248 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009250 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009251 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009252 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009253 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009254
Josef Bacik9e0baf62011-07-15 15:16:44 +00009255 spin_lock_init(&ei->lock);
9256 ei->outstanding_extents = 0;
9257 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009258
Josef Bacik72ac3c02012-05-23 14:13:11 -04009259 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009260 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009261
Miao Xie16cdcec2011-04-22 18:12:22 +08009262 ei->delayed_node = NULL;
9263
chandan r9cc97d62012-07-04 12:48:07 +05309264 ei->i_otime.tv_sec = 0;
9265 ei->i_otime.tv_nsec = 0;
9266
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009267 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009268 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009269 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9270 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009271 ei->io_tree.track_uptodate = 1;
9272 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009273 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009275 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009276 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009277 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009278 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009279 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009280 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009281
9282 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009283}
9284
Josef Bacikaaedb552013-10-11 14:44:09 -04009285#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9286void btrfs_test_destroy_inode(struct inode *inode)
9287{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009288 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009289 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9290}
9291#endif
9292
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009293static void btrfs_i_callback(struct rcu_head *head)
9294{
9295 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009296 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9297}
9298
Chris Mason39279cc2007-06-12 06:35:45 -04009299void btrfs_destroy_inode(struct inode *inode)
9300{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009302 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009303 struct btrfs_root *root = BTRFS_I(inode)->root;
9304
Al Virob3d9b7a2012-06-09 13:51:19 -04009305 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009306 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009307 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9308 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009309 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9310 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009311 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009312
Chris Mason5a3f23d2009-03-31 13:27:11 -04009313 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009314 * This can happen where we create an inode, but somebody else also
9315 * created the same inode and we need to destroy the one we already
9316 * created.
9317 */
9318 if (!root)
9319 goto free;
9320
Josef Bacik8a35d952012-05-23 14:26:42 -04009321 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9322 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009323 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009324 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009325 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009326 }
Josef Bacik7b128762008-07-24 12:17:14 -04009327
Chris Masond3977122009-01-05 21:25:51 -05009328 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009329 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9330 if (!ordered)
9331 break;
9332 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009333 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009334 "found ordered extent %llu %llu on inode cleanup",
9335 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009336 btrfs_remove_ordered_extent(inode, ordered);
9337 btrfs_put_ordered_extent(ordered);
9338 btrfs_put_ordered_extent(ordered);
9339 }
9340 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009341 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009342 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009343 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009344free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009345 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009346}
9347
Al Viro45321ac2010-06-07 13:43:19 -04009348int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009349{
9350 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009351
Naohiro Aota6379ef92013-06-06 09:56:34 +00009352 if (root == NULL)
9353 return 1;
9354
Liu Bofa6ac872013-02-20 14:10:23 +00009355 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009356 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009357 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009358 else
Al Viro45321ac2010-06-07 13:43:19 -04009359 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009360}
9361
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009362static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009363{
9364 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9365
9366 inode_init_once(&ei->vfs_inode);
9367}
9368
9369void btrfs_destroy_cachep(void)
9370{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009371 /*
9372 * Make sure all delayed rcu free inodes are flushed before we
9373 * destroy cache.
9374 */
9375 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009376 kmem_cache_destroy(btrfs_inode_cachep);
9377 kmem_cache_destroy(btrfs_trans_handle_cachep);
9378 kmem_cache_destroy(btrfs_transaction_cachep);
9379 kmem_cache_destroy(btrfs_path_cachep);
9380 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009381}
9382
9383int btrfs_init_cachep(void)
9384{
David Sterba837e1972012-09-07 03:00:48 -06009385 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009386 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009387 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9388 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009389 if (!btrfs_inode_cachep)
9390 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009391
David Sterba837e1972012-09-07 03:00:48 -06009392 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009393 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009394 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 if (!btrfs_trans_handle_cachep)
9396 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009400 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 if (!btrfs_transaction_cachep)
9402 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403
David Sterba837e1972012-09-07 03:00:48 -06009404 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009405 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009406 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009407 if (!btrfs_path_cachep)
9408 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009409
David Sterba837e1972012-09-07 03:00:48 -06009410 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009411 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009412 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009413 if (!btrfs_free_space_cachep)
9414 goto fail;
9415
Chris Mason39279cc2007-06-12 06:35:45 -04009416 return 0;
9417fail:
9418 btrfs_destroy_cachep();
9419 return -ENOMEM;
9420}
9421
9422static int btrfs_getattr(struct vfsmount *mnt,
9423 struct dentry *dentry, struct kstat *stat)
9424{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009425 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009426 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009427 u32 blocksize = inode->i_sb->s_blocksize;
9428
Chris Mason39279cc2007-06-12 06:35:45 -04009429 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009430 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009431
9432 spin_lock(&BTRFS_I(inode)->lock);
9433 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9434 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009435 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009436 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009437 return 0;
9438}
9439
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009440static int btrfs_rename_exchange(struct inode *old_dir,
9441 struct dentry *old_dentry,
9442 struct inode *new_dir,
9443 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009445 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009446 struct btrfs_trans_handle *trans;
9447 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009448 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009449 struct inode *new_inode = new_dentry->d_inode;
9450 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009451 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009452 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009453 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9454 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455 u64 old_idx = 0;
9456 u64 new_idx = 0;
9457 u64 root_objectid;
9458 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009459 bool root_log_pinned = false;
9460 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461
9462 /* we only allow rename subvolume link between subvolumes */
9463 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9464 return -EXDEV;
9465
9466 /* close the race window with snapshot create/destroy ioctl */
9467 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009468 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009471
9472 /*
9473 * We want to reserve the absolute worst case amount of items. So if
9474 * both inodes are subvols and we need to unlink them then that would
9475 * require 4 item modifications, but if they are both normal inodes it
9476 * would require 5 item modifications, so we'll assume their normal
9477 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9478 * should cover the worst case number of items we'll modify.
9479 */
9480 trans = btrfs_start_transaction(root, 12);
9481 if (IS_ERR(trans)) {
9482 ret = PTR_ERR(trans);
9483 goto out_notrans;
9484 }
9485
9486 /*
9487 * We need to find a free sequence number both in the source and
9488 * in the destination directory for the exchange.
9489 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009490 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 if (ret)
9492 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009493 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009494 if (ret)
9495 goto out_fail;
9496
9497 BTRFS_I(old_inode)->dir_index = 0ULL;
9498 BTRFS_I(new_inode)->dir_index = 0ULL;
9499
9500 /* Reference for the source. */
9501 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9502 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009503 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009505 btrfs_pin_log_trans(root);
9506 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 ret = btrfs_insert_inode_ref(trans, dest,
9508 new_dentry->d_name.name,
9509 new_dentry->d_name.len,
9510 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009511 btrfs_ino(BTRFS_I(new_dir)),
9512 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 if (ret)
9514 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 }
9516
9517 /* And now for the dest. */
9518 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9519 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009520 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009522 btrfs_pin_log_trans(dest);
9523 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 ret = btrfs_insert_inode_ref(trans, root,
9525 old_dentry->d_name.name,
9526 old_dentry->d_name.len,
9527 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009528 btrfs_ino(BTRFS_I(old_dir)),
9529 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530 if (ret)
9531 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 }
9533
9534 /* Update inode version and ctime/mtime. */
9535 inode_inc_iversion(old_dir);
9536 inode_inc_iversion(new_dir);
9537 inode_inc_iversion(old_inode);
9538 inode_inc_iversion(new_inode);
9539 old_dir->i_ctime = old_dir->i_mtime = ctime;
9540 new_dir->i_ctime = new_dir->i_mtime = ctime;
9541 old_inode->i_ctime = ctime;
9542 new_inode->i_ctime = ctime;
9543
9544 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009545 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9546 BTRFS_I(old_inode), 1);
9547 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9548 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 }
9550
9551 /* src is a subvolume */
9552 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9553 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9554 ret = btrfs_unlink_subvol(trans, root, old_dir,
9555 root_objectid,
9556 old_dentry->d_name.name,
9557 old_dentry->d_name.len);
9558 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009559 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9560 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 old_dentry->d_name.name,
9562 old_dentry->d_name.len);
9563 if (!ret)
9564 ret = btrfs_update_inode(trans, root, old_inode);
9565 }
9566 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009567 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009568 goto out_fail;
9569 }
9570
9571 /* dest is a subvolume */
9572 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9573 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9574 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9575 root_objectid,
9576 new_dentry->d_name.name,
9577 new_dentry->d_name.len);
9578 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009579 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9580 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 new_dentry->d_name.name,
9582 new_dentry->d_name.len);
9583 if (!ret)
9584 ret = btrfs_update_inode(trans, dest, new_inode);
9585 }
9586 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009587 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 goto out_fail;
9589 }
9590
9591 ret = btrfs_add_link(trans, new_dir, old_inode,
9592 new_dentry->d_name.name,
9593 new_dentry->d_name.len, 0, old_idx);
9594 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009595 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 goto out_fail;
9597 }
9598
9599 ret = btrfs_add_link(trans, old_dir, new_inode,
9600 old_dentry->d_name.name,
9601 old_dentry->d_name.len, 0, new_idx);
9602 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009603 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 goto out_fail;
9605 }
9606
9607 if (old_inode->i_nlink == 1)
9608 BTRFS_I(old_inode)->dir_index = old_idx;
9609 if (new_inode->i_nlink == 1)
9610 BTRFS_I(new_inode)->dir_index = new_idx;
9611
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009614 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9615 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009621 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9622 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009624 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 }
9626out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009627 /*
9628 * If we have pinned a log and an error happened, we unpin tasks
9629 * trying to sync the log and force them to fallback to a transaction
9630 * commit if the log currently contains any of the inodes involved in
9631 * this rename operation (to ensure we do not persist a log with an
9632 * inconsistent state for any of these inodes or leading to any
9633 * inconsistencies when replayed). If the transaction was aborted, the
9634 * abortion reason is propagated to userspace when attempting to commit
9635 * the transaction. If the log does not contain any of these inodes, we
9636 * allow the tasks to sync it.
9637 */
9638 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009639 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9640 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9641 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009642 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009643 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009644 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009645
9646 if (root_log_pinned) {
9647 btrfs_end_log_trans(root);
9648 root_log_pinned = false;
9649 }
9650 if (dest_log_pinned) {
9651 btrfs_end_log_trans(dest);
9652 dest_log_pinned = false;
9653 }
9654 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009655 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656out_notrans:
9657 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009658 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009660 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661
9662 return ret;
9663}
9664
9665static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9666 struct btrfs_root *root,
9667 struct inode *dir,
9668 struct dentry *dentry)
9669{
9670 int ret;
9671 struct inode *inode;
9672 u64 objectid;
9673 u64 index;
9674
9675 ret = btrfs_find_free_ino(root, &objectid);
9676 if (ret)
9677 return ret;
9678
9679 inode = btrfs_new_inode(trans, root, dir,
9680 dentry->d_name.name,
9681 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009682 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009683 objectid,
9684 S_IFCHR | WHITEOUT_MODE,
9685 &index);
9686
9687 if (IS_ERR(inode)) {
9688 ret = PTR_ERR(inode);
9689 return ret;
9690 }
9691
9692 inode->i_op = &btrfs_special_inode_operations;
9693 init_special_inode(inode, inode->i_mode,
9694 WHITEOUT_DEV);
9695
9696 ret = btrfs_init_inode_security(trans, inode, dir,
9697 &dentry->d_name);
9698 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009699 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700
9701 ret = btrfs_add_nondir(trans, dir, dentry,
9702 inode, 0, index);
9703 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009704 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705
9706 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009707out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009709 if (ret)
9710 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 iput(inode);
9712
Filipe Mananac9901612016-05-05 01:41:57 +01009713 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714}
9715
Chris Mason39279cc2007-06-12 06:35:45 -04009716static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 struct inode *new_dir, struct dentry *new_dentry,
9718 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009720 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009721 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009722 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009723 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9724 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009725 struct inode *new_inode = d_inode(new_dentry);
9726 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009727 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009728 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009729 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009730 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009731 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009732
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009733 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009734 return -EPERM;
9735
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009737 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009738 return -EXDEV;
9739
Li Zefan33345d012011-04-20 10:31:50 +08009740 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009741 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009742 return -ENOTEMPTY;
9743
Chris Mason39279cc2007-06-12 06:35:45 -04009744 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009745 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009746 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009747
9748
9749 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009750 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009751 new_dentry->d_name.name,
9752 new_dentry->d_name.len);
9753
9754 if (ret) {
9755 if (ret == -EEXIST) {
9756 /* we shouldn't get
9757 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309758 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009759 return ret;
9760 }
9761 } else {
9762 /* maybe -EOVERFLOW */
9763 return ret;
9764 }
9765 }
9766 ret = 0;
9767
Chris Mason5a3f23d2009-03-31 13:27:11 -04009768 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009769 * we're using rename to replace one file with another. Start IO on it
9770 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009771 */
Chris Mason8d875f92014-08-12 10:47:42 -07009772 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009773 filemap_flush(old_inode->i_mapping);
9774
Yan, Zheng76dda932009-09-21 16:00:26 -04009775 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009776 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009777 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009778 /*
9779 * We want to reserve the absolute worst case amount of items. So if
9780 * both inodes are subvols and we need to unlink them then that would
9781 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009782 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009783 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9784 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009785 * If our rename has the whiteout flag, we need more 5 units for the
9786 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9787 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009788 */
Filipe Manana5062af32016-05-05 10:26:26 +01009789 trans_num_items = 11;
9790 if (flags & RENAME_WHITEOUT)
9791 trans_num_items += 5;
9792 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009793 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794 ret = PTR_ERR(trans);
9795 goto out_notrans;
9796 }
Chris Mason5f39d392007-10-15 16:14:19 -04009797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009798 if (dest != root)
9799 btrfs_record_root_in_trans(trans, dest);
9800
Nikolay Borisov877574e2017-02-20 13:50:33 +02009801 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009802 if (ret)
9803 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009804
Miao Xie67de1172013-12-26 13:07:06 +08009805 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009806 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009808 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009809 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009810 btrfs_pin_log_trans(root);
9811 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009812 ret = btrfs_insert_inode_ref(trans, dest,
9813 new_dentry->d_name.name,
9814 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009815 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009816 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009817 if (ret)
9818 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009819 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009820
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009821 inode_inc_iversion(old_dir);
9822 inode_inc_iversion(new_dir);
9823 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009824 old_dir->i_ctime = old_dir->i_mtime =
9825 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009826 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009827
Chris Mason12fcfd22009-03-24 10:24:20 -04009828 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009829 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9830 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009831
Li Zefan33345d012011-04-20 10:31:50 +08009832 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009833 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9834 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9835 old_dentry->d_name.name,
9836 old_dentry->d_name.len);
9837 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009838 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9839 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009840 old_dentry->d_name.name,
9841 old_dentry->d_name.len);
9842 if (!ret)
9843 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009844 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009845 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009846 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009847 goto out_fail;
9848 }
Chris Mason39279cc2007-06-12 06:35:45 -04009849
9850 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009851 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009852 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009853 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009854 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9855 root_objectid = BTRFS_I(new_inode)->location.objectid;
9856 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9857 root_objectid,
9858 new_dentry->d_name.name,
9859 new_dentry->d_name.len);
9860 BUG_ON(new_inode->i_nlink == 0);
9861 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009862 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9863 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009864 new_dentry->d_name.name,
9865 new_dentry->d_name.len);
9866 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009867 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009868 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009869 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009870 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009871 goto out_fail;
9872 }
Chris Mason39279cc2007-06-12 06:35:45 -04009873 }
Josef Bacikaec74772008-07-24 12:12:38 -04009874
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 ret = btrfs_add_link(trans, new_dir, old_inode,
9876 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009877 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009878 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009879 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009880 goto out_fail;
9881 }
Chris Mason39279cc2007-06-12 06:35:45 -04009882
Miao Xie67de1172013-12-26 13:07:06 +08009883 if (old_inode->i_nlink == 1)
9884 BTRFS_I(old_inode)->dir_index = index;
9885
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009886 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009887 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009888
David Sterbaf85b7372017-01-20 14:54:07 +01009889 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9890 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009891 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009892 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009893 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009894
9895 if (flags & RENAME_WHITEOUT) {
9896 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9897 old_dentry);
9898
9899 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009900 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009901 goto out_fail;
9902 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009903 }
Chris Mason39279cc2007-06-12 06:35:45 -04009904out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009905 /*
9906 * If we have pinned the log and an error happened, we unpin tasks
9907 * trying to sync the log and force them to fallback to a transaction
9908 * commit if the log currently contains any of the inodes involved in
9909 * this rename operation (to ensure we do not persist a log with an
9910 * inconsistent state for any of these inodes or leading to any
9911 * inconsistencies when replayed). If the transaction was aborted, the
9912 * abortion reason is propagated to userspace when attempting to commit
9913 * the transaction. If the log does not contain any of these inodes, we
9914 * allow the tasks to sync it.
9915 */
9916 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009917 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9918 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9919 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009920 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009921 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009922 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009923
9924 btrfs_end_log_trans(root);
9925 log_pinned = false;
9926 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009927 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009928out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009929 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009930 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009931
Chris Mason39279cc2007-06-12 06:35:45 -04009932 return ret;
9933}
9934
Miklos Szeredi80ace852014-07-23 15:15:32 +02009935static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9936 struct inode *new_dir, struct dentry *new_dentry,
9937 unsigned int flags)
9938{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009939 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009940 return -EINVAL;
9941
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009942 if (flags & RENAME_EXCHANGE)
9943 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9944 new_dentry);
9945
9946 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009947}
9948
Miao Xie8ccf6f192012-10-25 09:28:04 +00009949static void btrfs_run_delalloc_work(struct btrfs_work *work)
9950{
9951 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009952 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009953
9954 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9955 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009956 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009957 filemap_flush(inode->i_mapping);
9958 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9959 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009960 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961
9962 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009963 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009964 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009965 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009966 complete(&delalloc_work->completion);
9967}
9968
9969struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009970 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971{
9972 struct btrfs_delalloc_work *work;
9973
David Sterba100d5702015-12-08 14:39:32 +01009974 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009975 if (!work)
9976 return NULL;
9977
9978 init_completion(&work->completion);
9979 INIT_LIST_HEAD(&work->list);
9980 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009982 WARN_ON_ONCE(!inode);
9983 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9984 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009985
9986 return work;
9987}
9988
9989void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9990{
9991 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009992 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009993}
9994
Chris Masond352ac62008-09-29 15:18:18 -04009995/*
9996 * some fairly slow code that needs optimization. This walks the list
9997 * of all the inodes with pending delalloc and forces them to disk.
9998 */
Miao Xie6c255e62014-03-06 13:55:01 +08009999static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10000 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010001{
Chris Masonea8c2812008-08-04 23:17:27 -040010002 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010003 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010004 struct btrfs_delalloc_work *work, *next;
10005 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010006 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010008
Miao Xie8ccf6f192012-10-25 09:28:04 +000010009 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010010 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010011
Miao Xie573bfb72014-03-06 13:55:03 +080010012 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010013 spin_lock(&root->delalloc_lock);
10014 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010015 while (!list_empty(&splice)) {
10016 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010017 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010018
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 list_move_tail(&binode->delalloc_inodes,
10020 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010021 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010022 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010023 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010024 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010025 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010026 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010027
David Sterba651d4942015-11-27 19:24:16 +010010028 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010029 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010030 if (delay_iput)
10031 btrfs_add_delayed_iput(inode);
10032 else
10033 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010034 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010035 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010036 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010037 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010038 btrfs_queue_work(root->fs_info->flush_workers,
10039 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010040 ret++;
10041 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010042 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010043 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010044 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010045 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010046 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010047
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010048out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010049 list_for_each_entry_safe(work, next, &works, list) {
10050 list_del_init(&work->list);
10051 btrfs_wait_and_free_delalloc_work(work);
10052 }
10053
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 if (!list_empty_careful(&splice)) {
10055 spin_lock(&root->delalloc_lock);
10056 list_splice_tail(&splice, &root->delalloc_inodes);
10057 spin_unlock(&root->delalloc_lock);
10058 }
Miao Xie573bfb72014-03-06 13:55:03 +080010059 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010060 return ret;
10061}
10062
10063int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10064{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010065 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 int ret;
10067
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010068 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 return -EROFS;
10070
Miao Xie6c255e62014-03-06 13:55:01 +080010071 ret = __start_delalloc_inodes(root, delay_iput, -1);
10072 if (ret > 0)
10073 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010074 /*
10075 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010076 * we have to make sure the IO is actually started and that
10077 * ordered extents get created before we return
10078 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010079 atomic_inc(&fs_info->async_submit_draining);
10080 while (atomic_read(&fs_info->nr_async_submits) ||
10081 atomic_read(&fs_info->async_delalloc_pages)) {
10082 wait_event(fs_info->async_submit_wait,
10083 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10084 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010085 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010086 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087 return ret;
10088}
10089
Miao Xie6c255e62014-03-06 13:55:01 +080010090int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10091 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092{
10093 struct btrfs_root *root;
10094 struct list_head splice;
10095 int ret;
10096
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010097 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 return -EROFS;
10099
10100 INIT_LIST_HEAD(&splice);
10101
Miao Xie573bfb72014-03-06 13:55:03 +080010102 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010103 spin_lock(&fs_info->delalloc_root_lock);
10104 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010105 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010106 root = list_first_entry(&splice, struct btrfs_root,
10107 delalloc_root);
10108 root = btrfs_grab_fs_root(root);
10109 BUG_ON(!root);
10110 list_move_tail(&root->delalloc_root,
10111 &fs_info->delalloc_roots);
10112 spin_unlock(&fs_info->delalloc_root_lock);
10113
Miao Xie6c255e62014-03-06 13:55:01 +080010114 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010116 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 goto out;
10118
Miao Xie6c255e62014-03-06 13:55:01 +080010119 if (nr != -1) {
10120 nr -= ret;
10121 WARN_ON(nr < 0);
10122 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 spin_lock(&fs_info->delalloc_root_lock);
10124 }
10125 spin_unlock(&fs_info->delalloc_root_lock);
10126
Miao Xie6c255e62014-03-06 13:55:01 +080010127 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128 atomic_inc(&fs_info->async_submit_draining);
10129 while (atomic_read(&fs_info->nr_async_submits) ||
10130 atomic_read(&fs_info->async_delalloc_pages)) {
10131 wait_event(fs_info->async_submit_wait,
10132 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10133 atomic_read(&fs_info->async_delalloc_pages) == 0));
10134 }
10135 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010136out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010137 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 spin_lock(&fs_info->delalloc_root_lock);
10139 list_splice_tail(&splice, &fs_info->delalloc_roots);
10140 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010141 }
Miao Xie573bfb72014-03-06 13:55:03 +080010142 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010143 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010144}
10145
Chris Mason39279cc2007-06-12 06:35:45 -040010146static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10147 const char *symname)
10148{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010149 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010150 struct btrfs_trans_handle *trans;
10151 struct btrfs_root *root = BTRFS_I(dir)->root;
10152 struct btrfs_path *path;
10153 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010154 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010155 int err;
10156 int drop_inode = 0;
10157 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010158 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010159 int name_len;
10160 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010161 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010162 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010163 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010164
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010165 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010166 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010167 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010168
Josef Bacik9ed74f22009-09-11 16:12:44 -040010169 /*
10170 * 2 items for inode item and ref
10171 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010172 * 1 item for updating parent inode item
10173 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010174 * 1 item for xattr if selinux is on
10175 */
Filipe Manana9269d122015-12-31 18:16:29 +000010176 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010177 if (IS_ERR(trans))
10178 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010179
Li Zefan581bb052011-04-20 10:06:11 +080010180 err = btrfs_find_free_ino(root, &objectid);
10181 if (err)
10182 goto out_unlock;
10183
Josef Bacikaec74772008-07-24 12:12:38 -040010184 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010185 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10186 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010187 if (IS_ERR(inode)) {
10188 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010189 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010190 }
Chris Mason39279cc2007-06-12 06:35:45 -040010191
Casey Schauflerad19db72011-12-15 10:09:07 -050010192 /*
10193 * If the active LSM wants to access the inode during
10194 * d_instantiate it needs these. Smack checks to see
10195 * if the filesystem supports xattrs by looking at the
10196 * ops vector.
10197 */
10198 inode->i_fop = &btrfs_file_operations;
10199 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010200 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010201 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10202
10203 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10204 if (err)
10205 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010206
Chris Mason39279cc2007-06-12 06:35:45 -040010207 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010208 if (!path) {
10209 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010210 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010211 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010212 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010213 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010214 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010215 datasize = btrfs_file_extent_calc_inline_size(name_len);
10216 err = btrfs_insert_empty_item(trans, root, path, &key,
10217 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010218 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010219 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010220 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010221 }
Chris Mason5f39d392007-10-15 16:14:19 -040010222 leaf = path->nodes[0];
10223 ei = btrfs_item_ptr(leaf, path->slots[0],
10224 struct btrfs_file_extent_item);
10225 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10226 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010227 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010228 btrfs_set_file_extent_encryption(leaf, ei, 0);
10229 btrfs_set_file_extent_compression(leaf, ei, 0);
10230 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10231 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10232
Chris Mason39279cc2007-06-12 06:35:45 -040010233 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010234 write_extent_buffer(leaf, symname, ptr, name_len);
10235 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010236 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010237
Chris Mason39279cc2007-06-12 06:35:45 -040010238 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010239 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010240 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010241 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010242 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010243 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010244 /*
10245 * Last step, add directory indexes for our symlink inode. This is the
10246 * last step to avoid extra cleanup of these indexes if an error happens
10247 * elsewhere above.
10248 */
10249 if (!err)
10250 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010251 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010252 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010253 goto out_unlock_inode;
10254 }
10255
10256 unlock_new_inode(inode);
10257 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010258
10259out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010260 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010261 if (drop_inode) {
10262 inode_dec_link_count(inode);
10263 iput(inode);
10264 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010265 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010266 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010267
10268out_unlock_inode:
10269 drop_inode = 1;
10270 unlock_new_inode(inode);
10271 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010272}
Chris Mason16432982008-04-10 10:23:21 -040010273
Josef Bacik0af3d002010-06-21 14:48:16 -040010274static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10275 u64 start, u64 num_bytes, u64 min_size,
10276 loff_t actual_len, u64 *alloc_hint,
10277 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010280 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10281 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010282 struct btrfs_root *root = BTRFS_I(inode)->root;
10283 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010285 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010286 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010287 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010288 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010289 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010290 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010291
Josef Bacik0af3d002010-06-21 14:48:16 -040010292 if (trans)
10293 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010295 if (own_trans) {
10296 trans = btrfs_start_transaction(root, 3);
10297 if (IS_ERR(trans)) {
10298 ret = PTR_ERR(trans);
10299 break;
10300 }
Yan Zhengd899e052008-10-30 14:25:28 -040010301 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010302
Byongho Leeee221842015-12-15 01:42:10 +090010303 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010304 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010305 /*
10306 * If we are severely fragmented we could end up with really
10307 * small allocations, so if the allocator is returning small
10308 * chunks lets make its job easier by only searching for those
10309 * sized chunks.
10310 */
10311 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010312 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10313 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010314 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010315 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010316 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010317 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010318 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010319 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010320
Josef Bacik0b670dc2015-09-23 17:11:16 -040010321 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010322 ret = insert_reserved_file_extent(trans, inode,
10323 cur_offset, ins.objectid,
10324 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010325 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010326 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010328 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010329 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010330 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010331 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010332 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010333 break;
10334 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010335
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010336 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010337 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338
Josef Bacik5dc562c2012-08-17 13:14:17 -040010339 em = alloc_extent_map();
10340 if (!em) {
10341 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10342 &BTRFS_I(inode)->runtime_flags);
10343 goto next;
10344 }
10345
10346 em->start = cur_offset;
10347 em->orig_start = cur_offset;
10348 em->len = ins.offset;
10349 em->block_start = ins.objectid;
10350 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010351 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010352 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010353 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010354 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10355 em->generation = trans->transid;
10356
10357 while (1) {
10358 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010359 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010360 write_unlock(&em_tree->lock);
10361 if (ret != -EEXIST)
10362 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010363 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010364 cur_offset + ins.offset - 1,
10365 0);
10366 }
10367 free_extent_map(em);
10368next:
Yan Zhengd899e052008-10-30 14:25:28 -040010369 num_bytes -= ins.offset;
10370 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010371 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010372
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010373 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010374 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010375 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010376 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010377 (actual_len > inode->i_size) &&
10378 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010379 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010380 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010381 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010382 i_size = cur_offset;
10383 i_size_write(inode, i_size);
10384 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010385 }
10386
Yan Zhengd899e052008-10-30 14:25:28 -040010387 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388
10389 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010390 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010391 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010392 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010393 break;
10394 }
Yan Zhengd899e052008-10-30 14:25:28 -040010395
Josef Bacik0af3d002010-06-21 14:48:16 -040010396 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010397 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010398 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010399 if (cur_offset < end)
10400 btrfs_free_reserved_data_space(inode, cur_offset,
10401 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010402 return ret;
10403}
10404
Josef Bacik0af3d002010-06-21 14:48:16 -040010405int btrfs_prealloc_file_range(struct inode *inode, int mode,
10406 u64 start, u64 num_bytes, u64 min_size,
10407 loff_t actual_len, u64 *alloc_hint)
10408{
10409 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10410 min_size, actual_len, alloc_hint,
10411 NULL);
10412}
10413
10414int btrfs_prealloc_file_range_trans(struct inode *inode,
10415 struct btrfs_trans_handle *trans, int mode,
10416 u64 start, u64 num_bytes, u64 min_size,
10417 loff_t actual_len, u64 *alloc_hint)
10418{
10419 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10420 min_size, actual_len, alloc_hint, trans);
10421}
10422
Chris Masone6dcd2d2008-07-17 12:53:50 -040010423static int btrfs_set_page_dirty(struct page *page)
10424{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010425 return __set_page_dirty_nobuffers(page);
10426}
10427
Al Viro10556cb22011-06-20 19:28:19 -040010428static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010429{
Li Zefanb83cc962010-12-20 16:04:08 +080010430 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010431 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010432
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010433 if (mask & MAY_WRITE &&
10434 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10435 if (btrfs_root_readonly(root))
10436 return -EROFS;
10437 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10438 return -EACCES;
10439 }
Al Viro2830ba72011-06-20 19:16:29 -040010440 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010441}
Chris Mason39279cc2007-06-12 06:35:45 -040010442
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010443static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10444{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010445 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010446 struct btrfs_trans_handle *trans;
10447 struct btrfs_root *root = BTRFS_I(dir)->root;
10448 struct inode *inode = NULL;
10449 u64 objectid;
10450 u64 index;
10451 int ret = 0;
10452
10453 /*
10454 * 5 units required for adding orphan entry
10455 */
10456 trans = btrfs_start_transaction(root, 5);
10457 if (IS_ERR(trans))
10458 return PTR_ERR(trans);
10459
10460 ret = btrfs_find_free_ino(root, &objectid);
10461 if (ret)
10462 goto out;
10463
10464 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010465 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 if (IS_ERR(inode)) {
10467 ret = PTR_ERR(inode);
10468 inode = NULL;
10469 goto out;
10470 }
10471
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010472 inode->i_fop = &btrfs_file_operations;
10473 inode->i_op = &btrfs_file_inode_operations;
10474
10475 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010476 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10477
Chris Masonb0d5d102014-09-08 13:08:51 -070010478 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10479 if (ret)
10480 goto out_inode;
10481
10482 ret = btrfs_update_inode(trans, root, inode);
10483 if (ret)
10484 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010485 ret = btrfs_orphan_add(trans, inode);
10486 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010487 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010488
Filipe Manana5762b5c2014-08-01 00:10:32 +010010489 /*
10490 * We set number of links to 0 in btrfs_new_inode(), and here we set
10491 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10492 * through:
10493 *
10494 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10495 */
10496 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010497 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498 d_tmpfile(dentry, inode);
10499 mark_inode_dirty(inode);
10500
10501out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010502 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 if (ret)
10504 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010505 btrfs_balance_delayed_items(fs_info);
10506 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010507 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010508
10509out_inode:
10510 unlock_new_inode(inode);
10511 goto out;
10512
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010513}
10514
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010515static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010516 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010517 .lookup = btrfs_lookup,
10518 .create = btrfs_create,
10519 .unlink = btrfs_unlink,
10520 .link = btrfs_link,
10521 .mkdir = btrfs_mkdir,
10522 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010523 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010524 .symlink = btrfs_symlink,
10525 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010526 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010527 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010528 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010529 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010530 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010531 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010532 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010533};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010534static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010535 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010536 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010537 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010538};
Yan, Zheng76dda932009-09-21 16:00:26 -040010539
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010540static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010541 .llseek = generic_file_llseek,
10542 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010543 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010544 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010545#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010546 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010547#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010548 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010549 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010550};
10551
David Sterba20e55062015-11-19 11:42:28 +010010552static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010553 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010554 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010555 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010556 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010557 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010558 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010559 .set_bit_hook = btrfs_set_bit_hook,
10560 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010561 .merge_extent_hook = btrfs_merge_extent_hook,
10562 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010563};
10564
Chris Mason35054392009-01-21 13:11:13 -050010565/*
10566 * btrfs doesn't support the bmap operation because swapfiles
10567 * use bmap to make a mapping of extents in the file. They assume
10568 * these extents won't change over the life of the file and they
10569 * use the bmap result to do IO directly to the drive.
10570 *
10571 * the btrfs bmap call would return logical addresses that aren't
10572 * suitable for IO and they also will change frequently as COW
10573 * operations happen. So, swapfile + btrfs == corruption.
10574 *
10575 * For now we're avoiding this by dropping bmap.
10576 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010577static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010578 .readpage = btrfs_readpage,
10579 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010580 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010581 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010582 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010583 .invalidatepage = btrfs_invalidatepage,
10584 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010585 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010586 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010587};
10588
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010589static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .readpage = btrfs_readpage,
10591 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010592 .invalidatepage = btrfs_invalidatepage,
10593 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010594};
10595
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010596static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010597 .getattr = btrfs_getattr,
10598 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010599 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010600 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010601 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010602 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010603 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010604 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010605};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010606static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010607 .getattr = btrfs_getattr,
10608 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010609 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010610 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010611 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010612 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010613 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010614};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010615static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010616 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010617 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010618 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010619 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010620 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010621 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010622};
Yan, Zheng76dda932009-09-21 16:00:26 -040010623
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010624const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010625 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010626 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010627};