blob: 175c28a94a576a2cf573d81604e28766f907c2fa [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400319 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400320 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400321out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800322 /*
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
327 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300328 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000329 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400330 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400331 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400332}
333
Chris Mason771ed682008-11-06 22:02:51 -0500334struct async_extent {
335 u64 start;
336 u64 ram_size;
337 u64 compressed_size;
338 struct page **pages;
339 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800340 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500341 struct list_head list;
342};
343
344struct async_cow {
345 struct inode *inode;
346 struct btrfs_root *root;
347 struct page *locked_page;
348 u64 start;
349 u64 end;
350 struct list_head extents;
351 struct btrfs_work work;
352};
353
354static noinline int add_async_extent(struct async_cow *cow,
355 u64 start, u64 ram_size,
356 u64 compressed_size,
357 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800358 unsigned long nr_pages,
359 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500360{
361 struct async_extent *async_extent;
362
363 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100364 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500365 async_extent->start = start;
366 async_extent->ram_size = ram_size;
367 async_extent->compressed_size = compressed_size;
368 async_extent->pages = pages;
369 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 list_add_tail(&async_extent->list, &cow->extents);
372 return 0;
373}
374
Wang Shilongf79707b2014-07-17 11:44:09 +0800375static inline int inode_need_compress(struct inode *inode)
376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800378
379 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400380 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800381 return 1;
382 /* bad compression ratios */
383 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
384 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400385 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800386 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 BTRFS_I(inode)->force_compress)
388 return 1;
389 return 0;
390}
391
Anand Jain26d30f82016-12-19 19:09:06 +0800392static inline void inode_should_defrag(struct inode *inode,
393 u64 start, u64 end, u64 num_bytes, u64 small_write)
394{
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes < small_write &&
397 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 btrfs_add_inode_defrag(NULL, inode);
399}
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401/*
Chris Mason771ed682008-11-06 22:02:51 -0500402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400405 *
Chris Mason771ed682008-11-06 22:02:51 -0500406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 *
Chris Mason771ed682008-11-06 22:02:51 -0500412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300415 * are written in the same order that the flusher thread sent them
416 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400417 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100418static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page,
420 u64 start, u64 end,
421 struct async_cow *async_cow,
422 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400426 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400431 struct page **pages = NULL;
432 unsigned long nr_pages;
433 unsigned long nr_pages_ret = 0;
434 unsigned long total_compressed = 0;
435 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900436 unsigned long max_compressed = SZ_128K;
437 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400438 int i;
439 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400440 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400441 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400442
Anand Jain26d30f82016-12-19 19:09:06 +0800443 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400444
Chris Mason42dc7ba2008-12-15 11:44:56 -0500445 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400446again:
447 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
449 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400450
Chris Masonf03d9301f2009-02-04 09:31:06 -0500451 /*
452 * we don't want to send crud past the end of i_size through
453 * compression, that's just a waste of CPU time. So, if the
454 * end of the file is before the start of our current
455 * requested range of bytes, we bail out to the uncompressed
456 * cleanup code that can deal with all of this.
457 *
458 * It isn't really the fastest way to fix things, but this is a
459 * very uncommon corner.
460 */
461 if (actual_end <= start)
462 goto cleanup_and_bail_uncompressed;
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_compressed = actual_end - start;
465
Shilong Wang4bcbb332014-10-07 18:44:35 -0400466 /*
467 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400468 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400469 */
470 if (total_compressed <= blocksize &&
471 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 goto cleanup_and_bail_uncompressed;
473
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we want to make sure that amount of ram required to uncompress
475 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500476 * of a compressed extent to 128k. This is a crucial number
477 * because it also controls how easily we can spread reads across
478 * cpus for decompression.
479 *
480 * We also want to make sure the amount of IO required to do
481 * a random read is reasonably small, so we limit the size of
482 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400483 */
484 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000485 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500486 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400487 total_in = 0;
488 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400489
Chris Mason771ed682008-11-06 22:02:51 -0500490 /*
491 * we do compression for mount -o compress and when the
492 * inode has not been flagged as nocompress. This flag can
493 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400494 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800495 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400496 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100497 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800498 if (!pages) {
499 /* just bail out to the uncompressed code */
500 goto cont;
501 }
Chris Mason179e29e2007-11-01 11:28:41 -0400502
Li Zefan261507a02010-12-17 14:21:50 +0800503 if (BTRFS_I(inode)->force_compress)
504 compress_type = BTRFS_I(inode)->force_compress;
505
Chris Mason4adaa612013-03-26 13:07:00 -0400506 /*
507 * we need to call clear_page_dirty_for_io on each
508 * page in the range. Otherwise applications with the file
509 * mmap'd can wander in and change the page contents while
510 * we are compressing them.
511 *
512 * If the compression fails for any reason, we set the pages
513 * dirty again later on.
514 */
515 extent_range_clear_dirty_for_io(inode, start, end);
516 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800517 ret = btrfs_compress_pages(compress_type,
518 inode->i_mapping, start,
519 total_compressed, pages,
520 nr_pages, &nr_pages_ret,
521 &total_in,
522 &total_compressed,
523 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400524
525 if (!ret) {
526 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300527 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 struct page *page = pages[nr_pages_ret - 1];
529 char *kaddr;
530
531 /* zero the tail end of the last page, we might be
532 * sending it down to disk
533 */
534 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800535 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300537 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800538 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400539 }
540 will_compress = 1;
541 }
542 }
Li Zefan560f7d72011-09-08 10:22:01 +0800543cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400544 if (start == 0) {
545 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500546 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400547 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500548 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400549 */
Josef Bacik00361582013-08-14 14:02:47 -0400550 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800551 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500553 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400554 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000555 total_compressed,
556 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400557 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400559 unsigned long clear_flags = EXTENT_DELALLOC |
560 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100561 unsigned long page_error_op;
562
Josef Bacik151a41b2013-07-29 13:22:24 -0400563 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100564 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400565
Chris Mason771ed682008-11-06 22:02:51 -0500566 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 * inline extent creation worked or returned error,
568 * we don't need to create any more async work items.
569 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500570 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800571 extent_clear_unlock_delalloc(inode, start, end, end,
572 NULL, clear_flags,
573 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400574 PAGE_CLEAR_DIRTY |
575 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400577 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800578 btrfs_free_reserved_data_space_noquota(inode, start,
579 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400580 goto free_pages_out;
581 }
582 }
583
584 if (will_compress) {
585 /*
586 * we aren't doing an inline extent round the compressed size
587 * up to a block size boundary so the allocator does sane
588 * things
589 */
Qu Wenruofda28322013-02-26 08:10:22 +0000590 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400591
592 /*
593 * one last check to make sure the compression is really a
594 * win, compare the page count read with the blocks on disk
595 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 if (total_compressed >= total_in) {
598 will_compress = 0;
599 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400600 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700601 *num_added += 1;
602
603 /*
604 * The async work queues will take care of doing actual
605 * allocation on disk for these compressed pages, and
606 * will submit them to the elevator.
607 */
608 add_async_extent(async_cow, start, num_bytes,
609 total_compressed, pages, nr_pages_ret,
610 compress_type);
611
612 if (start + num_bytes < end) {
613 start += num_bytes;
614 pages = NULL;
615 cond_resched();
616 goto again;
617 }
618 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400619 }
620 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400622 /*
623 * the compression code ran but failed to make things smaller,
624 * free any pages it allocated and our page pointer array
625 */
626 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400627 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
630 kfree(pages);
631 pages = NULL;
632 total_compressed = 0;
633 nr_pages_ret = 0;
634
635 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400636 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500637 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500638 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500639 }
Chris Masonc8b97812008-10-29 14:49:59 -0400640 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500641cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 /*
643 * No compression, but we still need to write the pages in the file
644 * we've been given so far. redirty the locked page if it corresponds
645 * to our extent and set things up for the async work queue to run
646 * cow_file_range to do the normal delalloc dance.
647 */
648 if (page_offset(locked_page) >= start &&
649 page_offset(locked_page) <= end)
650 __set_page_dirty_nobuffers(locked_page);
651 /* unlocked later on in the async handlers */
652
653 if (redirty)
654 extent_range_redirty_for_io(inode, start, end);
655 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
656 BTRFS_COMPRESS_NONE);
657 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500658
Filipe Mananac44f6492014-10-09 21:15:44 +0100659 return;
Chris Mason771ed682008-11-06 22:02:51 -0500660
661free_pages_out:
662 for (i = 0; i < nr_pages_ret; i++) {
663 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300664 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500665 }
Chris Masond3977122009-01-05 21:25:51 -0500666 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500667}
Chris Mason771ed682008-11-06 22:02:51 -0500668
Filipe Manana40ae8372014-10-06 22:14:24 +0100669static void free_async_extent_pages(struct async_extent *async_extent)
670{
671 int i;
672
673 if (!async_extent->pages)
674 return;
675
676 for (i = 0; i < async_extent->nr_pages; i++) {
677 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100679 }
680 kfree(async_extent->pages);
681 async_extent->nr_pages = 0;
682 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500683}
684
685/*
686 * phase two of compressed writeback. This is the ordered portion
687 * of the code, which only gets called in the order the work was
688 * queued. We walk all the async extents created by compress_file_range
689 * and send them down to the disk.
690 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100691static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500692 struct async_cow *async_cow)
693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500695 struct async_extent *async_extent;
696 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500697 struct btrfs_key ins;
698 struct extent_map *em;
699 struct btrfs_root *root = BTRFS_I(inode)->root;
700 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
701 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500703
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500704again:
Chris Masond3977122009-01-05 21:25:51 -0500705 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500706 async_extent = list_entry(async_cow->extents.next,
707 struct async_extent, list);
708 list_del(&async_extent->list);
709
710 io_tree = &BTRFS_I(inode)->io_tree;
711
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712retry:
Chris Mason771ed682008-11-06 22:02:51 -0500713 /* did the compression code fall back to uncompressed IO? */
714 if (!async_extent->pages) {
715 int page_started = 0;
716 unsigned long nr_written = 0;
717
718 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000719 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100720 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500721
722 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500723 ret = cow_file_range(inode, async_cow->locked_page,
724 async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800727 async_extent->start +
728 async_extent->ram_size - 1,
729 &page_started, &nr_written, 0,
730 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500731
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100732 /* JDM XXX */
733
Chris Mason771ed682008-11-06 22:02:51 -0500734 /*
735 * if page_started, cow_file_range inserted an
736 * inline extent and took care of all the unlocking
737 * and IO for us. Otherwise, we need to submit
738 * all those pages down to the drive.
739 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500741 extent_write_locked_range(io_tree,
742 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500743 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500744 async_extent->ram_size - 1,
745 btrfs_get_extent,
746 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500747 else if (ret)
748 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500749 kfree(async_extent);
750 cond_resched();
751 continue;
752 }
753
754 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100755 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500756
Wang Xiaoguang18513092016-07-25 15:51:40 +0800757 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500758 async_extent->compressed_size,
759 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800760 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100762 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400764 if (ret == -ENOSPC) {
765 unlock_extent(io_tree, async_extent->start,
766 async_extent->start +
767 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800768
769 /*
770 * we need to redirty the pages if we decide to
771 * fallback to uncompressed IO, otherwise we
772 * will not submit these pages down to lower
773 * layers.
774 */
775 extent_range_redirty_for_io(inode,
776 async_extent->start,
777 async_extent->start +
778 async_extent->ram_size - 1);
779
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100780 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400781 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500782 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500783 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000784 /*
785 * here we're doing allocation and writeback of the
786 * compressed pages
787 */
788 btrfs_drop_extent_cache(inode, async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1, 0);
791
David Sterba172ddd62011-04-21 00:48:27 +0200792 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000793 if (!em) {
794 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000796 }
Chris Mason771ed682008-11-06 22:02:51 -0500797 em->start = async_extent->start;
798 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500799 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400800 em->mod_start = em->start;
801 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500802
803 em->block_start = ins.objectid;
804 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500805 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400806 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400807 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800808 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500809 set_bit(EXTENT_FLAG_PINNED, &em->flags);
810 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400811 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500812
Chris Masond3977122009-01-05 21:25:51 -0500813 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400814 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400815 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400816 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500817 if (ret != -EEXIST) {
818 free_extent_map(em);
819 break;
820 }
821 btrfs_drop_extent_cache(inode, async_extent->start,
822 async_extent->start +
823 async_extent->ram_size - 1, 0);
824 }
825
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 if (ret)
827 goto out_free_reserve;
828
Li Zefan261507a02010-12-17 14:21:50 +0800829 ret = btrfs_add_ordered_extent_compress(inode,
830 async_extent->start,
831 ins.objectid,
832 async_extent->ram_size,
833 ins.offset,
834 BTRFS_ORDERED_COMPRESSED,
835 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100836 if (ret) {
837 btrfs_drop_extent_cache(inode, async_extent->start,
838 async_extent->start +
839 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500840 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100841 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500843
Chris Mason771ed682008-11-06 22:02:51 -0500844 /*
845 * clear dirty, set writeback and unlock the pages.
846 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400848 async_extent->start +
849 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800850 async_extent->start +
851 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400852 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
853 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500855 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500856 async_extent->start,
857 async_extent->ram_size,
858 ins.objectid,
859 ins.offset, async_extent->pages,
860 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 if (ret) {
862 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
863 struct page *p = async_extent->pages[0];
864 const u64 start = async_extent->start;
865 const u64 end = start + async_extent->ram_size - 1;
866
867 p->mapping = inode->i_mapping;
868 tree->ops->writepage_end_io_hook(p, start, end,
869 NULL, 0);
870 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800871 extent_clear_unlock_delalloc(inode, start, end, end,
872 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100873 PAGE_END_WRITEBACK |
874 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100876 }
Chris Mason771ed682008-11-06 22:02:51 -0500877 alloc_hint = ins.objectid + ins.offset;
878 kfree(async_extent);
879 cond_resched();
880 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100881 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500882out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400883 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400884 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 async_extent->start +
888 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800889 async_extent->start +
890 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400892 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
893 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100894 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
895 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100896 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100897 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500898 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500899}
900
Josef Bacik4b46fce2010-05-23 11:00:55 -0400901static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
902 u64 num_bytes)
903{
904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
905 struct extent_map *em;
906 u64 alloc_hint = 0;
907
908 read_lock(&em_tree->lock);
909 em = search_extent_mapping(em_tree, start, num_bytes);
910 if (em) {
911 /*
912 * if block start isn't an actual block number then find the
913 * first block in this inode and use that as a hint. If that
914 * block is also bogus then just don't worry about it.
915 */
916 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
917 free_extent_map(em);
918 em = search_extent_mapping(em_tree, 0, 0);
919 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
920 alloc_hint = em->block_start;
921 if (em)
922 free_extent_map(em);
923 } else {
924 alloc_hint = em->block_start;
925 free_extent_map(em);
926 }
927 }
928 read_unlock(&em_tree->lock);
929
930 return alloc_hint;
931}
932
Chris Mason771ed682008-11-06 22:02:51 -0500933/*
934 * when extent_io.c finds a delayed allocation range in the file,
935 * the call backs end up in this code. The basic idea is to
936 * allocate extents on disk for the range, and create ordered data structs
937 * in ram to track those extents.
938 *
939 * locked_page is the page that writepage had locked already. We use
940 * it to make sure we don't do extra locks or unlocks.
941 *
942 * *page_started is set to one if we unlock locked_page and do everything
943 * required to start IO on it. It may be clean and already done with
944 * IO when we return.
945 */
Josef Bacik00361582013-08-14 14:02:47 -0400946static noinline int cow_file_range(struct inode *inode,
947 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800948 u64 start, u64 end, u64 delalloc_end,
949 int *page_started, unsigned long *nr_written,
950 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500951{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500954 u64 alloc_hint = 0;
955 u64 num_bytes;
956 unsigned long ram_size;
957 u64 disk_num_bytes;
958 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500960 struct btrfs_key ins;
961 struct extent_map *em;
962 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
963 int ret = 0;
964
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400965 if (btrfs_is_free_space_inode(inode)) {
966 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500967 ret = -EINVAL;
968 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400969 }
Chris Mason771ed682008-11-06 22:02:51 -0500970
Qu Wenruofda28322013-02-26 08:10:22 +0000971 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500972 num_bytes = max(blocksize, num_bytes);
973 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500974
Anand Jain26d30f82016-12-19 19:09:06 +0800975 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400976
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (start == 0) {
978 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800979 ret = cow_file_range_inline(root, inode, start, end, 0,
980 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500981 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800982 extent_clear_unlock_delalloc(inode, start, end,
983 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400984 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400985 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400986 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
987 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800988 btrfs_free_reserved_data_space_noquota(inode, start,
989 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500990 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300991 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500992 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500993 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100995 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500996 }
997 }
Chris Masonc8b97812008-10-29 14:49:59 -0400998
999 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001000 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001001
Josef Bacik4b46fce2010-05-23 11:00:55 -04001002 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001003 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001004
Chris Masond3977122009-01-05 21:25:51 -05001005 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001006 unsigned long op;
1007
Josef Bacik287a0ab2010-03-19 18:07:23 +00001008 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001009 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001010 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001011 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001012 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001013 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001014
David Sterba172ddd62011-04-21 00:48:27 +02001015 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001016 if (!em) {
1017 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001018 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001021 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size = ins.offset;
1023 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001024 em->mod_start = em->start;
1025 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001026
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001028 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001029 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001030 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001031 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001032 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001033 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Chris Masond3977122009-01-05 21:25:51 -05001035 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001036 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001037 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001038 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001039 if (ret != -EEXIST) {
1040 free_extent_map(em);
1041 break;
1042 }
1043 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001044 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001045 }
Liu Boace68ba2013-04-22 10:53:47 +00001046 if (ret)
1047 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001048
Chris Mason98d20f62008-04-14 09:46:10 -04001049 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001050 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001051 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001052 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001053 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001054
Yan Zheng17d217f2008-12-12 10:03:38 -05001055 if (root->root_key.objectid ==
1056 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1057 ret = btrfs_reloc_clone_csums(inode, start,
1058 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001059 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001060 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001066 break;
Chris Masond3977122009-01-05 21:25:51 -05001067
Chris Masonc8b97812008-10-29 14:49:59 -04001068 /* we're not doing compressed IO, don't unlock the first
1069 * page (which the caller expects to stay locked), don't
1070 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001071 *
1072 * Do set the Private2 bit so we know this page was properly
1073 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001074 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 op = unlock ? PAGE_UNLOCK : 0;
1076 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001077
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001079 start + ram_size - 1,
1080 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001081 EXTENT_LOCKED | EXTENT_DELALLOC,
1082 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001083 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001087 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001089 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001090
Filipe Mananad9f85962014-08-25 10:43:00 +01001091out_drop_extent_cache:
1092 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001093out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001094 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001095 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001097 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1098 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001099 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1100 EXTENT_DELALLOC | EXTENT_DEFRAG,
1101 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1102 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001103 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001104}
Chris Masonc8b97812008-10-29 14:49:59 -04001105
Chris Mason771ed682008-11-06 22:02:51 -05001106/*
1107 * work queue call back to started compression on a file and pages
1108 */
1109static noinline void async_cow_start(struct btrfs_work *work)
1110{
1111 struct async_cow *async_cow;
1112 int num_added = 0;
1113 async_cow = container_of(work, struct async_cow, work);
1114
1115 compress_file_range(async_cow->inode, async_cow->locked_page,
1116 async_cow->start, async_cow->end, async_cow,
1117 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001118 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001119 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001120 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001121 }
Chris Mason771ed682008-11-06 22:02:51 -05001122}
1123
1124/*
1125 * work queue call back to submit previously compressed pages
1126 */
1127static noinline void async_cow_submit(struct btrfs_work *work)
1128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001129 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001130 struct async_cow *async_cow;
1131 struct btrfs_root *root;
1132 unsigned long nr_pages;
1133
1134 async_cow = container_of(work, struct async_cow, work);
1135
1136 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001137 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001138 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1139 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001140
David Sterbaee863952015-02-16 19:41:40 +01001141 /*
1142 * atomic_sub_return implies a barrier for waitqueue_active
1143 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001145 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001146 waitqueue_active(&fs_info->async_submit_wait))
1147 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001148
Chris Masond3977122009-01-05 21:25:51 -05001149 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001150 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001151}
Chris Masonc8b97812008-10-29 14:49:59 -04001152
Chris Mason771ed682008-11-06 22:02:51 -05001153static noinline void async_cow_free(struct btrfs_work *work)
1154{
1155 struct async_cow *async_cow;
1156 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001158 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001159 kfree(async_cow);
1160}
1161
1162static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1163 u64 start, u64 end, int *page_started,
1164 unsigned long *nr_written)
1165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001167 struct async_cow *async_cow;
1168 struct btrfs_root *root = BTRFS_I(inode)->root;
1169 unsigned long nr_pages;
1170 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001171
Chris Masona3429ab2009-10-08 12:30:20 -04001172 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1173 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001174 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001175 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001176 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001177 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001178 async_cow->root = root;
1179 async_cow->locked_page = locked_page;
1180 async_cow->start = start;
1181
Wang Shilongf79707b2014-07-17 11:44:09 +08001182 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001183 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001184 cur_end = end;
1185 else
Byongho Leeee221842015-12-15 01:42:10 +09001186 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001187
1188 async_cow->end = cur_end;
1189 INIT_LIST_HEAD(&async_cow->extents);
1190
Liu Bo9e0af232014-08-15 23:36:53 +08001191 btrfs_init_work(&async_cow->work,
1192 btrfs_delalloc_helper,
1193 async_cow_start, async_cow_submit,
1194 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001195
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001196 nr_pages = (cur_end - start + PAGE_SIZE) >>
1197 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001199
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001202 while (atomic_read(&fs_info->async_submit_draining) &&
1203 atomic_read(&fs_info->async_delalloc_pages)) {
1204 wait_event(fs_info->async_submit_wait,
1205 (atomic_read(&fs_info->async_delalloc_pages) ==
1206 0));
Chris Mason771ed682008-11-06 22:02:51 -05001207 }
1208
1209 *nr_written += nr_pages;
1210 start = cur_end + 1;
1211 }
1212 *page_started = 1;
1213 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214}
1215
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001216static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001217 u64 bytenr, u64 num_bytes)
1218{
1219 int ret;
1220 struct btrfs_ordered_sum *sums;
1221 LIST_HEAD(list);
1222
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001223 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001224 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 if (ret == 0 && list_empty(&list))
1226 return 0;
1227
1228 while (!list_empty(&list)) {
1229 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1230 list_del(&sums->list);
1231 kfree(sums);
1232 }
1233 return 1;
1234}
1235
Chris Masond352ac62008-09-29 15:18:18 -04001236/*
1237 * when nowcow writeback call back. This checks for snapshots or COW copies
1238 * of the extents that exist in the file, and COWs the file as required.
1239 *
1240 * If no cow copies or snapshots exist, we write directly to the existing
1241 * blocks on disk
1242 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001243static noinline int run_delalloc_nocow(struct inode *inode,
1244 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001245 u64 start, u64 end, int *page_started, int force,
1246 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001249 struct btrfs_root *root = BTRFS_I(inode)->root;
1250 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 u64 cow_start;
1255 u64 cur_offset;
1256 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 disk_bytenr;
1259 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001260 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001261 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001264 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int nocow;
1266 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001267 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001268 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001269
1270 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001271 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001272 extent_clear_unlock_delalloc(inode, start, end, end,
1273 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001274 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001275 EXTENT_DO_ACCOUNTING |
1276 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 PAGE_CLEAR_DIRTY |
1278 PAGE_SET_WRITEBACK |
1279 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001280 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001281 }
Li Zefan82d59022011-04-20 10:33:24 +08001282
Liu Bo83eea1f2012-07-10 05:28:39 -06001283 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 cow_start = (u64)-1;
1286 cur_offset = start;
1287 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001288 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001290 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key,
1295 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001296 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 found_key.type == BTRFS_EXTENT_DATA_KEY)
1298 path->slots[0]--;
1299 }
1300 check_prev = 0;
1301next_slot:
1302 leaf = path->nodes[0];
1303 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1304 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001305 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (ret > 0)
1308 break;
1309 leaf = path->nodes[0];
1310 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001311
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 nocow = 0;
1313 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001316
Filipe Manana1d512cb2015-11-09 00:33:58 +00001317 if (found_key.objectid > ino)
1318 break;
1319 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1320 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1321 path->slots[0]++;
1322 goto next_slot;
1323 }
1324 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.offset > end)
1326 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (found_key.offset > cur_offset) {
1329 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001330 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 goto out_check;
1332 }
Chris Masonc31f8832008-01-08 15:46:31 -05001333
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 fi = btrfs_item_ptr(leaf, path->slots[0],
1335 struct btrfs_file_extent_item);
1336 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
Josef Bacikcc95bef2013-04-04 14:31:27 -04001338 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001339 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1340 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 extent_end = found_key.offset +
1344 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001345 disk_num_bytes =
1346 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 if (extent_end <= start) {
1348 path->slots[0]++;
1349 goto next_slot;
1350 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001351 if (disk_bytenr == 0)
1352 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 if (btrfs_file_extent_compression(leaf, fi) ||
1354 btrfs_file_extent_encryption(leaf, fi) ||
1355 btrfs_file_extent_other_encoding(leaf, fi))
1356 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1358 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001359 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001361 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 found_key.offset -
1363 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001364 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001365 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001366 disk_bytenr += cur_offset - found_key.offset;
1367 num_bytes = min(end + 1, extent_end) - cur_offset;
1368 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001369 * if there are pending snapshots for this root,
1370 * we fall into common COW way.
1371 */
1372 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001373 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001374 if (!err)
1375 goto out_check;
1376 }
1377 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001378 * force cow if csum exists in the range.
1379 * this ensure that csum for a given extent are
1380 * either valid or do not exist.
1381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001382 if (csum_exist_in_range(fs_info, disk_bytenr,
1383 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001385 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001386 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 nocow = 1;
1388 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1389 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001390 btrfs_file_extent_inline_len(leaf,
1391 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001392 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001393 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 } else {
1395 BUG_ON(1);
1396 }
1397out_check:
1398 if (extent_end <= start) {
1399 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001401 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001402 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001403 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 goto next_slot;
1405 }
1406 if (!nocow) {
1407 if (cow_start == (u64)-1)
1408 cow_start = cur_offset;
1409 cur_offset = extent_end;
1410 if (cur_offset > end)
1411 break;
1412 path->slots[0]++;
1413 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001414 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001415
David Sterbab3b4aa72011-04-21 01:20:15 +02001416 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001418 ret = cow_file_range(inode, locked_page,
1419 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001420 end, page_started, nr_written, 1,
1421 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (ret) {
1423 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001424 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001425 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001427 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001431 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001432
Yan Zhengd899e052008-10-30 14:25:28 -04001433 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1434 struct extent_map *em;
1435 struct extent_map_tree *em_tree;
1436 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001437 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001438 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001439 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001440 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001441 em->len = num_bytes;
1442 em->block_len = num_bytes;
1443 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001444 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001445 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001447 em->mod_start = em->start;
1448 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001449 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001450 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001451 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001452 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001453 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001454 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001455 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001456 if (ret != -EEXIST) {
1457 free_extent_map(em);
1458 break;
1459 }
1460 btrfs_drop_extent_cache(inode, em->start,
1461 em->start + em->len - 1, 0);
1462 }
1463 type = BTRFS_ORDERED_PREALLOC;
1464 } else {
1465 type = BTRFS_ORDERED_NOCOW;
1466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
1468 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001469 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001470 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001473
Yan, Zhengefa56462010-05-16 10:49:59 -04001474 if (root->root_key.objectid ==
1475 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1476 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1477 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001478 if (ret) {
1479 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001480 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001481 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001482 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001483 }
1484
Josef Bacikc2790a22013-07-29 11:20:47 -04001485 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001486 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001487 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001488 EXTENT_DELALLOC |
1489 EXTENT_CLEAR_DATA_RESV,
1490 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1491
Wang Shilonge9894fd2014-03-27 11:12:25 +08001492 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001493 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 cur_offset = extent_end;
1495 if (cur_offset > end)
1496 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001497 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001498 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001499
Josef Bacik17ca04a2012-05-31 15:58:55 -04001500 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001502 cur_offset = end;
1503 }
1504
Yan Zheng80ff3852008-10-30 14:20:02 -04001505 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001506 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1507 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001508 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001509 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001510 }
1511
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001512error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001513 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001514 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001516 EXTENT_DELALLOC | EXTENT_DEFRAG |
1517 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1518 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001519 PAGE_SET_WRITEBACK |
1520 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001522 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001523}
1524
Wang Shilong47059d92014-07-03 18:22:07 +08001525static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1526{
1527
1528 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1529 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1530 return 0;
1531
1532 /*
1533 * @defrag_bytes is a hint value, no spinlock held here,
1534 * if is not zero, it means the file is defragging.
1535 * Force cow if given extent needs to be defragged.
1536 */
1537 if (BTRFS_I(inode)->defrag_bytes &&
1538 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1539 EXTENT_DEFRAG, 0, NULL))
1540 return 1;
1541
1542 return 0;
1543}
1544
Chris Masond352ac62008-09-29 15:18:18 -04001545/*
1546 * extent_io.c call back to do delayed allocation processing
1547 */
Chris Masonc8b97812008-10-29 14:49:59 -04001548static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001549 u64 start, u64 end, int *page_started,
1550 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001551{
Chris Masonbe20aa92007-12-17 20:14:01 -05001552 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001553 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001554
Wang Shilong47059d92014-07-03 18:22:07 +08001555 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001556 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001557 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001558 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001559 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001560 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001561 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001562 ret = cow_file_range(inode, locked_page, start, end, end,
1563 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001564 } else {
1565 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1566 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001567 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001568 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001569 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001570 return ret;
1571}
1572
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001573static void btrfs_split_extent_hook(struct inode *inode,
1574 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001575{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001576 u64 size;
1577
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001578 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001579 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 size = orig->end - orig->start + 1;
1583 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001584 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001585 u64 new_size;
1586
1587 /*
Josef Bacikba117212015-03-13 15:01:24 -04001588 * See the explanation in btrfs_merge_extent_hook, the same
1589 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001590 */
1591 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001592 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001593 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001594 num_extents += count_max_extents(new_size);
1595 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001596 return;
1597 }
1598
Josef Bacik9e0baf62011-07-15 15:16:44 +00001599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents++;
1601 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602}
1603
1604/*
1605 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1606 * extents so we can keep track of new extents that are just merged onto old
1607 * extents, such as when we are doing sequential writes, so we can properly
1608 * account for the metadata space we'll need.
1609 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001610static void btrfs_merge_extent_hook(struct inode *inode,
1611 struct extent_state *new,
1612 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001613{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001615 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617 /* not delalloc, ignore it */
1618 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620
Josef Bacik8461a3d2015-03-13 15:12:08 -04001621 if (new->start > other->start)
1622 new_size = new->end - other->start + 1;
1623 else
1624 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001625
1626 /* we're not bigger than the max, unreserve the space and go */
1627 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1628 spin_lock(&BTRFS_I(inode)->lock);
1629 BTRFS_I(inode)->outstanding_extents--;
1630 spin_unlock(&BTRFS_I(inode)->lock);
1631 return;
1632 }
1633
1634 /*
Josef Bacikba117212015-03-13 15:01:24 -04001635 * We have to add up either side to figure out how many extents were
1636 * accounted for before we merged into one big extent. If the number of
1637 * extents we accounted for is <= the amount we need for the new range
1638 * then we can return, otherwise drop. Think of it like this
1639 *
1640 * [ 4k][MAX_SIZE]
1641 *
1642 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1643 * need 2 outstanding extents, on one side we have 1 and the other side
1644 * we have 1 so they are == and we can return. But in this case
1645 *
1646 * [MAX_SIZE+4k][MAX_SIZE+4k]
1647 *
1648 * Each range on their own accounts for 2 extents, but merged together
1649 * they are only 3 extents worth of accounting, so we need to drop in
1650 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 */
Josef Bacikba117212015-03-13 15:01:24 -04001652 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001653 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001654 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001655 num_extents += count_max_extents(old_size);
1656 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657 return;
1658
Josef Bacik9e0baf62011-07-15 15:16:44 +00001659 spin_lock(&BTRFS_I(inode)->lock);
1660 BTRFS_I(inode)->outstanding_extents--;
1661 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662}
1663
Miao Xieeb73c1b2013-05-15 07:48:22 +00001664static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1665 struct inode *inode)
1666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001667 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1668
Miao Xieeb73c1b2013-05-15 07:48:22 +00001669 spin_lock(&root->delalloc_lock);
1670 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1671 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1672 &root->delalloc_inodes);
1673 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1674 &BTRFS_I(inode)->runtime_flags);
1675 root->nr_delalloc_inodes++;
1676 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001677 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001678 BUG_ON(!list_empty(&root->delalloc_root));
1679 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001680 &fs_info->delalloc_roots);
1681 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001682 }
1683 }
1684 spin_unlock(&root->delalloc_lock);
1685}
1686
1687static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1688 struct inode *inode)
1689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1691
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692 spin_lock(&root->delalloc_lock);
1693 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1694 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1695 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1696 &BTRFS_I(inode)->runtime_flags);
1697 root->nr_delalloc_inodes--;
1698 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001699 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001700 BUG_ON(list_empty(&root->delalloc_root));
1701 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703 }
1704 }
1705 spin_unlock(&root->delalloc_lock);
1706}
1707
Chris Masond352ac62008-09-29 15:18:18 -04001708/*
1709 * extent_io.c set_bit_hook, used to track delayed allocation
1710 * bytes in this file, and to maintain the list of inodes that
1711 * have pending delalloc work to be done.
1712 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001713static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001714 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001715{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1718
Wang Shilong47059d92014-07-03 18:22:07 +08001719 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1720 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001721 /*
1722 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001723 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001724 * bit, which is only set or cleared with irqs on
1725 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001727 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001728 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001729 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001730
Josef Bacik9e0baf62011-07-15 15:16:44 +00001731 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001732 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001733 } else {
1734 spin_lock(&BTRFS_I(inode)->lock);
1735 BTRFS_I(inode)->outstanding_extents++;
1736 spin_unlock(&BTRFS_I(inode)->lock);
1737 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001738
Josef Bacik6a3891c2015-03-16 17:38:52 -04001739 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001741 return;
1742
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1744 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001745 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001746 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001747 if (*bits & EXTENT_DEFRAG)
1748 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001749 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750 &BTRFS_I(inode)->runtime_flags))
1751 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001752 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001753 }
Chris Mason291d6732008-01-29 15:55:23 -05001754}
1755
Chris Masond352ac62008-09-29 15:18:18 -04001756/*
1757 * extent_io.c clear_bit_hook, see set_bit_hook for why
1758 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001759static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001760 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001761 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001764 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001765 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001766
1767 spin_lock(&BTRFS_I(inode)->lock);
1768 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1769 BTRFS_I(inode)->defrag_bytes -= len;
1770 spin_unlock(&BTRFS_I(inode)->lock);
1771
Chris Mason75eff682008-12-15 15:54:40 -05001772 /*
1773 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001774 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001775 * bit, which is only set or cleared with irqs on
1776 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001778 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001779 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001780
Josef Bacik9e0baf62011-07-15 15:16:44 +00001781 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001782 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001783 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1784 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001785 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001786 spin_unlock(&BTRFS_I(inode)->lock);
1787 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001788
Josef Bacikb6d08f02013-09-27 14:57:43 -04001789 /*
1790 * We don't reserve metadata space for space cache inodes so we
1791 * don't need to call dellalloc_release_metadata if there is an
1792 * error.
1793 */
1794 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001795 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001796 btrfs_delalloc_release_metadata(inode, len);
1797
Josef Bacik6a3891c2015-03-16 17:38:52 -04001798 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001799 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001800 return;
1801
Josef Bacik0cb59c92010-07-02 12:14:14 -04001802 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001803 && do_list && !(state->state & EXTENT_NORESERVE)
1804 && (*bits & (EXTENT_DO_ACCOUNTING |
1805 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001806 btrfs_free_reserved_data_space_noquota(inode,
1807 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001808
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001809 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1810 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001811 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001812 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001813 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001814 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001815 &BTRFS_I(inode)->runtime_flags))
1816 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001818 }
Chris Mason291d6732008-01-29 15:55:23 -05001819}
1820
Chris Masond352ac62008-09-29 15:18:18 -04001821/*
1822 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1823 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001824 *
1825 * return 1 if page cannot be merged to bio
1826 * return 0 if page can be merged to bio
1827 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001828 */
Mike Christie81a75f672016-06-05 14:31:54 -05001829int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001830 size_t size, struct bio *bio,
1831 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001832{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001833 struct inode *inode = page->mapping->host;
1834 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001835 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001836 u64 length = 0;
1837 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001838 int ret;
1839
Chris Mason771ed682008-11-06 22:02:51 -05001840 if (bio_flags & EXTENT_BIO_COMPRESSED)
1841 return 0;
1842
Kent Overstreet4f024f32013-10-11 15:44:27 -07001843 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001844 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1846 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001847 if (ret < 0)
1848 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001849 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001850 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001851 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
1855 * in order to insert checksums into the metadata in large chunks,
1856 * we wait until bio submission time. All the pages in the bio are
1857 * checksummed and sums are attached onto the ordered extent record.
1858 *
1859 * At IO completion time the cums attached on the ordered extent record
1860 * are inserted into the btree
1861 */
Mike Christie81a75f672016-06-05 14:31:54 -05001862static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1863 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001864 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001865{
Chris Mason065631f2008-02-20 12:07:25 -05001866 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001867
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001868 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001869 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001870 return 0;
1871}
Chris Masone0156402008-04-16 11:15:20 -04001872
Chris Mason4a69a412008-11-06 22:03:00 -05001873/*
1874 * in order to insert checksums into the metadata in large chunks,
1875 * we wait until bio submission time. All the pages in the bio are
1876 * checksummed and sums are attached onto the ordered extent record.
1877 *
1878 * At IO completion time the cums attached on the ordered extent record
1879 * are inserted into the btree
1880 */
Mike Christie81a75f672016-06-05 14:31:54 -05001881static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001882 int mirror_num, unsigned long bio_flags,
1883 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001884{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001886 int ret;
1887
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001888 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001889 if (ret) {
1890 bio->bi_error = ret;
1891 bio_endio(bio);
1892 }
Stefan Behrens61891922012-11-05 18:51:52 +01001893 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001894}
1895
Chris Masond352ac62008-09-29 15:18:18 -04001896/*
Chris Masoncad321a2008-12-17 14:51:42 -05001897 * extent_io.c submission hook. This does the right thing for csum calculation
1898 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001899 */
Mike Christie81a75f672016-06-05 14:31:54 -05001900static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001901 int mirror_num, unsigned long bio_flags,
1902 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001904 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001905 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301906 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001907 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001908 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001909 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001910
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001911 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001912
Liu Bo83eea1f2012-07-10 05:28:39 -06001913 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301914 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001915
Mike Christie37226b22016-06-05 14:31:52 -05001916 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001917 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001918 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001919 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001920
Chris Masond20f7042008-12-08 16:58:54 -05001921 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001922 ret = btrfs_submit_compressed_read(inode, bio,
1923 mirror_num,
1924 bio_flags);
1925 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001926 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001928 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001929 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001930 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001931 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001932 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001933 /* csum items have already been cloned */
1934 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1935 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001936 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1938 bio_flags, bio_offset,
1939 __btrfs_submit_bio_start,
1940 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001941 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001942 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001943 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001944 if (ret)
1945 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001946 }
1947
Chris Mason0b86a832008-03-24 15:01:56 -04001948mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001949 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001950
1951out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001952 if (ret < 0) {
1953 bio->bi_error = ret;
1954 bio_endio(bio);
1955 }
Stefan Behrens61891922012-11-05 18:51:52 +01001956 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001957}
Chris Mason6885f302008-02-20 16:11:05 -05001958
Chris Masond352ac62008-09-29 15:18:18 -04001959/*
1960 * given a list of ordered sums record them in the inode. This happens
1961 * at IO completion time based on sums calculated at bio submission time.
1962 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001963static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001964 struct inode *inode, u64 file_offset,
1965 struct list_head *list)
1966{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001967 struct btrfs_ordered_sum *sum;
1968
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001969 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001970 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001971 btrfs_csum_file_blocks(trans,
1972 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001973 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001974 }
1975 return 0;
1976}
1977
Josef Bacik2ac55d42010-02-03 19:33:23 +00001978int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001979 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001980{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001982 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001983 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001984}
1985
Chris Masond352ac62008-09-29 15:18:18 -04001986/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001987struct btrfs_writepage_fixup {
1988 struct page *page;
1989 struct btrfs_work work;
1990};
1991
Christoph Hellwigb2950862008-12-02 09:54:17 -05001992static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001993{
1994 struct btrfs_writepage_fixup *fixup;
1995 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001996 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001997 struct page *page;
1998 struct inode *inode;
1999 u64 page_start;
2000 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002001 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002002
2003 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2004 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002005again:
Chris Mason247e7432008-07-17 12:53:51 -04002006 lock_page(page);
2007 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2008 ClearPageChecked(page);
2009 goto out_page;
2010 }
2011
2012 inode = page->mapping->host;
2013 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002014 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002015
David Sterbaff13db42015-12-03 14:30:40 +01002016 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002017 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002018
2019 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002020 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002021 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002022
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302023 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002024 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002025 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002026 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2027 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002028 unlock_page(page);
2029 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002030 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002031 goto again;
2032 }
Chris Mason247e7432008-07-17 12:53:51 -04002033
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002034 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002035 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002036 if (ret) {
2037 mapping_set_error(page->mapping, ret);
2038 end_extent_writepage(page, ret, page_start, page_end);
2039 ClearPageChecked(page);
2040 goto out;
2041 }
2042
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002043 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2044 0);
Chris Mason247e7432008-07-17 12:53:51 -04002045 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002046 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002047out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2049 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002050out_page:
2051 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002053 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002054}
2055
2056/*
2057 * There are a few paths in the higher layers of the kernel that directly
2058 * set the page dirty bit without asking the filesystem if it is a
2059 * good idea. This causes problems because we want to make sure COW
2060 * properly happens and the data=ordered rules are followed.
2061 *
Chris Masonc8b97812008-10-29 14:49:59 -04002062 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002063 * hasn't been properly setup for IO. We kick off an async process
2064 * to fix it up. The async helper will wait for ordered extents, set
2065 * the delalloc bit and make it safe to write the page.
2066 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002067static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002068{
2069 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002070 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002071 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002072
Chris Mason8b62b722009-09-02 16:53:46 -04002073 /* this page is properly in the ordered list */
2074 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002075 return 0;
2076
2077 if (PageChecked(page))
2078 return -EAGAIN;
2079
2080 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2081 if (!fixup)
2082 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002083
Chris Mason247e7432008-07-17 12:53:51 -04002084 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002086 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2087 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002088 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002089 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002090 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002091}
2092
Yan Zhengd899e052008-10-30 14:25:28 -04002093static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2094 struct inode *inode, u64 file_pos,
2095 u64 disk_bytenr, u64 disk_num_bytes,
2096 u64 num_bytes, u64 ram_bytes,
2097 u8 compression, u8 encryption,
2098 u16 other_encoding, int extent_type)
2099{
2100 struct btrfs_root *root = BTRFS_I(inode)->root;
2101 struct btrfs_file_extent_item *fi;
2102 struct btrfs_path *path;
2103 struct extent_buffer *leaf;
2104 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002105 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002106 int ret;
2107
2108 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002109 if (!path)
2110 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002111
Chris Masona1ed8352009-09-11 12:27:37 -04002112 /*
2113 * we may be replacing one extent in the tree with another.
2114 * The new extent is pinned in the extent map, and we don't want
2115 * to drop it from the cache until it is completely in the btree.
2116 *
2117 * So, tell btrfs_drop_extents to leave this extent in the cache.
2118 * the caller is expected to unpin it and allow it to be merged
2119 * with the others.
2120 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002121 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2122 file_pos + num_bytes, NULL, 0,
2123 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002124 if (ret)
2125 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002126
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002127 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002128 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002129 ins.offset = file_pos;
2130 ins.type = BTRFS_EXTENT_DATA_KEY;
2131
2132 path->leave_spinning = 1;
2133 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2134 sizeof(*fi));
2135 if (ret)
2136 goto out;
2137 }
Yan Zhengd899e052008-10-30 14:25:28 -04002138 leaf = path->nodes[0];
2139 fi = btrfs_item_ptr(leaf, path->slots[0],
2140 struct btrfs_file_extent_item);
2141 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2142 btrfs_set_file_extent_type(leaf, fi, extent_type);
2143 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2144 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2145 btrfs_set_file_extent_offset(leaf, fi, 0);
2146 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2147 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2148 btrfs_set_file_extent_compression(leaf, fi, compression);
2149 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2150 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002151
Yan Zhengd899e052008-10-30 14:25:28 -04002152 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002153 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002154
2155 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002156
2157 ins.objectid = disk_bytenr;
2158 ins.offset = disk_num_bytes;
2159 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002160 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002161 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002162 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002163 * Release the reserved range from inode dirty range map, as it is
2164 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002165 */
2166 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002167out:
Yan Zhengd899e052008-10-30 14:25:28 -04002168 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002169
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002170 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002171}
2172
Liu Bo38c227d2013-01-29 03:18:40 +00002173/* snapshot-aware defrag */
2174struct sa_defrag_extent_backref {
2175 struct rb_node node;
2176 struct old_sa_defrag_extent *old;
2177 u64 root_id;
2178 u64 inum;
2179 u64 file_pos;
2180 u64 extent_offset;
2181 u64 num_bytes;
2182 u64 generation;
2183};
2184
2185struct old_sa_defrag_extent {
2186 struct list_head list;
2187 struct new_sa_defrag_extent *new;
2188
2189 u64 extent_offset;
2190 u64 bytenr;
2191 u64 offset;
2192 u64 len;
2193 int count;
2194};
2195
2196struct new_sa_defrag_extent {
2197 struct rb_root root;
2198 struct list_head head;
2199 struct btrfs_path *path;
2200 struct inode *inode;
2201 u64 file_pos;
2202 u64 len;
2203 u64 bytenr;
2204 u64 disk_len;
2205 u8 compress_type;
2206};
2207
2208static int backref_comp(struct sa_defrag_extent_backref *b1,
2209 struct sa_defrag_extent_backref *b2)
2210{
2211 if (b1->root_id < b2->root_id)
2212 return -1;
2213 else if (b1->root_id > b2->root_id)
2214 return 1;
2215
2216 if (b1->inum < b2->inum)
2217 return -1;
2218 else if (b1->inum > b2->inum)
2219 return 1;
2220
2221 if (b1->file_pos < b2->file_pos)
2222 return -1;
2223 else if (b1->file_pos > b2->file_pos)
2224 return 1;
2225
2226 /*
2227 * [------------------------------] ===> (a range of space)
2228 * |<--->| |<---->| =============> (fs/file tree A)
2229 * |<---------------------------->| ===> (fs/file tree B)
2230 *
2231 * A range of space can refer to two file extents in one tree while
2232 * refer to only one file extent in another tree.
2233 *
2234 * So we may process a disk offset more than one time(two extents in A)
2235 * and locate at the same extent(one extent in B), then insert two same
2236 * backrefs(both refer to the extent in B).
2237 */
2238 return 0;
2239}
2240
2241static void backref_insert(struct rb_root *root,
2242 struct sa_defrag_extent_backref *backref)
2243{
2244 struct rb_node **p = &root->rb_node;
2245 struct rb_node *parent = NULL;
2246 struct sa_defrag_extent_backref *entry;
2247 int ret;
2248
2249 while (*p) {
2250 parent = *p;
2251 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2252
2253 ret = backref_comp(backref, entry);
2254 if (ret < 0)
2255 p = &(*p)->rb_left;
2256 else
2257 p = &(*p)->rb_right;
2258 }
2259
2260 rb_link_node(&backref->node, parent, p);
2261 rb_insert_color(&backref->node, root);
2262}
2263
2264/*
2265 * Note the backref might has changed, and in this case we just return 0.
2266 */
2267static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2268 void *ctx)
2269{
2270 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002271 struct old_sa_defrag_extent *old = ctx;
2272 struct new_sa_defrag_extent *new = old->new;
2273 struct btrfs_path *path = new->path;
2274 struct btrfs_key key;
2275 struct btrfs_root *root;
2276 struct sa_defrag_extent_backref *backref;
2277 struct extent_buffer *leaf;
2278 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002280 int slot;
2281 int ret;
2282 u64 extent_offset;
2283 u64 num_bytes;
2284
2285 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002286 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002287 return 0;
2288
2289 key.objectid = root_id;
2290 key.type = BTRFS_ROOT_ITEM_KEY;
2291 key.offset = (u64)-1;
2292
Liu Bo38c227d2013-01-29 03:18:40 +00002293 root = btrfs_read_fs_root_no_name(fs_info, &key);
2294 if (IS_ERR(root)) {
2295 if (PTR_ERR(root) == -ENOENT)
2296 return 0;
2297 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002298 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002299 inum, offset, root_id);
2300 return PTR_ERR(root);
2301 }
2302
2303 key.objectid = inum;
2304 key.type = BTRFS_EXTENT_DATA_KEY;
2305 if (offset > (u64)-1 << 32)
2306 key.offset = 0;
2307 else
2308 key.offset = offset;
2309
2310 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302311 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002312 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002313 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002314
2315 while (1) {
2316 cond_resched();
2317
2318 leaf = path->nodes[0];
2319 slot = path->slots[0];
2320
2321 if (slot >= btrfs_header_nritems(leaf)) {
2322 ret = btrfs_next_leaf(root, path);
2323 if (ret < 0) {
2324 goto out;
2325 } else if (ret > 0) {
2326 ret = 0;
2327 goto out;
2328 }
2329 continue;
2330 }
2331
2332 path->slots[0]++;
2333
2334 btrfs_item_key_to_cpu(leaf, &key, slot);
2335
2336 if (key.objectid > inum)
2337 goto out;
2338
2339 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2340 continue;
2341
2342 extent = btrfs_item_ptr(leaf, slot,
2343 struct btrfs_file_extent_item);
2344
2345 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2346 continue;
2347
Liu Boe68afa42013-07-01 22:13:26 +08002348 /*
2349 * 'offset' refers to the exact key.offset,
2350 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2351 * (key.offset - extent_offset).
2352 */
2353 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002354 continue;
2355
Liu Boe68afa42013-07-01 22:13:26 +08002356 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002357 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002358
Liu Bo38c227d2013-01-29 03:18:40 +00002359 if (extent_offset >= old->extent_offset + old->offset +
2360 old->len || extent_offset + num_bytes <=
2361 old->extent_offset + old->offset)
2362 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002363 break;
2364 }
2365
2366 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2367 if (!backref) {
2368 ret = -ENOENT;
2369 goto out;
2370 }
2371
2372 backref->root_id = root_id;
2373 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002374 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002375 backref->num_bytes = num_bytes;
2376 backref->extent_offset = extent_offset;
2377 backref->generation = btrfs_file_extent_generation(leaf, extent);
2378 backref->old = old;
2379 backref_insert(&new->root, backref);
2380 old->count++;
2381out:
2382 btrfs_release_path(path);
2383 WARN_ON(ret);
2384 return ret;
2385}
2386
2387static noinline bool record_extent_backrefs(struct btrfs_path *path,
2388 struct new_sa_defrag_extent *new)
2389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002391 struct old_sa_defrag_extent *old, *tmp;
2392 int ret;
2393
2394 new->path = path;
2395
2396 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002397 ret = iterate_inodes_from_logical(old->bytenr +
2398 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002399 path, record_one_backref,
2400 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002401 if (ret < 0 && ret != -ENOENT)
2402 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002403
2404 /* no backref to be processed for this extent */
2405 if (!old->count) {
2406 list_del(&old->list);
2407 kfree(old);
2408 }
2409 }
2410
2411 if (list_empty(&new->head))
2412 return false;
2413
2414 return true;
2415}
2416
2417static int relink_is_mergable(struct extent_buffer *leaf,
2418 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002419 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002420{
Liu Bo116e0022013-08-02 16:30:40 +08002421 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002422 return 0;
2423
2424 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2425 return 0;
2426
Liu Bo116e0022013-08-02 16:30:40 +08002427 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2428 return 0;
2429
2430 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002431 btrfs_file_extent_other_encoding(leaf, fi))
2432 return 0;
2433
2434 return 1;
2435}
2436
2437/*
2438 * Note the backref might has changed, and in this case we just return 0.
2439 */
2440static noinline int relink_extent_backref(struct btrfs_path *path,
2441 struct sa_defrag_extent_backref *prev,
2442 struct sa_defrag_extent_backref *backref)
2443{
2444 struct btrfs_file_extent_item *extent;
2445 struct btrfs_file_extent_item *item;
2446 struct btrfs_ordered_extent *ordered;
2447 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002448 struct btrfs_root *root;
2449 struct btrfs_key key;
2450 struct extent_buffer *leaf;
2451 struct old_sa_defrag_extent *old = backref->old;
2452 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002453 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002454 struct inode *inode;
2455 struct extent_state *cached = NULL;
2456 int ret = 0;
2457 u64 start;
2458 u64 len;
2459 u64 lock_start;
2460 u64 lock_end;
2461 bool merge = false;
2462 int index;
2463
2464 if (prev && prev->root_id == backref->root_id &&
2465 prev->inum == backref->inum &&
2466 prev->file_pos + prev->num_bytes == backref->file_pos)
2467 merge = true;
2468
2469 /* step 1: get root */
2470 key.objectid = backref->root_id;
2471 key.type = BTRFS_ROOT_ITEM_KEY;
2472 key.offset = (u64)-1;
2473
Liu Bo38c227d2013-01-29 03:18:40 +00002474 index = srcu_read_lock(&fs_info->subvol_srcu);
2475
2476 root = btrfs_read_fs_root_no_name(fs_info, &key);
2477 if (IS_ERR(root)) {
2478 srcu_read_unlock(&fs_info->subvol_srcu, index);
2479 if (PTR_ERR(root) == -ENOENT)
2480 return 0;
2481 return PTR_ERR(root);
2482 }
Liu Bo38c227d2013-01-29 03:18:40 +00002483
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002484 if (btrfs_root_readonly(root)) {
2485 srcu_read_unlock(&fs_info->subvol_srcu, index);
2486 return 0;
2487 }
2488
Liu Bo38c227d2013-01-29 03:18:40 +00002489 /* step 2: get inode */
2490 key.objectid = backref->inum;
2491 key.type = BTRFS_INODE_ITEM_KEY;
2492 key.offset = 0;
2493
2494 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2495 if (IS_ERR(inode)) {
2496 srcu_read_unlock(&fs_info->subvol_srcu, index);
2497 return 0;
2498 }
2499
2500 srcu_read_unlock(&fs_info->subvol_srcu, index);
2501
2502 /* step 3: relink backref */
2503 lock_start = backref->file_pos;
2504 lock_end = backref->file_pos + backref->num_bytes - 1;
2505 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002506 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002507
2508 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2509 if (ordered) {
2510 btrfs_put_ordered_extent(ordered);
2511 goto out_unlock;
2512 }
2513
2514 trans = btrfs_join_transaction(root);
2515 if (IS_ERR(trans)) {
2516 ret = PTR_ERR(trans);
2517 goto out_unlock;
2518 }
2519
2520 key.objectid = backref->inum;
2521 key.type = BTRFS_EXTENT_DATA_KEY;
2522 key.offset = backref->file_pos;
2523
2524 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2525 if (ret < 0) {
2526 goto out_free_path;
2527 } else if (ret > 0) {
2528 ret = 0;
2529 goto out_free_path;
2530 }
2531
2532 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2533 struct btrfs_file_extent_item);
2534
2535 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2536 backref->generation)
2537 goto out_free_path;
2538
2539 btrfs_release_path(path);
2540
2541 start = backref->file_pos;
2542 if (backref->extent_offset < old->extent_offset + old->offset)
2543 start += old->extent_offset + old->offset -
2544 backref->extent_offset;
2545
2546 len = min(backref->extent_offset + backref->num_bytes,
2547 old->extent_offset + old->offset + old->len);
2548 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2549
2550 ret = btrfs_drop_extents(trans, root, inode, start,
2551 start + len, 1);
2552 if (ret)
2553 goto out_free_path;
2554again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002555 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002556 key.type = BTRFS_EXTENT_DATA_KEY;
2557 key.offset = start;
2558
Liu Boa09a0a72013-03-11 09:20:58 +00002559 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 if (merge) {
2561 struct btrfs_file_extent_item *fi;
2562 u64 extent_len;
2563 struct btrfs_key found_key;
2564
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002565 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 if (ret < 0)
2567 goto out_free_path;
2568
2569 path->slots[0]--;
2570 leaf = path->nodes[0];
2571 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2572
2573 fi = btrfs_item_ptr(leaf, path->slots[0],
2574 struct btrfs_file_extent_item);
2575 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2576
Liu Bo116e0022013-08-02 16:30:40 +08002577 if (extent_len + found_key.offset == start &&
2578 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002579 btrfs_set_file_extent_num_bytes(leaf, fi,
2580 extent_len + len);
2581 btrfs_mark_buffer_dirty(leaf);
2582 inode_add_bytes(inode, len);
2583
2584 ret = 1;
2585 goto out_free_path;
2586 } else {
2587 merge = false;
2588 btrfs_release_path(path);
2589 goto again;
2590 }
2591 }
2592
2593 ret = btrfs_insert_empty_item(trans, root, path, &key,
2594 sizeof(*extent));
2595 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002596 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002597 goto out_free_path;
2598 }
2599
2600 leaf = path->nodes[0];
2601 item = btrfs_item_ptr(leaf, path->slots[0],
2602 struct btrfs_file_extent_item);
2603 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2604 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2605 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2606 btrfs_set_file_extent_num_bytes(leaf, item, len);
2607 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2608 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2609 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2610 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2611 btrfs_set_file_extent_encryption(leaf, item, 0);
2612 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2613
2614 btrfs_mark_buffer_dirty(leaf);
2615 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002616 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002617
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002618 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002619 new->disk_len, 0,
2620 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002621 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002622 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002623 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002624 goto out_free_path;
2625 }
2626
2627 ret = 1;
2628out_free_path:
2629 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002630 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002631 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002632out_unlock:
2633 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2634 &cached, GFP_NOFS);
2635 iput(inode);
2636 return ret;
2637}
2638
Liu Bo6f519562013-10-29 10:45:05 +08002639static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2640{
2641 struct old_sa_defrag_extent *old, *tmp;
2642
2643 if (!new)
2644 return;
2645
2646 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002647 kfree(old);
2648 }
2649 kfree(new);
2650}
2651
Liu Bo38c227d2013-01-29 03:18:40 +00002652static void relink_file_extents(struct new_sa_defrag_extent *new)
2653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002654 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002655 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002656 struct sa_defrag_extent_backref *backref;
2657 struct sa_defrag_extent_backref *prev = NULL;
2658 struct inode *inode;
2659 struct btrfs_root *root;
2660 struct rb_node *node;
2661 int ret;
2662
2663 inode = new->inode;
2664 root = BTRFS_I(inode)->root;
2665
2666 path = btrfs_alloc_path();
2667 if (!path)
2668 return;
2669
2670 if (!record_extent_backrefs(path, new)) {
2671 btrfs_free_path(path);
2672 goto out;
2673 }
2674 btrfs_release_path(path);
2675
2676 while (1) {
2677 node = rb_first(&new->root);
2678 if (!node)
2679 break;
2680 rb_erase(node, &new->root);
2681
2682 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2683
2684 ret = relink_extent_backref(path, prev, backref);
2685 WARN_ON(ret < 0);
2686
2687 kfree(prev);
2688
2689 if (ret == 1)
2690 prev = backref;
2691 else
2692 prev = NULL;
2693 cond_resched();
2694 }
2695 kfree(prev);
2696
2697 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002698out:
Liu Bo6f519562013-10-29 10:45:05 +08002699 free_sa_defrag_extent(new);
2700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002701 atomic_dec(&fs_info->defrag_running);
2702 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002703}
2704
2705static struct new_sa_defrag_extent *
2706record_old_file_extents(struct inode *inode,
2707 struct btrfs_ordered_extent *ordered)
2708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct btrfs_root *root = BTRFS_I(inode)->root;
2711 struct btrfs_path *path;
2712 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002713 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002714 struct new_sa_defrag_extent *new;
2715 int ret;
2716
2717 new = kmalloc(sizeof(*new), GFP_NOFS);
2718 if (!new)
2719 return NULL;
2720
2721 new->inode = inode;
2722 new->file_pos = ordered->file_offset;
2723 new->len = ordered->len;
2724 new->bytenr = ordered->start;
2725 new->disk_len = ordered->disk_len;
2726 new->compress_type = ordered->compress_type;
2727 new->root = RB_ROOT;
2728 INIT_LIST_HEAD(&new->head);
2729
2730 path = btrfs_alloc_path();
2731 if (!path)
2732 goto out_kfree;
2733
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002734 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002735 key.type = BTRFS_EXTENT_DATA_KEY;
2736 key.offset = new->file_pos;
2737
2738 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2739 if (ret < 0)
2740 goto out_free_path;
2741 if (ret > 0 && path->slots[0] > 0)
2742 path->slots[0]--;
2743
2744 /* find out all the old extents for the file range */
2745 while (1) {
2746 struct btrfs_file_extent_item *extent;
2747 struct extent_buffer *l;
2748 int slot;
2749 u64 num_bytes;
2750 u64 offset;
2751 u64 end;
2752 u64 disk_bytenr;
2753 u64 extent_offset;
2754
2755 l = path->nodes[0];
2756 slot = path->slots[0];
2757
2758 if (slot >= btrfs_header_nritems(l)) {
2759 ret = btrfs_next_leaf(root, path);
2760 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002761 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002762 else if (ret > 0)
2763 break;
2764 continue;
2765 }
2766
2767 btrfs_item_key_to_cpu(l, &key, slot);
2768
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002769 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002770 break;
2771 if (key.type != BTRFS_EXTENT_DATA_KEY)
2772 break;
2773 if (key.offset >= new->file_pos + new->len)
2774 break;
2775
2776 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2777
2778 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2779 if (key.offset + num_bytes < new->file_pos)
2780 goto next;
2781
2782 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2783 if (!disk_bytenr)
2784 goto next;
2785
2786 extent_offset = btrfs_file_extent_offset(l, extent);
2787
2788 old = kmalloc(sizeof(*old), GFP_NOFS);
2789 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002790 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002791
2792 offset = max(new->file_pos, key.offset);
2793 end = min(new->file_pos + new->len, key.offset + num_bytes);
2794
2795 old->bytenr = disk_bytenr;
2796 old->extent_offset = extent_offset;
2797 old->offset = offset - key.offset;
2798 old->len = end - offset;
2799 old->new = new;
2800 old->count = 0;
2801 list_add_tail(&old->list, &new->head);
2802next:
2803 path->slots[0]++;
2804 cond_resched();
2805 }
2806
2807 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002809
2810 return new;
2811
Liu Bo38c227d2013-01-29 03:18:40 +00002812out_free_path:
2813 btrfs_free_path(path);
2814out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002815 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002816 return NULL;
2817}
2818
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002819static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002820 u64 start, u64 len)
2821{
2822 struct btrfs_block_group_cache *cache;
2823
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002825 ASSERT(cache);
2826
2827 spin_lock(&cache->lock);
2828 cache->delalloc_bytes -= len;
2829 spin_unlock(&cache->lock);
2830
2831 btrfs_put_block_group(cache);
2832}
2833
Chris Masond352ac62008-09-29 15:18:18 -04002834/* as ordered data IO finishes, this gets called so we can finish
2835 * an ordered extent if the range of bytes in the file it covers are
2836 * fully written.
2837 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002838static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002839{
Josef Bacik5fd02042012-05-02 14:00:54 -04002840 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002842 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002843 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002845 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002846 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002847 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002848 int ret = 0;
2849 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002850 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002851 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002852
Liu Bo83eea1f2012-07-10 05:28:39 -06002853 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002854
Josef Bacik5fd02042012-05-02 14:00:54 -04002855 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2856 ret = -EIO;
2857 goto out;
2858 }
2859
Miao Xief6124962014-09-12 18:44:04 +08002860 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2861 ordered_extent->file_offset +
2862 ordered_extent->len - 1);
2863
Josef Bacik77cef2e2013-08-29 13:57:21 -04002864 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2865 truncated = true;
2866 logical_len = ordered_extent->truncated_len;
2867 /* Truncated the entire extent, don't bother adding */
2868 if (!logical_len)
2869 goto out;
2870 }
2871
Yan, Zhengc2167752009-11-12 09:34:21 +00002872 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002873 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002874
2875 /*
2876 * For mwrite(mmap + memset to write) case, we still reserve
2877 * space for NOCOW range.
2878 * As NOCOW won't cause a new delayed ref, just free the space
2879 */
2880 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2881 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002882 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2883 if (nolock)
2884 trans = btrfs_join_transaction_nolock(root);
2885 else
2886 trans = btrfs_join_transaction(root);
2887 if (IS_ERR(trans)) {
2888 ret = PTR_ERR(trans);
2889 trans = NULL;
2890 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002891 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002893 ret = btrfs_update_inode_fallback(trans, root, inode);
2894 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002895 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002896 goto out;
2897 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898
Josef Bacik2ac55d42010-02-03 19:33:23 +00002899 lock_extent_bits(io_tree, ordered_extent->file_offset,
2900 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002901 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902
Liu Bo38c227d2013-01-29 03:18:40 +00002903 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2904 ordered_extent->file_offset + ordered_extent->len - 1,
2905 EXTENT_DEFRAG, 1, cached_state);
2906 if (ret) {
2907 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002908 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002909 /* the inode is shared */
2910 new = record_old_file_extents(inode, ordered_extent);
2911
2912 clear_extent_bit(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset + ordered_extent->len - 1,
2914 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2915 }
2916
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002918 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002919 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002920 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002921 if (IS_ERR(trans)) {
2922 ret = PTR_ERR(trans);
2923 trans = NULL;
2924 goto out_unlock;
2925 }
Chris Masona79b7d42014-05-22 16:18:52 -07002926
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002927 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002928
Chris Masonc8b97812008-10-29 14:49:59 -04002929 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002930 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002931 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002932 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002933 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002934 ordered_extent->file_offset,
2935 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002936 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002937 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002939 ret = insert_reserved_file_extent(trans, inode,
2940 ordered_extent->file_offset,
2941 ordered_extent->start,
2942 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002943 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002944 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002945 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002946 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002947 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002948 ordered_extent->start,
2949 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002950 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002951 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2952 ordered_extent->file_offset, ordered_extent->len,
2953 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002955 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002956 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002957 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002958
Chris Masone6dcd2d2008-07-17 12:53:50 -04002959 add_pending_csums(trans, inode, ordered_extent->file_offset,
2960 &ordered_extent->list);
2961
Josef Bacik6c760c02012-11-09 10:53:21 -05002962 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2963 ret = btrfs_update_inode_fallback(trans, root, inode);
2964 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002966 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002967 }
2968 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002969out_unlock:
2970 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2971 ordered_extent->file_offset +
2972 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002973out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002974 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002976 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002977 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002978
Josef Bacik77cef2e2013-08-29 13:57:21 -04002979 if (ret || truncated) {
2980 u64 start, end;
2981
2982 if (truncated)
2983 start = ordered_extent->file_offset + logical_len;
2984 else
2985 start = ordered_extent->file_offset;
2986 end = ordered_extent->file_offset + ordered_extent->len - 1;
2987 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2988
2989 /* Drop the cache for the part of the extent we didn't write. */
2990 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002992 /*
2993 * If the ordered extent had an IOERR or something else went
2994 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 * back to the allocator. We only free the extent in the
2996 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002997 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002998 if ((ret || !logical_len) &&
2999 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003000 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003001 btrfs_free_reserved_extent(fs_info,
3002 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003003 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003004 }
3005
3006
Josef Bacik5fd02042012-05-02 14:00:54 -04003007 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003008 * This needs to be done to make sure anybody waiting knows we are done
3009 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003010 */
3011 btrfs_remove_ordered_extent(inode, ordered_extent);
3012
Liu Bo38c227d2013-01-29 03:18:40 +00003013 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003014 if (new) {
3015 if (ret) {
3016 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003018 } else {
3019 relink_file_extents(new);
3020 }
3021 }
Liu Bo38c227d2013-01-29 03:18:40 +00003022
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 /* once for us */
3024 btrfs_put_ordered_extent(ordered_extent);
3025 /* once for the tree */
3026 btrfs_put_ordered_extent(ordered_extent);
3027
Josef Bacik5fd02042012-05-02 14:00:54 -04003028 return ret;
3029}
3030
3031static void finish_ordered_fn(struct btrfs_work *work)
3032{
3033 struct btrfs_ordered_extent *ordered_extent;
3034 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3035 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036}
3037
Christoph Hellwigb2950862008-12-02 09:54:17 -05003038static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003039 struct extent_state *state, int uptodate)
3040{
Josef Bacik5fd02042012-05-02 14:00:54 -04003041 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003043 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003044 struct btrfs_workqueue *wq;
3045 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003046
liubo1abe9b82011-03-24 11:18:59 +00003047 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3048
Chris Mason8b62b722009-09-02 16:53:46 -04003049 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003050 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3051 end - start + 1, uptodate))
3052 return 0;
3053
Liu Bo9e0af232014-08-15 23:36:53 +08003054 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003056 func = btrfs_freespace_write_helper;
3057 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003058 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003059 func = btrfs_endio_write_helper;
3060 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003061
Liu Bo9e0af232014-08-15 23:36:53 +08003062 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3063 NULL);
3064 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003065
3066 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003067}
3068
Miao Xiedc380ae2014-09-12 18:43:55 +08003069static int __readpage_endio_check(struct inode *inode,
3070 struct btrfs_io_bio *io_bio,
3071 int icsum, struct page *page,
3072 int pgoff, u64 start, size_t len)
3073{
3074 char *kaddr;
3075 u32 csum_expected;
3076 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003077
3078 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3079
3080 kaddr = kmap_atomic(page);
3081 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003082 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003083 if (csum != csum_expected)
3084 goto zeroit;
3085
3086 kunmap_atomic(kaddr);
3087 return 0;
3088zeroit:
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003089 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3090 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003091 memset(kaddr + pgoff, 1, len);
3092 flush_dcache_page(page);
3093 kunmap_atomic(kaddr);
3094 if (csum_expected == 0)
3095 return 0;
3096 return -EIO;
3097}
3098
Chris Masond352ac62008-09-29 15:18:18 -04003099/*
Chris Masond352ac62008-09-29 15:18:18 -04003100 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003101 * if there's a match, we allow the bio to finish. If not, the code in
3102 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003103 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003104static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3105 u64 phy_offset, struct page *page,
3106 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003107{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003108 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003109 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003110 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003111 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003112
Chris Masond20f7042008-12-08 16:58:54 -05003113 if (PageChecked(page)) {
3114 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003115 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003116 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003117
3118 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003119 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003120
Yan Zheng17d217f2008-12-12 10:03:38 -05003121 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003122 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003123 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003124 return 0;
3125 }
3126
Miao Xiefacc8a222013-07-25 19:22:34 +08003127 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003128 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3129 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003130}
Chris Masonb888db2b2007-08-27 16:49:44 -04003131
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003132void btrfs_add_delayed_iput(struct inode *inode)
3133{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003135 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003136
3137 if (atomic_add_unless(&inode->i_count, -1, 1))
3138 return;
3139
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003140 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003141 if (binode->delayed_iput_count == 0) {
3142 ASSERT(list_empty(&binode->delayed_iput));
3143 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3144 } else {
3145 binode->delayed_iput_count++;
3146 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003147 spin_unlock(&fs_info->delayed_iput_lock);
3148}
3149
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003150void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003151{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003152
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003153 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003154 while (!list_empty(&fs_info->delayed_iputs)) {
3155 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003156
David Sterba8089fe62015-11-19 14:15:51 +01003157 inode = list_first_entry(&fs_info->delayed_iputs,
3158 struct btrfs_inode, delayed_iput);
3159 if (inode->delayed_iput_count) {
3160 inode->delayed_iput_count--;
3161 list_move_tail(&inode->delayed_iput,
3162 &fs_info->delayed_iputs);
3163 } else {
3164 list_del_init(&inode->delayed_iput);
3165 }
3166 spin_unlock(&fs_info->delayed_iput_lock);
3167 iput(&inode->vfs_inode);
3168 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003169 }
David Sterba8089fe62015-11-19 14:15:51 +01003170 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003171}
3172
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003174 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003175 * files in the subvolume, it removes orphan item and frees block_rsv
3176 * structure.
3177 */
3178void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *root)
3180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003181 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003182 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 int ret;
3184
Josef Bacik8a35d952012-05-23 14:26:42 -04003185 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3187 return;
3188
Josef Bacik90290e12011-12-02 15:44:12 -05003189 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003190 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003191 spin_unlock(&root->orphan_lock);
3192 return;
3193 }
3194
3195 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3196 spin_unlock(&root->orphan_lock);
3197 return;
3198 }
3199
3200 block_rsv = root->orphan_block_rsv;
3201 root->orphan_block_rsv = NULL;
3202 spin_unlock(&root->orphan_lock);
3203
Miao Xie27cdeb72014-04-02 19:51:05 +08003204 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003205 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003208 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003209 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003210 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003211 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3212 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 }
3214
Josef Bacik90290e12011-12-02 15:44:12 -05003215 if (block_rsv) {
3216 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003217 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003218 }
3219}
3220
3221/*
Josef Bacik7b128762008-07-24 12:17:14 -04003222 * This creates an orphan entry for the given inode in case something goes
3223 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 *
3225 * NOTE: caller of this function should reserve 5 units of metadata for
3226 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003227 */
3228int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003231 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 struct btrfs_block_rsv *block_rsv = NULL;
3233 int reserve = 0;
3234 int insert = 0;
3235 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003236
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003238 block_rsv = btrfs_alloc_block_rsv(fs_info,
3239 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003240 if (!block_rsv)
3241 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003242 }
3243
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 spin_lock(&root->orphan_lock);
3245 if (!root->orphan_block_rsv) {
3246 root->orphan_block_rsv = block_rsv;
3247 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003248 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 block_rsv = NULL;
3250 }
Josef Bacik7b128762008-07-24 12:17:14 -04003251
Josef Bacik8a35d952012-05-23 14:26:42 -04003252 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3253 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254#if 0
3255 /*
3256 * For proper ENOSPC handling, we should do orphan
3257 * cleanup when mounting. But this introduces backward
3258 * compatibility issue.
3259 */
3260 if (!xchg(&root->orphan_item_inserted, 1))
3261 insert = 2;
3262 else
3263 insert = 1;
3264#endif
3265 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003266 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 }
Josef Bacik7b128762008-07-24 12:17:14 -04003268
Josef Bacik72ac3c02012-05-23 14:13:11 -04003269 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3270 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003273
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 /* grab metadata reservation from transaction handle */
3275 if (reserve) {
3276 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003277 ASSERT(!ret);
3278 if (ret) {
3279 atomic_dec(&root->orphan_inodes);
3280 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3281 &BTRFS_I(inode)->runtime_flags);
3282 if (insert)
3283 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3284 &BTRFS_I(inode)->runtime_flags);
3285 return ret;
3286 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 }
3288
3289 /* insert an orphan item to track this unlinked/truncated file */
3290 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003291 ret = btrfs_insert_orphan_item(trans, root,
3292 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003293 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003294 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003295 if (reserve) {
3296 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3297 &BTRFS_I(inode)->runtime_flags);
3298 btrfs_orphan_release_metadata(inode);
3299 }
3300 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003301 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3302 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003303 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003304 return ret;
3305 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003306 }
3307 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 }
3309
3310 /* insert an orphan item to track subvolume contains orphan files */
3311 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003312 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003314 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003315 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003316 return ret;
3317 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 }
3319 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003320}
3321
3322/*
3323 * We have done the truncate/delete so we can go ahead and remove the orphan
3324 * item for this particular inode.
3325 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003326static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3327 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003328{
3329 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003330 int delete_item = 0;
3331 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003332 int ret = 0;
3333
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003335 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3336 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003338
Josef Bacik72ac3c02012-05-23 14:13:11 -04003339 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3340 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 spin_unlock(&root->orphan_lock);
3343
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003344 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003345 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003346 if (trans)
3347 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003348 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003349 }
Josef Bacik7b128762008-07-24 12:17:14 -04003350
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003351 if (release_rsv)
3352 btrfs_orphan_release_metadata(inode);
3353
Josef Bacik4ef31a42013-08-13 14:10:08 -04003354 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003355}
3356
3357/*
3358 * this cleans up any orphans that may be left on the list from the last use
3359 * of this root.
3360 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003361int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003362{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003363 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003364 struct btrfs_path *path;
3365 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003366 struct btrfs_key key, found_key;
3367 struct btrfs_trans_handle *trans;
3368 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003369 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003370 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3371
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003373 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003374
3375 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 if (!path) {
3377 ret = -ENOMEM;
3378 goto out;
3379 }
David Sterbae4058b52015-11-27 16:31:35 +01003380 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003381
3382 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003383 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003384 key.offset = (u64)-1;
3385
Josef Bacik7b128762008-07-24 12:17:14 -04003386 while (1) {
3387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003388 if (ret < 0)
3389 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003390
3391 /*
3392 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003393 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003394 * find the key and see if we have stuff that matches
3395 */
3396 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003397 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003398 if (path->slots[0] == 0)
3399 break;
3400 path->slots[0]--;
3401 }
3402
3403 /* pull out the item */
3404 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003405 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3406
3407 /* make sure the item matches what we want */
3408 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3409 break;
David Sterba962a2982014-06-04 18:41:45 +02003410 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003411 break;
3412
3413 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003414 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003415
3416 /*
3417 * this is where we are basically btrfs_lookup, without the
3418 * crossing root thing. we store the inode number in the
3419 * offset of the orphan item.
3420 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003421
3422 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 btrfs_err(fs_info,
3424 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003425 ret = -EINVAL;
3426 goto out;
3427 }
3428
3429 last_objectid = found_key.offset;
3430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003431 found_key.objectid = found_key.offset;
3432 found_key.type = BTRFS_INODE_ITEM_KEY;
3433 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303435 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003436 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003437 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003438
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003439 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003440 struct btrfs_root *dead_root;
3441 struct btrfs_fs_info *fs_info = root->fs_info;
3442 int is_dead_root = 0;
3443
3444 /*
3445 * this is an orphan in the tree root. Currently these
3446 * could come from 2 sources:
3447 * a) a snapshot deletion in progress
3448 * b) a free space cache inode
3449 * We need to distinguish those two, as the snapshot
3450 * orphan must not get deleted.
3451 * find_dead_roots already ran before us, so if this
3452 * is a snapshot deletion, we should find the root
3453 * in the dead_roots list
3454 */
3455 spin_lock(&fs_info->trans_lock);
3456 list_for_each_entry(dead_root, &fs_info->dead_roots,
3457 root_list) {
3458 if (dead_root->root_key.objectid ==
3459 found_key.objectid) {
3460 is_dead_root = 1;
3461 break;
3462 }
3463 }
3464 spin_unlock(&fs_info->trans_lock);
3465 if (is_dead_root) {
3466 /* prevent this orphan from being found again */
3467 key.offset = found_key.objectid - 1;
3468 continue;
3469 }
3470 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003471 /*
3472 * Inode is already gone but the orphan item is still there,
3473 * kill the orphan item.
3474 */
Filipe Manana67710892016-06-06 11:51:25 +01003475 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003476 trans = btrfs_start_transaction(root, 1);
3477 if (IS_ERR(trans)) {
3478 ret = PTR_ERR(trans);
3479 goto out;
3480 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003481 btrfs_debug(fs_info, "auto deleting %Lu",
3482 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003483 ret = btrfs_del_orphan_item(trans, root,
3484 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003485 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003486 if (ret)
3487 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003488 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 }
Josef Bacik7b128762008-07-24 12:17:14 -04003490
Josef Bacik7b128762008-07-24 12:17:14 -04003491 /*
3492 * add this inode to the orphan list so btrfs_orphan_del does
3493 * the proper thing when we hit it
3494 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003495 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3496 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003497 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003498
Josef Bacik7b128762008-07-24 12:17:14 -04003499 /* if we have links, this was a truncate, lets do that */
3500 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303501 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003502 iput(inode);
3503 continue;
3504 }
Josef Bacik7b128762008-07-24 12:17:14 -04003505 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003506
3507 /* 1 for the orphan item deletion. */
3508 trans = btrfs_start_transaction(root, 1);
3509 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003510 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003511 ret = PTR_ERR(trans);
3512 goto out;
3513 }
3514 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003515 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003516 if (ret) {
3517 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003518 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003519 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003520
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003522 if (ret)
3523 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003524 } else {
3525 nr_unlink++;
3526 }
3527
3528 /* this will do delete_inode and everything for us */
3529 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003530 if (ret)
3531 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003532 }
Miao Xie3254c872011-11-10 20:45:05 -05003533 /* release the path since we're done with it */
3534 btrfs_release_path(path);
3535
Yan, Zhengd68fc572010-05-16 10:49:58 -04003536 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3537
3538 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003539 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003540 (u64)-1);
3541
Miao Xie27cdeb72014-04-02 19:51:05 +08003542 if (root->orphan_block_rsv ||
3543 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003544 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003545 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003546 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003547 }
Josef Bacik7b128762008-07-24 12:17:14 -04003548
3549 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003551 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003552 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003553
3554out:
3555 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003557 btrfs_free_path(path);
3558 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003559}
3560
Chris Masond352ac62008-09-29 15:18:18 -04003561/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003562 * very simple check to peek ahead in the leaf looking for xattrs. If we
3563 * don't find any xattrs, we know there can't be any acls.
3564 *
3565 * slot is the slot the inode is in, objectid is the objectid of the inode
3566 */
3567static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568 int slot, u64 objectid,
3569 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003570{
3571 u32 nritems = btrfs_header_nritems(leaf);
3572 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003573 static u64 xattr_access = 0;
3574 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003575 int scanned = 0;
3576
Josef Bacikf23b5a52013-06-19 10:16:26 -04003577 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003578 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3579 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3580 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3581 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003582 }
3583
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003585 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003586 while (slot < nritems) {
3587 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3588
3589 /* we found a different objectid, there must not be acls */
3590 if (found_key.objectid != objectid)
3591 return 0;
3592
3593 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003594 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003595 if (*first_xattr_slot == -1)
3596 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003597 if (found_key.offset == xattr_access ||
3598 found_key.offset == xattr_default)
3599 return 1;
3600 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003601
3602 /*
3603 * we found a key greater than an xattr key, there can't
3604 * be any acls later on
3605 */
3606 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3607 return 0;
3608
3609 slot++;
3610 scanned++;
3611
3612 /*
3613 * it goes inode, inode backrefs, xattrs, extents,
3614 * so if there are a ton of hard links to an inode there can
3615 * be a lot of backrefs. Don't waste time searching too hard,
3616 * this is just an optimization
3617 */
3618 if (scanned >= 8)
3619 break;
3620 }
3621 /* we hit the end of the leaf before we found an xattr or
3622 * something larger than an xattr. We have to assume the inode
3623 * has acls
3624 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003625 if (*first_xattr_slot == -1)
3626 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003627 return 1;
3628}
3629
3630/*
Chris Masond352ac62008-09-29 15:18:18 -04003631 * read an inode from the btree into the in-memory inode
3632 */
Filipe Manana67710892016-06-06 11:51:25 +01003633static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003634{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003635 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003636 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003637 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003638 struct btrfs_inode_item *inode_item;
3639 struct btrfs_root *root = BTRFS_I(inode)->root;
3640 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003641 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003642 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003643 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003644 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003645 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003646 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003647
3648 ret = btrfs_fill_inode(inode, &rdev);
3649 if (!ret)
3650 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003651
3652 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003653 if (!path) {
3654 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003655 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003656 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003657
Chris Mason39279cc2007-06-12 06:35:45 -04003658 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003659
Chris Mason39279cc2007-06-12 06:35:45 -04003660 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003661 if (ret) {
3662 if (ret > 0)
3663 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003665 }
Chris Mason39279cc2007-06-12 06:35:45 -04003666
Chris Mason5f39d392007-10-15 16:14:19 -04003667 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003668
3669 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003670 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003671
Chris Mason5f39d392007-10-15 16:14:19 -04003672 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3673 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003674 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003675 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003676 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3677 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003678 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003679
David Sterbaa937b972014-12-12 17:39:12 +01003680 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3681 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003682
David Sterbaa937b972014-12-12 17:39:12 +01003683 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3684 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003685
David Sterbaa937b972014-12-12 17:39:12 +01003686 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3687 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003688
chandan r9cc97d62012-07-04 12:48:07 +05303689 BTRFS_I(inode)->i_otime.tv_sec =
3690 btrfs_timespec_sec(leaf, &inode_item->otime);
3691 BTRFS_I(inode)->i_otime.tv_nsec =
3692 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003693
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003694 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003695 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003696 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3697
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003698 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003699 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003700 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003701 rdev = btrfs_inode_rdev(leaf, inode_item);
3702
Josef Bacikaec74772008-07-24 12:12:38 -04003703 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003704 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003705
3706cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003707 /*
3708 * If we were modified in the current generation and evicted from memory
3709 * and then re-read we need to do a full sync since we don't have any
3710 * idea about which extents were modified before we were evicted from
3711 * cache.
3712 *
3713 * This is required for both inode re-read from disk and delayed inode
3714 * in delayed_nodes_tree.
3715 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003716 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003717 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3718 &BTRFS_I(inode)->runtime_flags);
3719
Filipe Mananabde6c242015-07-24 00:00:19 +01003720 /*
3721 * We don't persist the id of the transaction where an unlink operation
3722 * against the inode was last made. So here we assume the inode might
3723 * have been evicted, and therefore the exact value of last_unlink_trans
3724 * lost, and set it to last_trans to avoid metadata inconsistencies
3725 * between the inode and its parent if the inode is fsync'ed and the log
3726 * replayed. For example, in the scenario:
3727 *
3728 * touch mydir/foo
3729 * ln mydir/foo mydir/bar
3730 * sync
3731 * unlink mydir/bar
3732 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3733 * xfs_io -c fsync mydir/foo
3734 * <power failure>
3735 * mount fs, triggers fsync log replay
3736 *
3737 * We must make sure that when we fsync our inode foo we also log its
3738 * parent inode, otherwise after log replay the parent still has the
3739 * dentry with the "bar" name but our inode foo has a link count of 1
3740 * and doesn't have an inode ref with the name "bar" anymore.
3741 *
3742 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003743 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003744 * transaction commits on fsync if our inode is a directory, or if our
3745 * inode is not a directory, logging its parent unnecessarily.
3746 */
3747 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3748
Miao Xie67de1172013-12-26 13:07:06 +08003749 path->slots[0]++;
3750 if (inode->i_nlink != 1 ||
3751 path->slots[0] >= btrfs_header_nritems(leaf))
3752 goto cache_acl;
3753
3754 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003755 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003756 goto cache_acl;
3757
3758 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3759 if (location.type == BTRFS_INODE_REF_KEY) {
3760 struct btrfs_inode_ref *ref;
3761
3762 ref = (struct btrfs_inode_ref *)ptr;
3763 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3764 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3765 struct btrfs_inode_extref *extref;
3766
3767 extref = (struct btrfs_inode_extref *)ptr;
3768 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3769 extref);
3770 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003771cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003772 /*
3773 * try to precache a NULL acl entry for files that don't have
3774 * any xattrs or acls
3775 */
Li Zefan33345d012011-04-20 10:31:50 +08003776 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003777 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003778 if (first_xattr_slot != -1) {
3779 path->slots[0] = first_xattr_slot;
3780 ret = btrfs_load_inode_props(inode, path);
3781 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003782 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003783 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003784 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003785 root->root_key.objectid, ret);
3786 }
3787 btrfs_free_path(path);
3788
Al Viro72c04902009-06-24 16:58:48 -04003789 if (!maybe_acls)
3790 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003791
Chris Mason39279cc2007-06-12 06:35:45 -04003792 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003793 case S_IFREG:
3794 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003795 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003796 inode->i_fop = &btrfs_file_operations;
3797 inode->i_op = &btrfs_file_inode_operations;
3798 break;
3799 case S_IFDIR:
3800 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003801 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003802 break;
3803 case S_IFLNK:
3804 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003805 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003806 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3807 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003808 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003809 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003810 init_special_inode(inode, inode->i_mode, rdev);
3811 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003813
3814 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003815 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003816
3817make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003818 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003819 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003820 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003821}
3822
Chris Masond352ac62008-09-29 15:18:18 -04003823/*
3824 * given a leaf and an inode, copy the inode fields into the leaf
3825 */
Chris Masone02119d2008-09-05 16:13:11 -04003826static void fill_inode_item(struct btrfs_trans_handle *trans,
3827 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003828 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003829 struct inode *inode)
3830{
Liu Bo51fab692012-12-27 09:01:21 +00003831 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003832
Liu Bo51fab692012-12-27 09:01:21 +00003833 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003834
Liu Bo51fab692012-12-27 09:01:21 +00003835 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3836 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3837 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3838 &token);
3839 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3840 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003841
David Sterbaa937b972014-12-12 17:39:12 +01003842 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003843 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003844 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003845 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003846
David Sterbaa937b972014-12-12 17:39:12 +01003847 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003848 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003849 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003850 inode->i_mtime.tv_nsec, &token);
3851
David Sterbaa937b972014-12-12 17:39:12 +01003852 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003853 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003854 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003855 inode->i_ctime.tv_nsec, &token);
3856
chandan r9cc97d62012-07-04 12:48:07 +05303857 btrfs_set_token_timespec_sec(leaf, &item->otime,
3858 BTRFS_I(inode)->i_otime.tv_sec, &token);
3859 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3860 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3861
Liu Bo51fab692012-12-27 09:01:21 +00003862 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3863 &token);
3864 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3865 &token);
3866 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3867 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3868 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3869 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3870 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003871}
3872
Chris Masond352ac62008-09-29 15:18:18 -04003873/*
3874 * copy everything in the in-memory inode into the btree.
3875 */
Chris Mason21151332011-11-10 20:39:08 -05003876static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003877 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003878{
3879 struct btrfs_inode_item *inode_item;
3880 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003881 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003882 int ret;
3883
3884 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003885 if (!path)
3886 return -ENOMEM;
3887
Chris Masonb9473432009-03-13 11:00:37 -04003888 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003889 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3890 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003891 if (ret) {
3892 if (ret > 0)
3893 ret = -ENOENT;
3894 goto failed;
3895 }
3896
Chris Mason5f39d392007-10-15 16:14:19 -04003897 leaf = path->nodes[0];
3898 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003899 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003900
Chris Masone02119d2008-09-05 16:13:11 -04003901 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003902 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003903 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 ret = 0;
3905failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003906 btrfs_free_path(path);
3907 return ret;
3908}
3909
Chris Masond352ac62008-09-29 15:18:18 -04003910/*
Chris Mason21151332011-11-10 20:39:08 -05003911 * copy everything in the in-memory inode into the btree.
3912 */
3913noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root, struct inode *inode)
3915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003916 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003917 int ret;
3918
3919 /*
3920 * If the inode is a free space inode, we can deadlock during commit
3921 * if we put it into the delayed code.
3922 *
3923 * The data relocation inode should also be directly updated
3924 * without delay
3925 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003926 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003927 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003928 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003929 btrfs_update_root_times(trans, root);
3930
Chris Mason21151332011-11-10 20:39:08 -05003931 ret = btrfs_delayed_update_inode(trans, root, inode);
3932 if (!ret)
3933 btrfs_set_inode_last_trans(trans, inode);
3934 return ret;
3935 }
3936
3937 return btrfs_update_inode_item(trans, root, inode);
3938}
3939
Josef Bacikbe6aef62012-10-22 15:43:12 -04003940noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3941 struct btrfs_root *root,
3942 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003943{
3944 int ret;
3945
3946 ret = btrfs_update_inode(trans, root, inode);
3947 if (ret == -ENOSPC)
3948 return btrfs_update_inode_item(trans, root, inode);
3949 return ret;
3950}
3951
3952/*
Chris Masond352ac62008-09-29 15:18:18 -04003953 * unlink helper that gets used here in inode.c and in the tree logging
3954 * recovery code. It remove a link in a directory with a given name, and
3955 * also drops the back refs in the inode to the directory
3956 */
Al Viro92986792011-03-04 17:14:37 +00003957static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3958 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003959 struct btrfs_inode *dir,
3960 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003961 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003962{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003964 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003965 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003966 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003967 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003968 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003969 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003970 u64 ino = btrfs_ino(inode);
3971 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003972
3973 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003974 if (!path) {
3975 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003976 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003977 }
3978
Chris Masonb9473432009-03-13 11:00:37 -04003979 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003980 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003981 name, name_len, -1);
3982 if (IS_ERR(di)) {
3983 ret = PTR_ERR(di);
3984 goto err;
3985 }
3986 if (!di) {
3987 ret = -ENOENT;
3988 goto err;
3989 }
Chris Mason5f39d392007-10-15 16:14:19 -04003990 leaf = path->nodes[0];
3991 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003992 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003993 if (ret)
3994 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003995 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003996
Miao Xie67de1172013-12-26 13:07:06 +08003997 /*
3998 * If we don't have dir index, we have to get it by looking up
3999 * the inode ref, since we get the inode ref, remove it directly,
4000 * it is unnecessary to do delayed deletion.
4001 *
4002 * But if we have dir index, needn't search inode ref to get it.
4003 * Since the inode ref is close to the inode item, it is better
4004 * that we delay to delete it, and just do this deletion when
4005 * we update the inode item.
4006 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004007 if (inode->dir_index) {
4008 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004009 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004010 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004011 goto skip_backref;
4012 }
4013 }
4014
Li Zefan33345d012011-04-20 10:31:50 +08004015 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4016 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004017 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004018 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004019 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004020 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004021 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004022 goto err;
4023 }
Miao Xie67de1172013-12-26 13:07:06 +08004024skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004025 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004026 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004027 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004028 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004029 }
Chris Mason39279cc2007-06-12 06:35:45 -04004030
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004031 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4032 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004033 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004034 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004035 goto err;
4036 }
Chris Masone02119d2008-09-05 16:13:11 -04004037
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004038 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4039 index);
Chris Mason6418c962010-10-30 07:34:24 -04004040 if (ret == -ENOENT)
4041 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004042 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004043 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004044err:
4045 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004046 if (ret)
4047 goto out;
4048
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004049 btrfs_i_size_write(&dir->vfs_inode,
4050 dir->vfs_inode.i_size - name_len * 2);
4051 inode_inc_iversion(&inode->vfs_inode);
4052 inode_inc_iversion(&dir->vfs_inode);
4053 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4054 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4055 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004056out:
Chris Mason39279cc2007-06-12 06:35:45 -04004057 return ret;
4058}
4059
Al Viro92986792011-03-04 17:14:37 +00004060int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4061 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004062 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004063 const char *name, int name_len)
4064{
4065 int ret;
4066 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4067 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004068 drop_nlink(&inode->vfs_inode);
4069 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004070 }
4071 return ret;
4072}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004073
4074/*
4075 * helper to start transaction for unlink and rmdir.
4076 *
Josef Bacikd52be812013-05-29 14:54:47 -04004077 * unlink and rmdir are special in btrfs, they do not always free space, so
4078 * if we cannot make our reservations the normal way try and see if there is
4079 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4080 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004081 */
Josef Bacikd52be812013-05-29 14:54:47 -04004082static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004083{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004084 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085
Josef Bacike70bea52011-10-11 14:18:24 -04004086 /*
4087 * 1 for the possible orphan item
4088 * 1 for the dir item
4089 * 1 for the dir index
4090 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004091 * 1 for the inode
4092 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004093 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004094}
4095
Chris Mason39279cc2007-06-12 06:35:45 -04004096static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4097{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004098 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004099 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004100 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004101 int ret;
4102
Josef Bacikd52be812013-05-29 14:54:47 -04004103 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004104 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004105 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004106
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004107 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4108 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004109
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4111 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4112 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004113 if (ret)
4114 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004115
Yan, Zhenga22285a2010-05-16 10:48:46 -04004116 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004117 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004118 if (ret)
4119 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004120 }
Josef Bacik7b128762008-07-24 12:17:14 -04004121
Tsutomu Itohb5324022011-07-19 07:27:20 +00004122out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004123 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004124 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004125 return ret;
4126}
4127
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4129 struct btrfs_root *root,
4130 struct inode *dir, u64 objectid,
4131 const char *name, int name_len)
4132{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004133 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134 struct btrfs_path *path;
4135 struct extent_buffer *leaf;
4136 struct btrfs_dir_item *di;
4137 struct btrfs_key key;
4138 u64 index;
4139 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004140 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004141
4142 path = btrfs_alloc_path();
4143 if (!path)
4144 return -ENOMEM;
4145
Li Zefan33345d012011-04-20 10:31:50 +08004146 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (IS_ERR_OR_NULL(di)) {
4149 if (!di)
4150 ret = -ENOENT;
4151 else
4152 ret = PTR_ERR(di);
4153 goto out;
4154 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004155
4156 leaf = path->nodes[0];
4157 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4158 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4159 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004161 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 goto out;
4163 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004164 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004165
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004166 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4167 root->root_key.objectid, dir_ino,
4168 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004171 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004172 goto out;
4173 }
Li Zefan33345d012011-04-20 10:31:50 +08004174 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004175 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 if (IS_ERR_OR_NULL(di)) {
4177 if (!di)
4178 ret = -ENOENT;
4179 else
4180 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004181 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004182 goto out;
4183 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004184
4185 leaf = path->nodes[0];
4186 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004187 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004188 index = key.offset;
4189 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004190 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004192 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004193 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004194 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004195 goto out;
4196 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004197
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004199 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004200 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004201 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004202 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004203 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004204out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004205 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004206 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207}
4208
Chris Mason39279cc2007-06-12 06:35:45 -04004209static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4210{
David Howells2b0143b2015-03-17 22:25:59 +00004211 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004212 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004213 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004214 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004215 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004216
David Sterbab3ae2442012-09-13 16:04:34 -06004217 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004218 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004219 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004220 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004221
Josef Bacikd52be812013-05-29 14:54:47 -04004222 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004223 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004224 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004225
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004226 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 err = btrfs_unlink_subvol(trans, root, dir,
4228 BTRFS_I(inode)->location.objectid,
4229 dentry->d_name.name,
4230 dentry->d_name.len);
4231 goto out;
4232 }
4233
Josef Bacik7b128762008-07-24 12:17:14 -04004234 err = btrfs_orphan_add(trans, inode);
4235 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004237
Filipe Manana44f714d2016-06-06 16:11:13 +01004238 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4239
Chris Mason39279cc2007-06-12 06:35:45 -04004240 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004241 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4242 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4243 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004244 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004245 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004246 /*
4247 * Propagate the last_unlink_trans value of the deleted dir to
4248 * its parent directory. This is to prevent an unrecoverable
4249 * log tree in the case we do something like this:
4250 * 1) create dir foo
4251 * 2) create snapshot under dir foo
4252 * 3) delete the snapshot
4253 * 4) rmdir foo
4254 * 5) mkdir foo
4255 * 6) fsync foo or some file inside foo
4256 */
4257 if (last_unlink_trans >= trans->transid)
4258 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4259 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004261 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004262 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004263
Chris Mason39279cc2007-06-12 06:35:45 -04004264 return err;
4265}
4266
Chris Mason28f75a02015-02-04 06:59:29 -08004267static int truncate_space_check(struct btrfs_trans_handle *trans,
4268 struct btrfs_root *root,
4269 u64 bytes_deleted)
4270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004271 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004272 int ret;
4273
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004274 /*
4275 * This is only used to apply pressure to the enospc system, we don't
4276 * intend to use this reservation at all.
4277 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004278 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004279 bytes_deleted *= fs_info->nodesize;
4280 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004281 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004282 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004283 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004284 trans->transid,
4285 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004286 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004287 }
Chris Mason28f75a02015-02-04 06:59:29 -08004288 return ret;
4289
4290}
4291
Filipe Manana0305cd52015-10-16 12:34:25 +01004292static int truncate_inline_extent(struct inode *inode,
4293 struct btrfs_path *path,
4294 struct btrfs_key *found_key,
4295 const u64 item_end,
4296 const u64 new_size)
4297{
4298 struct extent_buffer *leaf = path->nodes[0];
4299 int slot = path->slots[0];
4300 struct btrfs_file_extent_item *fi;
4301 u32 size = (u32)(new_size - found_key->offset);
4302 struct btrfs_root *root = BTRFS_I(inode)->root;
4303
4304 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4305
4306 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4307 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004308 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004309
4310 /*
4311 * Zero out the remaining of the last page of our inline extent,
4312 * instead of directly truncating our inline extent here - that
4313 * would be much more complex (decompressing all the data, then
4314 * compressing the truncated data, which might be bigger than
4315 * the size of the inline extent, resize the extent, etc).
4316 * We release the path because to get the page we might need to
4317 * read the extent item from disk (data not in the page cache).
4318 */
4319 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304320 return btrfs_truncate_block(inode, offset, page_end - offset,
4321 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004322 }
4323
4324 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4325 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004326 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004327
4328 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4329 inode_sub_bytes(inode, item_end + 1 - new_size);
4330
4331 return 0;
4332}
4333
Chris Mason323ac952008-10-01 19:05:46 -04004334/*
Chris Mason39279cc2007-06-12 06:35:45 -04004335 * this can truncate away extent items, csum items and directory items.
4336 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004337 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004338 *
4339 * csum items that cross the new i_size are truncated to the new size
4340 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004341 *
4342 * min_type is the minimum key type to truncate down to. If set to 0, this
4343 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004344 */
Yan, Zheng80825102009-11-12 09:35:36 +00004345int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4346 struct btrfs_root *root,
4347 struct inode *inode,
4348 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004349{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004350 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004351 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004352 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004353 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004354 struct btrfs_key key;
4355 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004356 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004357 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004358 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004359 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004360 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004361 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004362 int found_extent;
4363 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004364 int pending_del_nr = 0;
4365 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004366 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004367 int ret;
4368 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004369 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004370 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004371 bool be_nice = 0;
4372 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004373 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004374
4375 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004376
Chris Mason28ed1342014-12-17 09:41:04 -08004377 /*
4378 * for non-free space inodes and ref cows, we want to back off from
4379 * time to time
4380 */
4381 if (!btrfs_is_free_space_inode(inode) &&
4382 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4383 be_nice = 1;
4384
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004385 path = btrfs_alloc_path();
4386 if (!path)
4387 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004388 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004389
Josef Bacik5dc562c2012-08-17 13:14:17 -04004390 /*
4391 * We want to drop from the next block forward in case this new size is
4392 * not block aligned since we will be keeping the last block of the
4393 * extent just the way it is.
4394 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004395 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004396 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004397 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004399 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004400
Miao Xie16cdcec2011-04-22 18:12:22 +08004401 /*
4402 * This function is also used to drop the items in the log tree before
4403 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4404 * it is used to drop the loged items. So we shouldn't kill the delayed
4405 * items.
4406 */
4407 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004408 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004409
Li Zefan33345d012011-04-20 10:31:50 +08004410 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004412 key.type = (u8)-1;
4413
Chris Mason85e21ba2008-01-29 15:11:36 -05004414search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004415 /*
4416 * with a 16K leaf size and 128MB extents, you can actually queue
4417 * up a huge file in a single leaf. Most of the time that
4418 * bytes_deleted is > 0, it will be huge by the time we get here
4419 */
Byongho Leeee221842015-12-15 01:42:10 +09004420 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004421 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004422 err = -EAGAIN;
4423 goto error;
4424 }
4425 }
4426
4427
Chris Masonb9473432009-03-13 11:00:37 -04004428 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004429 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004430 if (ret < 0) {
4431 err = ret;
4432 goto out;
4433 }
Chris Masond3977122009-01-05 21:25:51 -05004434
Chris Mason85e21ba2008-01-29 15:11:36 -05004435 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004436 /* there are no items in the tree for us to truncate, we're
4437 * done
4438 */
Yan, Zheng80825102009-11-12 09:35:36 +00004439 if (path->slots[0] == 0)
4440 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004441 path->slots[0]--;
4442 }
4443
Chris Masond3977122009-01-05 21:25:51 -05004444 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004445 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004446 leaf = path->nodes[0];
4447 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004448 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004449
Li Zefan33345d012011-04-20 10:31:50 +08004450 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004451 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004452
Chris Mason85e21ba2008-01-29 15:11:36 -05004453 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004454 break;
4455
Chris Mason5f39d392007-10-15 16:14:19 -04004456 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004457 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004458 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004459 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004460 extent_type = btrfs_file_extent_type(leaf, fi);
4461 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004462 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004463 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004464 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004465 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004466 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
Yan008630c2007-11-07 13:31:09 -05004468 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
Yan, Zheng80825102009-11-12 09:35:36 +00004470 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004471 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004472 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004473 if (item_end < new_size) {
4474 /*
4475 * With NO_HOLES mode, for the following mapping
4476 *
4477 * [0-4k][hole][8k-12k]
4478 *
4479 * if truncating isize down to 6k, it ends up
4480 * isize being 8k.
4481 */
4482 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4483 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004484 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004485 }
Yan, Zheng80825102009-11-12 09:35:36 +00004486 if (found_key.offset >= new_size)
4487 del_item = 1;
4488 else
4489 del_item = 0;
4490 }
Chris Mason39279cc2007-06-12 06:35:45 -04004491 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004492 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004493 if (found_type != BTRFS_EXTENT_DATA_KEY)
4494 goto delete;
4495
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004496 if (del_item)
4497 last_size = found_key.offset;
4498 else
4499 last_size = new_size;
4500
Chris Mason179e29e2007-11-01 11:28:41 -04004501 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004502 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004503 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004504 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004505 u64 orig_num_bytes =
4506 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004507 extent_num_bytes = ALIGN(new_size -
4508 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004510 btrfs_set_file_extent_num_bytes(leaf, fi,
4511 extent_num_bytes);
4512 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004513 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004514 if (test_bit(BTRFS_ROOT_REF_COWS,
4515 &root->state) &&
4516 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004517 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004518 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004519 } else {
Chris Masondb945352007-10-15 16:15:53 -04004520 extent_num_bytes =
4521 btrfs_file_extent_disk_num_bytes(leaf,
4522 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004523 extent_offset = found_key.offset -
4524 btrfs_file_extent_offset(leaf, fi);
4525
Chris Mason39279cc2007-06-12 06:35:45 -04004526 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004527 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004528 if (extent_start != 0) {
4529 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004530 if (test_bit(BTRFS_ROOT_REF_COWS,
4531 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004532 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004533 }
4534 }
Chris Mason90692182008-02-08 13:49:28 -05004535 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004536 /*
4537 * we can't truncate inline items that have had
4538 * special encodings
4539 */
4540 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004541 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4542 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004543
4544 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004545 * Need to release path in order to truncate a
4546 * compressed extent. So delete any accumulated
4547 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004548 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004549 if (btrfs_file_extent_compression(leaf, fi) !=
4550 BTRFS_COMPRESS_NONE && pending_del_nr) {
4551 err = btrfs_del_items(trans, root, path,
4552 pending_del_slot,
4553 pending_del_nr);
4554 if (err) {
4555 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004556 err);
4557 goto error;
4558 }
4559 pending_del_nr = 0;
4560 }
4561
4562 err = truncate_inline_extent(inode, path,
4563 &found_key,
4564 item_end,
4565 new_size);
4566 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004567 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004568 goto error;
4569 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004570 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4571 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004572 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004573 }
Chris Mason39279cc2007-06-12 06:35:45 -04004574 }
Chris Mason179e29e2007-11-01 11:28:41 -04004575delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004576 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004577 if (!pending_del_nr) {
4578 /* no pending yet, add ourselves */
4579 pending_del_slot = path->slots[0];
4580 pending_del_nr = 1;
4581 } else if (pending_del_nr &&
4582 path->slots[0] + 1 == pending_del_slot) {
4583 /* hop on the pending chunk */
4584 pending_del_nr++;
4585 pending_del_slot = path->slots[0];
4586 } else {
Chris Masond3977122009-01-05 21:25:51 -05004587 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004588 }
Chris Mason39279cc2007-06-12 06:35:45 -04004589 } else {
4590 break;
4591 }
Chris Mason28f75a02015-02-04 06:59:29 -08004592 should_throttle = 0;
4593
Miao Xie27cdeb72014-04-02 19:51:05 +08004594 if (found_extent &&
4595 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004596 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004597 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004598 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004599 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004600 extent_num_bytes, 0,
4601 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004602 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004603 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004604 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4605 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004606 trans->delayed_ref_updates * 2,
4607 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004608 if (be_nice) {
4609 if (truncate_space_check(trans, root,
4610 extent_num_bytes)) {
4611 should_end = 1;
4612 }
4613 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004614 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004615 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004616 }
Chris Mason39279cc2007-06-12 06:35:45 -04004617 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004618
Yan, Zheng80825102009-11-12 09:35:36 +00004619 if (found_type == BTRFS_INODE_ITEM_KEY)
4620 break;
4621
4622 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004623 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004624 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004625 if (pending_del_nr) {
4626 ret = btrfs_del_items(trans, root, path,
4627 pending_del_slot,
4628 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004629 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004630 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004631 goto error;
4632 }
Yan, Zheng80825102009-11-12 09:35:36 +00004633 pending_del_nr = 0;
4634 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004635 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004636 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004637 unsigned long updates = trans->delayed_ref_updates;
4638 if (updates) {
4639 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004640 ret = btrfs_run_delayed_refs(trans,
4641 fs_info,
4642 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004643 if (ret && !err)
4644 err = ret;
4645 }
4646 }
Chris Mason28f75a02015-02-04 06:59:29 -08004647 /*
4648 * if we failed to refill our space rsv, bail out
4649 * and let the transaction restart
4650 */
4651 if (should_end) {
4652 err = -EAGAIN;
4653 goto error;
4654 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004655 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004656 } else {
4657 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 }
Chris Mason39279cc2007-06-12 06:35:45 -04004659 }
Yan, Zheng80825102009-11-12 09:35:36 +00004660out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004661 if (pending_del_nr) {
4662 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4663 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004664 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004665 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004666 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004667error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004668 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004669 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004670
Chris Mason39279cc2007-06-12 06:35:45 -04004671 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004672
Liu Bo19fd2df2016-12-01 13:01:02 -08004673 if (err == 0) {
4674 /* only inline file may have last_size != new_size */
4675 if (new_size >= fs_info->sectorsize ||
4676 new_size > fs_info->max_inline)
4677 ASSERT(last_size == new_size);
4678 }
4679
Byongho Leeee221842015-12-15 01:42:10 +09004680 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004681 unsigned long updates = trans->delayed_ref_updates;
4682 if (updates) {
4683 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 ret = btrfs_run_delayed_refs(trans, fs_info,
4685 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004686 if (ret && !err)
4687 err = ret;
4688 }
4689 }
Yan, Zheng80825102009-11-12 09:35:36 +00004690 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004691}
4692
Chris Masona52d9a82007-08-27 16:49:44 -04004693/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304694 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004695 * @inode - inode that we're zeroing
4696 * @from - the offset to start zeroing
4697 * @len - the length to zero, 0 to zero the entire range respective to the
4698 * offset
4699 * @front - zero up to the offset instead of from the offset on
4700 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004702 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004703 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304704int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004705 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004708 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4710 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004711 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004712 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004713 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004714 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004716 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004717 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004718 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 u64 block_start;
4720 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004721
Josef Bacik2aaa6652012-08-29 14:27:18 -04004722 if ((offset & (blocksize - 1)) == 0 &&
4723 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004724 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004726 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004728 if (ret)
4729 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004730
Chris Mason211c17f2008-05-15 09:13:45 -04004731again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004732 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004733 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004734 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 round_down(from, blocksize),
4736 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004737 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004738 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004739 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 block_start = round_down(from, blocksize);
4742 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004743
Chris Masona52d9a82007-08-27 16:49:44 -04004744 if (!PageUptodate(page)) {
4745 ret = btrfs_readpage(NULL, page);
4746 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004747 if (page->mapping != mapping) {
4748 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004749 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004750 goto again;
4751 }
Chris Masona52d9a82007-08-27 16:49:44 -04004752 if (!PageUptodate(page)) {
4753 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004754 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004755 }
4756 }
Chris Mason211c17f2008-05-15 09:13:45 -04004757 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004760 set_page_extent_mapped(page);
4761
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004763 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304764 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004765 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004766 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004767 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004768 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004769 btrfs_put_ordered_extent(ordered);
4770 goto again;
4771 }
4772
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004774 EXTENT_DIRTY | EXTENT_DELALLOC |
4775 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004776 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004777
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004779 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004780 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004782 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004783 goto out_unlock;
4784 }
4785
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004787 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004789 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004790 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304791 memset(kaddr + (block_start - page_offset(page)),
4792 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004793 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304794 memset(kaddr + (block_start - page_offset(page)) + offset,
4795 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004796 flush_dcache_page(page);
4797 kunmap(page);
4798 }
Chris Mason247e7432008-07-17 12:53:51 -04004799 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004800 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004802 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004803
Chris Mason89642222008-07-24 09:41:53 -04004804out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004805 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 btrfs_delalloc_release_space(inode, block_start,
4807 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004808 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004809 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004810out:
4811 return ret;
4812}
4813
Josef Bacik16e75492013-10-22 12:18:51 -04004814static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4815 u64 offset, u64 len)
4816{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004817 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004818 struct btrfs_trans_handle *trans;
4819 int ret;
4820
4821 /*
4822 * Still need to make sure the inode looks like it's been updated so
4823 * that any holes get logged if we fsync.
4824 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004825 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4826 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004827 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4828 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4829 return 0;
4830 }
4831
4832 /*
4833 * 1 - for the one we're dropping
4834 * 1 - for the one we're adding
4835 * 1 - for updating the inode.
4836 */
4837 trans = btrfs_start_transaction(root, 3);
4838 if (IS_ERR(trans))
4839 return PTR_ERR(trans);
4840
4841 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004844 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004845 return ret;
4846 }
4847
David Sterbaf85b7372017-01-20 14:54:07 +01004848 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4849 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004850 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004851 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004852 else
4853 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004854 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004855 return ret;
4856}
4857
Josef Bacik695a0d02011-03-04 15:46:53 -05004858/*
4859 * This function puts in dummy file extents for the area we're creating a hole
4860 * for. So if we are truncating this file to a larger size we need to insert
4861 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4862 * the range between oldsize and size
4863 */
Josef Bacika41ad392011-01-31 15:30:16 -05004864int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004865{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004867 struct btrfs_root *root = BTRFS_I(inode)->root;
4868 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004869 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004870 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004871 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4873 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004874 u64 last_byte;
4875 u64 cur_offset;
4876 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004877 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004878
Josef Bacika71754f2013-06-17 17:14:39 -04004879 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 * If our size started in the middle of a block we need to zero out the
4881 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004882 * expose stale data.
4883 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304884 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004885 if (err)
4886 return err;
4887
Yan Zheng9036c102008-10-30 14:19:41 -04004888 if (size <= hole_start)
4889 return 0;
4890
Yan Zheng9036c102008-10-30 14:19:41 -04004891 while (1) {
4892 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004893
David Sterbaff13db42015-12-03 14:30:40 +01004894 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004895 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004896 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4897 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004898 if (!ordered)
4899 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004900 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4901 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004902 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004903 btrfs_put_ordered_extent(ordered);
4904 }
4905
Yan Zheng9036c102008-10-30 14:19:41 -04004906 cur_offset = hole_start;
4907 while (1) {
4908 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4909 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004910 if (IS_ERR(em)) {
4911 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004912 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004913 break;
4914 }
Yan Zheng9036c102008-10-30 14:19:41 -04004915 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004916 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004917 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004918 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004919 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004920
Josef Bacik16e75492013-10-22 12:18:51 -04004921 err = maybe_insert_hole(root, inode, cur_offset,
4922 hole_size);
4923 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004924 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004925 btrfs_drop_extent_cache(inode, cur_offset,
4926 cur_offset + hole_size - 1, 0);
4927 hole_em = alloc_extent_map();
4928 if (!hole_em) {
4929 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4930 &BTRFS_I(inode)->runtime_flags);
4931 goto next;
4932 }
4933 hole_em->start = cur_offset;
4934 hole_em->len = hole_size;
4935 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004936
Josef Bacik5dc562c2012-08-17 13:14:17 -04004937 hole_em->block_start = EXTENT_MAP_HOLE;
4938 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004939 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004940 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004944
4945 while (1) {
4946 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004947 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004948 write_unlock(&em_tree->lock);
4949 if (err != -EEXIST)
4950 break;
4951 btrfs_drop_extent_cache(inode, cur_offset,
4952 cur_offset +
4953 hole_size - 1, 0);
4954 }
4955 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004956 }
Josef Bacik16e75492013-10-22 12:18:51 -04004957next:
Yan Zheng9036c102008-10-30 14:19:41 -04004958 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004959 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004960 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004961 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004962 break;
4963 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004964 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004965 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4966 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004967 return err;
4968}
4969
Eric Sandeen3972f262013-01-12 02:57:22 +00004970static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004971{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004972 struct btrfs_root *root = BTRFS_I(inode)->root;
4973 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004974 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004975 loff_t newsize = attr->ia_size;
4976 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004977 int ret;
4978
Eric Sandeen3972f262013-01-12 02:57:22 +00004979 /*
4980 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4981 * special case where we need to update the times despite not having
4982 * these flags set. For all other operations the VFS set these flags
4983 * explicitly if it wants a timestamp update.
4984 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004985 if (newsize != oldsize) {
4986 inode_inc_iversion(inode);
4987 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4988 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004989 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004990 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004991
Josef Bacika41ad392011-01-31 15:30:16 -05004992 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004993 /*
4994 * Don't do an expanding truncate while snapshoting is ongoing.
4995 * This is to ensure the snapshot captures a fully consistent
4996 * state of this file - if the snapshot captures this expanding
4997 * truncation, it must capture all writes that happened before
4998 * this truncation.
4999 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005000 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005001 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005002 if (ret) {
5003 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005004 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005005 }
Yan, Zheng80825102009-11-12 09:35:36 +00005006
Miao Xief4a2f4c2011-12-14 20:12:01 -05005007 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005008 if (IS_ERR(trans)) {
5009 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005010 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005011 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005012
5013 i_size_write(inode, newsize);
5014 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305015 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005016 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005017 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005018 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005019 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005020
Josef Bacika41ad392011-01-31 15:30:16 -05005021 /*
5022 * We're truncating a file that used to have good data down to
5023 * zero. Make sure it gets into the ordered flush list so that
5024 * any new writes get down to disk quickly.
5025 */
5026 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005027 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5028 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005029
Josef Bacikf3fe8202013-01-07 17:03:21 -05005030 /*
5031 * 1 for the orphan item we're going to add
5032 * 1 for the orphan item deletion.
5033 */
5034 trans = btrfs_start_transaction(root, 2);
5035 if (IS_ERR(trans))
5036 return PTR_ERR(trans);
5037
5038 /*
5039 * We need to do this in case we fail at _any_ point during the
5040 * actual truncate. Once we do the truncate_setsize we could
5041 * invalidate pages which forces any outstanding ordered io to
5042 * be instantly completed which will give us extents that need
5043 * to be truncated. If we fail to get an orphan inode down we
5044 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005045 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005046 * will be consistent.
5047 */
5048 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005049 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005050 if (ret)
5051 return ret;
5052
Josef Bacika41ad392011-01-31 15:30:16 -05005053 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5054 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005055
5056 /* Disable nonlocked read DIO to avoid the end less truncate */
5057 btrfs_inode_block_unlocked_dio(inode);
5058 inode_dio_wait(inode);
5059 btrfs_inode_resume_unlocked_dio(inode);
5060
Josef Bacika41ad392011-01-31 15:30:16 -05005061 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005062 if (ret && inode->i_nlink) {
5063 int err;
5064
Liu Bo19fd2df2016-12-01 13:01:02 -08005065 /* To get a stable disk_i_size */
5066 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5067 if (err) {
5068 btrfs_orphan_del(NULL, inode);
5069 return err;
5070 }
5071
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005072 /*
5073 * failed to truncate, disk_i_size is only adjusted down
5074 * as we remove extents, so it should represent the true
5075 * size of the inode, so reset the in memory size and
5076 * delete our orphan entry.
5077 */
5078 trans = btrfs_join_transaction(root);
5079 if (IS_ERR(trans)) {
5080 btrfs_orphan_del(NULL, inode);
5081 return ret;
5082 }
5083 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5084 err = btrfs_orphan_del(trans, inode);
5085 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005086 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005087 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005088 }
Yan, Zheng80825102009-11-12 09:35:36 +00005089 }
5090
Josef Bacika41ad392011-01-31 15:30:16 -05005091 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005092}
5093
Chris Mason39279cc2007-06-12 06:35:45 -04005094static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5095{
David Howells2b0143b2015-03-17 22:25:59 +00005096 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005097 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005098 int err;
5099
Li Zefanb83cc962010-12-20 16:04:08 +08005100 if (btrfs_root_readonly(root))
5101 return -EROFS;
5102
Jan Kara31051c82016-05-26 16:55:18 +02005103 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005104 if (err)
5105 return err;
5106
Chris Mason5a3f23d2009-03-31 13:27:11 -04005107 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005108 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005109 if (err)
5110 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005111 }
Yan Zheng9036c102008-10-30 14:19:41 -04005112
Christoph Hellwig10257742010-06-04 11:30:02 +02005113 if (attr->ia_valid) {
5114 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005115 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005116 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005117
Josef Bacik22c44fe2011-11-30 10:45:38 -05005118 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005119 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005120 }
5121
Chris Mason39279cc2007-06-12 06:35:45 -04005122 return err;
5123}
Chris Mason61295eb2008-01-14 16:24:38 -05005124
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005125/*
5126 * While truncating the inode pages during eviction, we get the VFS calling
5127 * btrfs_invalidatepage() against each page of the inode. This is slow because
5128 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5129 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5130 * extent_state structures over and over, wasting lots of time.
5131 *
5132 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5133 * those expensive operations on a per page basis and do only the ordered io
5134 * finishing, while we release here the extent_map and extent_state structures,
5135 * without the excessive merging and splitting.
5136 */
5137static void evict_inode_truncate_pages(struct inode *inode)
5138{
5139 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5140 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5141 struct rb_node *node;
5142
5143 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005144 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005145
5146 write_lock(&map_tree->lock);
5147 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5148 struct extent_map *em;
5149
5150 node = rb_first(&map_tree->map);
5151 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005152 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5153 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005154 remove_extent_mapping(map_tree, em);
5155 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005156 if (need_resched()) {
5157 write_unlock(&map_tree->lock);
5158 cond_resched();
5159 write_lock(&map_tree->lock);
5160 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005161 }
5162 write_unlock(&map_tree->lock);
5163
Filipe Manana6ca07092015-05-26 00:55:42 +01005164 /*
5165 * Keep looping until we have no more ranges in the io tree.
5166 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005167 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5168 * still in progress (unlocked the pages in the bio but did not yet
5169 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005170 * ranges can still be locked and eviction started because before
5171 * submitting those bios, which are executed by a separate task (work
5172 * queue kthread), inode references (inode->i_count) were not taken
5173 * (which would be dropped in the end io callback of each bio).
5174 * Therefore here we effectively end up waiting for those bios and
5175 * anyone else holding locked ranges without having bumped the inode's
5176 * reference count - if we don't do it, when they access the inode's
5177 * io_tree to unlock a range it may be too late, leading to an
5178 * use-after-free issue.
5179 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005180 spin_lock(&io_tree->lock);
5181 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5182 struct extent_state *state;
5183 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005184 u64 start;
5185 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005186
5187 node = rb_first(&io_tree->state);
5188 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005189 start = state->start;
5190 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005191 spin_unlock(&io_tree->lock);
5192
David Sterbaff13db42015-12-03 14:30:40 +01005193 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005194
5195 /*
5196 * If still has DELALLOC flag, the extent didn't reach disk,
5197 * and its reserved space won't be freed by delayed_ref.
5198 * So we need to free its reserved space here.
5199 * (Refer to comment in btrfs_invalidatepage, case 2)
5200 *
5201 * Note, end is the bytenr of last byte, so we need + 1 here.
5202 */
5203 if (state->state & EXTENT_DELALLOC)
5204 btrfs_qgroup_free_data(inode, start, end - start + 1);
5205
Filipe Manana6ca07092015-05-26 00:55:42 +01005206 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207 EXTENT_LOCKED | EXTENT_DIRTY |
5208 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5209 EXTENT_DEFRAG, 1, 1,
5210 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211
Filipe Manana7064dd52014-08-08 02:47:05 +01005212 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005213 spin_lock(&io_tree->lock);
5214 }
5215 spin_unlock(&io_tree->lock);
5216}
5217
Al Virobd555972010-06-07 11:35:40 -04005218void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005221 struct btrfs_trans_handle *trans;
5222 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005223 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005224 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005225 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005226 int ret;
5227
liubo1abe9b82011-03-24 11:18:59 +00005228 trace_btrfs_inode_evict(inode);
5229
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005230 if (!root) {
5231 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5232 return;
5233 }
5234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005235 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005236
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237 evict_inode_truncate_pages(inode);
5238
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005239 if (inode->i_nlink &&
5240 ((btrfs_root_refs(&root->root_item) != 0 &&
5241 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5242 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005243 goto no_delete;
5244
Chris Mason39279cc2007-06-12 06:35:45 -04005245 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005246 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005247 goto no_delete;
5248 }
Al Virobd555972010-06-07 11:35:40 -04005249 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005250 if (!special_file(inode->i_mode))
5251 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005252
Miao Xief6124962014-09-12 18:44:04 +08005253 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5254
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005256 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005257 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005258 goto no_delete;
5259 }
5260
Yan, Zheng76dda932009-09-21 16:00:26 -04005261 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005262 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5263 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005264 goto no_delete;
5265 }
5266
Nikolay Borisovaa790212017-01-10 20:35:40 +02005267 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005268 if (ret) {
5269 btrfs_orphan_del(NULL, inode);
5270 goto no_delete;
5271 }
5272
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005273 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005274 if (!rsv) {
5275 btrfs_orphan_del(NULL, inode);
5276 goto no_delete;
5277 }
Josef Bacik4a338542011-08-29 11:01:31 -04005278 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005279 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005281
Chris Masondbe674a2008-07-17 12:54:05 -04005282 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005283
Josef Bacik4289a662011-08-05 13:22:24 -04005284 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005285 * This is a bit simpler than btrfs_truncate since we've already
5286 * reserved our space for our orphan item in the unlink, so we just
5287 * need to reserve some slack space in case we add bytes and update
5288 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005289 */
Yan, Zheng80825102009-11-12 09:35:36 +00005290 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005291 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5292 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005293
Josef Bacik726c35f2011-09-26 15:46:06 -04005294 /*
5295 * Try and steal from the global reserve since we will
5296 * likely not use this space anyway, we want to try as
5297 * hard as possible to get this to work.
5298 */
5299 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005300 steal_from_global++;
5301 else
5302 steal_from_global = 0;
5303 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005304
Josef Bacik3bce8762015-02-24 12:35:51 -08005305 /*
5306 * steal_from_global == 0: we reserved stuff, hooray!
5307 * steal_from_global == 1: we didn't reserve stuff, boo!
5308 * steal_from_global == 2: we've committed, still not a lot of
5309 * room but maybe we'll have room in the global reserve this
5310 * time.
5311 * steal_from_global == 3: abandon all hope!
5312 */
5313 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005314 btrfs_warn(fs_info,
5315 "Could not get space for a delete, will truncate on mount %d",
5316 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005317 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005318 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005319 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005320 }
5321
Miao Xie0e8c36a2012-12-19 06:59:51 +00005322 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005323 if (IS_ERR(trans)) {
5324 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005325 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005326 goto no_delete;
5327 }
5328
Josef Bacik3bce8762015-02-24 12:35:51 -08005329 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005330 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005331 * sure there is room to do it, if not we need to commit and try
5332 * again.
5333 */
5334 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005335 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005336 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005337 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005338 else
5339 ret = -ENOSPC;
5340 }
5341
5342 /*
5343 * Couldn't steal from the global reserve, we have too much
5344 * pending stuff built up, commit the transaction and try it
5345 * again.
5346 */
5347 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005348 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005349 if (ret) {
5350 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005351 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005352 goto no_delete;
5353 }
5354 continue;
5355 } else {
5356 steal_from_global = 0;
5357 }
5358
Josef Bacik4289a662011-08-05 13:22:24 -04005359 trans->block_rsv = rsv;
5360
Yan, Zhengd68fc572010-05-16 10:49:58 -04005361 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005362 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005363 break;
5364
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005365 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005366 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005367 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005368 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005369 }
5370
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005371 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005372
Josef Bacik4ef31a42013-08-13 14:10:08 -04005373 /*
5374 * Errors here aren't a big deal, it just means we leave orphan items
5375 * in the tree. They will be cleaned up on the next mount.
5376 */
Yan, Zheng80825102009-11-12 09:35:36 +00005377 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005378 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005379 btrfs_orphan_del(trans, inode);
5380 } else {
5381 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005382 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005383
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005384 trans->block_rsv = &fs_info->trans_block_rsv;
5385 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005386 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005387 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005388
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005389 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005390 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005391no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005392 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005393 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005394}
5395
5396/*
5397 * this returns the key found in the dir entry in the location pointer.
5398 * If no dir entries were found, location->objectid is 0.
5399 */
5400static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5401 struct btrfs_key *location)
5402{
5403 const char *name = dentry->d_name.name;
5404 int namelen = dentry->d_name.len;
5405 struct btrfs_dir_item *di;
5406 struct btrfs_path *path;
5407 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005408 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005409
5410 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005411 if (!path)
5412 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005413
David Sterbaf85b7372017-01-20 14:54:07 +01005414 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5415 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005416 if (IS_ERR(di))
5417 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005418
David Sterbac7040052011-04-19 18:00:01 +02005419 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005420 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005421
Chris Mason5f39d392007-10-15 16:14:19 -04005422 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005423out:
Chris Mason39279cc2007-06-12 06:35:45 -04005424 btrfs_free_path(path);
5425 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005426out_err:
5427 location->objectid = 0;
5428 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005429}
5430
5431/*
5432 * when we hit a tree root in a directory, the btrfs part of the inode
5433 * needs to be changed to reflect the root directory of the tree root. This
5434 * is kind of like crossing a mount point.
5435 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005437 struct inode *dir,
5438 struct dentry *dentry,
5439 struct btrfs_key *location,
5440 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005441{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005442 struct btrfs_path *path;
5443 struct btrfs_root *new_root;
5444 struct btrfs_root_ref *ref;
5445 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005446 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005447 int ret;
5448 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005449
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005450 path = btrfs_alloc_path();
5451 if (!path) {
5452 err = -ENOMEM;
5453 goto out;
5454 }
Chris Mason39279cc2007-06-12 06:35:45 -04005455
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005456 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005457 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5458 key.type = BTRFS_ROOT_REF_KEY;
5459 key.offset = location->objectid;
5460
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005461 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005462 if (ret) {
5463 if (ret < 0)
5464 err = ret;
5465 goto out;
5466 }
Chris Mason39279cc2007-06-12 06:35:45 -04005467
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005468 leaf = path->nodes[0];
5469 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005470 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005471 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5472 goto out;
5473
5474 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5475 (unsigned long)(ref + 1),
5476 dentry->d_name.len);
5477 if (ret)
5478 goto out;
5479
David Sterbab3b4aa72011-04-21 01:20:15 +02005480 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005482 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 if (IS_ERR(new_root)) {
5484 err = PTR_ERR(new_root);
5485 goto out;
5486 }
5487
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488 *sub_root = new_root;
5489 location->objectid = btrfs_root_dirid(&new_root->root_item);
5490 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005491 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 err = 0;
5493out:
5494 btrfs_free_path(path);
5495 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005496}
5497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498static void inode_tree_add(struct inode *inode)
5499{
5500 struct btrfs_root *root = BTRFS_I(inode)->root;
5501 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005502 struct rb_node **p;
5503 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005504 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005505 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005506
Al Viro1d3382cb2010-10-23 15:19:20 -04005507 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005508 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005509 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005510 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005511 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005512 while (*p) {
5513 parent = *p;
5514 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5515
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005516 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005517 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005518 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005519 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520 else {
5521 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005522 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005523 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005524 RB_CLEAR_NODE(parent);
5525 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005526 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527 }
5528 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005529 rb_link_node(new, parent, p);
5530 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531 spin_unlock(&root->inode_lock);
5532}
5533
5534static void inode_tree_del(struct inode *inode)
5535{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005536 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005538 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005539
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005540 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005544 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005546 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005547
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005548 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005549 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005550 spin_lock(&root->inode_lock);
5551 empty = RB_EMPTY_ROOT(&root->inode_tree);
5552 spin_unlock(&root->inode_lock);
5553 if (empty)
5554 btrfs_add_dead_root(root);
5555 }
5556}
5557
Jeff Mahoney143bede2012-03-01 14:56:26 +01005558void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005560 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005561 struct rb_node *node;
5562 struct rb_node *prev;
5563 struct btrfs_inode *entry;
5564 struct inode *inode;
5565 u64 objectid = 0;
5566
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005567 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005568 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005569
5570 spin_lock(&root->inode_lock);
5571again:
5572 node = root->inode_tree.rb_node;
5573 prev = NULL;
5574 while (node) {
5575 prev = node;
5576 entry = rb_entry(node, struct btrfs_inode, rb_node);
5577
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005578 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005580 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005581 node = node->rb_right;
5582 else
5583 break;
5584 }
5585 if (!node) {
5586 while (prev) {
5587 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005588 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005589 node = prev;
5590 break;
5591 }
5592 prev = rb_next(prev);
5593 }
5594 }
5595 while (node) {
5596 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005597 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 inode = igrab(&entry->vfs_inode);
5599 if (inode) {
5600 spin_unlock(&root->inode_lock);
5601 if (atomic_read(&inode->i_count) > 1)
5602 d_prune_aliases(inode);
5603 /*
Al Viro45321ac2010-06-07 13:43:19 -04005604 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005605 * the inode cache when its usage count
5606 * hits zero.
5607 */
5608 iput(inode);
5609 cond_resched();
5610 spin_lock(&root->inode_lock);
5611 goto again;
5612 }
5613
5614 if (cond_resched_lock(&root->inode_lock))
5615 goto again;
5616
5617 node = rb_next(node);
5618 }
5619 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620}
5621
Chris Masone02119d2008-09-05 16:13:11 -04005622static int btrfs_init_locked_inode(struct inode *inode, void *p)
5623{
5624 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005625 inode->i_ino = args->location->objectid;
5626 memcpy(&BTRFS_I(inode)->location, args->location,
5627 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005628 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005629 return 0;
5630}
5631
5632static int btrfs_find_actor(struct inode *inode, void *opaque)
5633{
5634 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005635 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005636 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005637}
5638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005640 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005641 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005642{
5643 struct inode *inode;
5644 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005645 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005646
Chris Mason90d3e592014-01-09 17:28:00 -08005647 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005648 args.root = root;
5649
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005650 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005651 btrfs_init_locked_inode,
5652 (void *)&args);
5653 return inode;
5654}
5655
Balaji Rao1a54ef82008-07-21 02:01:04 +05305656/* Get an inode object given its location and corresponding root.
5657 * Returns in *is_new if the inode was read from disk
5658 */
5659struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005660 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305661{
5662 struct inode *inode;
5663
Chris Mason90d3e592014-01-09 17:28:00 -08005664 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305665 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005666 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305667
5668 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005669 int ret;
5670
5671 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005672 if (!is_bad_inode(inode)) {
5673 inode_tree_add(inode);
5674 unlock_new_inode(inode);
5675 if (new)
5676 *new = 1;
5677 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005678 unlock_new_inode(inode);
5679 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005680 ASSERT(ret < 0);
5681 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005682 }
5683 }
5684
Balaji Rao1a54ef82008-07-21 02:01:04 +05305685 return inode;
5686}
5687
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005688static struct inode *new_simple_dir(struct super_block *s,
5689 struct btrfs_key *key,
5690 struct btrfs_root *root)
5691{
5692 struct inode *inode = new_inode(s);
5693
5694 if (!inode)
5695 return ERR_PTR(-ENOMEM);
5696
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005697 BTRFS_I(inode)->root = root;
5698 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005699 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005700
5701 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005702 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005703 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005704 inode->i_fop = &simple_dir_operations;
5705 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005706 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305707 inode->i_atime = inode->i_mtime;
5708 inode->i_ctime = inode->i_mtime;
5709 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005710
5711 return inode;
5712}
5713
Chris Mason3de45862008-11-17 21:02:50 -05005714struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005715{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005716 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005717 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005719 struct btrfs_root *sub_root = root;
5720 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005721 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005722 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005723
5724 if (dentry->d_name.len > BTRFS_NAME_LEN)
5725 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005726
Jeff Layton39e3c952012-11-28 11:30:53 -05005727 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005728 if (ret < 0)
5729 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005732 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005733
5734 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005735 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005737 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005738
5739 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5740
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005741 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005742 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 &location, &sub_root);
5744 if (ret < 0) {
5745 if (ret != -ENOENT)
5746 inode = ERR_PTR(ret);
5747 else
5748 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5749 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005750 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005751 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005752 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005753
Julia Lawall34d19ba2011-01-24 19:55:19 +00005754 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005756 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005757 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005758 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005759 if (ret) {
5760 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005761 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005762 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005763 }
5764
Chris Mason3de45862008-11-17 21:02:50 -05005765 return inode;
5766}
5767
Nick Pigginfe15ce42011-01-07 17:49:23 +11005768static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005769{
5770 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005771 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005772
Li Zefan848cce02012-02-21 17:04:28 +08005773 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005774 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005775
Li Zefan848cce02012-02-21 17:04:28 +08005776 if (inode) {
5777 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005778 if (btrfs_root_refs(&root->root_item) == 0)
5779 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005780
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005781 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005782 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005783 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005784 return 0;
5785}
5786
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005787static void btrfs_dentry_release(struct dentry *dentry)
5788{
Daeseok Youn944a4512014-04-14 15:37:02 +09005789 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005790}
5791
Chris Mason3de45862008-11-17 21:02:50 -05005792static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005793 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005794{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005795 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005796
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005797 inode = btrfs_lookup_dentry(dir, dentry);
5798 if (IS_ERR(inode)) {
5799 if (PTR_ERR(inode) == -ENOENT)
5800 inode = NULL;
5801 else
5802 return ERR_CAST(inode);
5803 }
5804
Al Viro41d28bc2014-10-12 22:24:21 -04005805 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005806}
5807
Miao Xie16cdcec2011-04-22 18:12:22 +08005808unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005809 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5810};
5811
Al Viro9cdda8d2013-05-22 16:48:09 -04005812static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005813{
Al Viro9cdda8d2013-05-22 16:48:09 -04005814 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005816 struct btrfs_root *root = BTRFS_I(inode)->root;
5817 struct btrfs_item *item;
5818 struct btrfs_dir_item *di;
5819 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005820 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005821 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005822 struct list_head ins_list;
5823 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005825 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005826 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005827 unsigned char d_type;
5828 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 char tmp_name[32];
5830 char *name_ptr;
5831 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005832 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005833 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005834
Al Viro9cdda8d2013-05-22 16:48:09 -04005835 if (!dir_emit_dots(file, ctx))
5836 return 0;
5837
David Woodhouse49593bf2008-08-17 17:08:36 +01005838 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005839 if (!path)
5840 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005841
David Sterbae4058b52015-11-27 16:31:35 +01005842 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005843
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005844 INIT_LIST_HEAD(&ins_list);
5845 INIT_LIST_HEAD(&del_list);
5846 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005847
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005848 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005850 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Chris Mason39279cc2007-06-12 06:35:45 -04005852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5853 if (ret < 0)
5854 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005855
5856 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005857 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005858 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005859 if (slot >= btrfs_header_nritems(leaf)) {
5860 ret = btrfs_next_leaf(root, path);
5861 if (ret < 0)
5862 goto err;
5863 else if (ret > 0)
5864 break;
5865 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005866 }
Chris Mason3de45862008-11-17 21:02:50 -05005867
Ross Kirkdd3cc162013-09-16 15:58:09 +01005868 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005869 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5870
5871 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005872 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005873 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005874 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005875 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005876 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005877 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005878 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005879
Al Viro9cdda8d2013-05-22 16:48:09 -04005880 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005881
Chris Mason39279cc2007-06-12 06:35:45 -04005882 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005883 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005884 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005885
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005886 name_len = btrfs_dir_name_len(leaf, di);
5887 if (name_len <= sizeof(tmp_name)) {
5888 name_ptr = tmp_name;
5889 } else {
5890 name_ptr = kmalloc(name_len, GFP_KERNEL);
5891 if (!name_ptr) {
5892 ret = -ENOMEM;
5893 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005894 }
Chris Mason39279cc2007-06-12 06:35:45 -04005895 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005896 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5897 name_len);
5898
5899 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5900 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5901
5902 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5903 d_type);
5904
5905 if (name_ptr != tmp_name)
5906 kfree(name_ptr);
5907
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005908 if (over)
5909 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005910 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005911next:
5912 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005913 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005914
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005915 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005916 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005917 goto nopos;
5918
Zach Browndb62efb2013-07-11 16:19:42 -07005919 /*
5920 * Stop new entries from being returned after we return the last
5921 * entry.
5922 *
5923 * New directory entries are assigned a strictly increasing
5924 * offset. This means that new entries created during readdir
5925 * are *guaranteed* to be seen in the future by that readdir.
5926 * This has broken buggy programs which operate on names as
5927 * they're returned by readdir. Until we re-use freed offsets
5928 * we have this hack to stop new entries from being returned
5929 * under the assumption that they'll never reach this huge
5930 * offset.
5931 *
5932 * This is being careful not to overflow 32bit loff_t unless the
5933 * last entry requires it because doing so has broken 32bit apps
5934 * in the past.
5935 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005936 if (ctx->pos >= INT_MAX)
5937 ctx->pos = LLONG_MAX;
5938 else
5939 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005940nopos:
5941 ret = 0;
5942err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005943 if (put)
5944 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005945 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005946 return ret;
5947}
5948
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005949int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005950{
5951 struct btrfs_root *root = BTRFS_I(inode)->root;
5952 struct btrfs_trans_handle *trans;
5953 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005954 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005955
Josef Bacik72ac3c02012-05-23 14:13:11 -04005956 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005957 return 0;
5958
Liu Bo83eea1f2012-07-10 05:28:39 -06005959 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005960 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005961
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005962 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005963 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005964 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005965 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005966 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005967 if (IS_ERR(trans))
5968 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005969 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005970 }
5971 return ret;
5972}
5973
5974/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005975 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005976 * inode changes. But, it is most likely to find the inode in cache.
5977 * FIXME, needs more benchmarking...there are no reasons other than performance
5978 * to keep or drop this code.
5979 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005980static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005981{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005983 struct btrfs_root *root = BTRFS_I(inode)->root;
5984 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005985 int ret;
5986
Josef Bacik72ac3c02012-05-23 14:13:11 -04005987 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005988 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005989
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005990 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005991 if (IS_ERR(trans))
5992 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005993
5994 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005995 if (ret && ret == -ENOSPC) {
5996 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005997 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005998 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005999 if (IS_ERR(trans))
6000 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006001
Chris Mason94b60442010-05-26 11:02:00 -04006002 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006003 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006004 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006005 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006006 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006007
6008 return ret;
6009}
6010
6011/*
6012 * This is a copy of file_update_time. We need this so we can return error on
6013 * ENOSPC for updating the inode in the case of file write and mmap writes.
6014 */
Josef Bacike41f9412012-03-26 09:46:47 -04006015static int btrfs_update_time(struct inode *inode, struct timespec *now,
6016 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006017{
Alexander Block2bc5565282012-06-15 09:49:33 +02006018 struct btrfs_root *root = BTRFS_I(inode)->root;
6019
6020 if (btrfs_root_readonly(root))
6021 return -EROFS;
6022
Josef Bacike41f9412012-03-26 09:46:47 -04006023 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006024 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006025 if (flags & S_CTIME)
6026 inode->i_ctime = *now;
6027 if (flags & S_MTIME)
6028 inode->i_mtime = *now;
6029 if (flags & S_ATIME)
6030 inode->i_atime = *now;
6031 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006032}
6033
Chris Masond352ac62008-09-29 15:18:18 -04006034/*
6035 * find the highest existing sequence number in a directory
6036 * and then set the in-memory index_cnt variable to reflect
6037 * free sequence numbers
6038 */
Josef Bacikaec74772008-07-24 12:12:38 -04006039static int btrfs_set_inode_index_count(struct inode *inode)
6040{
6041 struct btrfs_root *root = BTRFS_I(inode)->root;
6042 struct btrfs_key key, found_key;
6043 struct btrfs_path *path;
6044 struct extent_buffer *leaf;
6045 int ret;
6046
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006047 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +02006048 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006049 key.offset = (u64)-1;
6050
6051 path = btrfs_alloc_path();
6052 if (!path)
6053 return -ENOMEM;
6054
6055 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6056 if (ret < 0)
6057 goto out;
6058 /* FIXME: we should be able to handle this */
6059 if (ret == 0)
6060 goto out;
6061 ret = 0;
6062
6063 /*
6064 * MAGIC NUMBER EXPLANATION:
6065 * since we search a directory based on f_pos we have to start at 2
6066 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6067 * else has to start at 2
6068 */
6069 if (path->slots[0] == 0) {
6070 BTRFS_I(inode)->index_cnt = 2;
6071 goto out;
6072 }
6073
6074 path->slots[0]--;
6075
6076 leaf = path->nodes[0];
6077 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6078
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006079 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
David Sterba962a2982014-06-04 18:41:45 +02006080 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006081 BTRFS_I(inode)->index_cnt = 2;
6082 goto out;
6083 }
6084
6085 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6086out:
6087 btrfs_free_path(path);
6088 return ret;
6089}
6090
Chris Masond352ac62008-09-29 15:18:18 -04006091/*
6092 * helper to find a free sequence number in a given directory. This current
6093 * code is very simple, later versions will do smarter things in the btree
6094 */
Chris Mason3de45862008-11-17 21:02:50 -05006095int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006096{
6097 int ret = 0;
6098
6099 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02006100 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
Miao Xie16cdcec2011-04-22 18:12:22 +08006101 if (ret) {
6102 ret = btrfs_set_inode_index_count(dir);
6103 if (ret)
6104 return ret;
6105 }
Josef Bacikaec74772008-07-24 12:12:38 -04006106 }
6107
Chris Mason00e4e6b2008-08-05 11:18:09 -04006108 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006109 BTRFS_I(dir)->index_cnt++;
6110
6111 return ret;
6112}
6113
Chris Masonb0d5d102014-09-08 13:08:51 -07006114static int btrfs_insert_inode_locked(struct inode *inode)
6115{
6116 struct btrfs_iget_args args;
6117 args.location = &BTRFS_I(inode)->location;
6118 args.root = BTRFS_I(inode)->root;
6119
6120 return insert_inode_locked4(inode,
6121 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6122 btrfs_find_actor, &args);
6123}
6124
Chris Mason39279cc2007-06-12 06:35:45 -04006125static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6126 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006127 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006128 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006129 u64 ref_objectid, u64 objectid,
6130 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006131{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006132 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006133 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006134 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006135 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006136 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006137 struct btrfs_inode_ref *ref;
6138 struct btrfs_key key[2];
6139 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006140 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006141 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006142 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006143
Chris Mason5f39d392007-10-15 16:14:19 -04006144 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006145 if (!path)
6146 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006147
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006148 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006149 if (!inode) {
6150 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006151 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006152 }
Chris Mason39279cc2007-06-12 06:35:45 -04006153
Li Zefan581bb052011-04-20 10:06:11 +08006154 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006155 * O_TMPFILE, set link count to 0, so that after this point,
6156 * we fill in an inode item with the correct link count.
6157 */
6158 if (!name)
6159 set_nlink(inode, 0);
6160
6161 /*
Li Zefan581bb052011-04-20 10:06:11 +08006162 * we have to initialize this early, so we can reclaim the inode
6163 * number if we fail afterwards in this function.
6164 */
6165 inode->i_ino = objectid;
6166
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006167 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006168 trace_btrfs_inode_request(dir);
6169
Chris Mason3de45862008-11-17 21:02:50 -05006170 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006171 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006172 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006173 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006174 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006175 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006176 } else if (dir) {
6177 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006178 }
6179 /*
6180 * index_cnt is ignored for everything but a dir,
6181 * btrfs_get_inode_index_count has an explanation for the magic
6182 * number
6183 */
6184 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006185 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006186 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006187 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006188 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006189
Josef Bacik5dc562c2012-08-17 13:14:17 -04006190 /*
6191 * We could have gotten an inode number from somebody who was fsynced
6192 * and then removed in this same transaction, so let's just set full
6193 * sync since it will be a full sync anyway and this will blow away the
6194 * old info in the log.
6195 */
6196 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6197
Chris Mason9c583092008-01-29 15:15:18 -05006198 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006199 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006200 key[0].offset = 0;
6201
Chris Mason9c583092008-01-29 15:15:18 -05006202 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006203
6204 if (name) {
6205 /*
6206 * Start new inodes with an inode_ref. This is slightly more
6207 * efficient for small numbers of hard links since they will
6208 * be packed into one item. Extended refs will kick in if we
6209 * add more hard links than can fit in the ref item.
6210 */
6211 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006212 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006213 key[1].offset = ref_objectid;
6214
6215 sizes[1] = name_len + sizeof(*ref);
6216 }
Chris Mason9c583092008-01-29 15:15:18 -05006217
Chris Masonb0d5d102014-09-08 13:08:51 -07006218 location = &BTRFS_I(inode)->location;
6219 location->objectid = objectid;
6220 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006221 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006222
6223 ret = btrfs_insert_inode_locked(inode);
6224 if (ret < 0)
6225 goto fail;
6226
Chris Masonb9473432009-03-13 11:00:37 -04006227 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006228 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006229 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006230 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006231
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006232 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006233 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306234
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006235 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306236 inode->i_atime = inode->i_mtime;
6237 inode->i_ctime = inode->i_mtime;
6238 BTRFS_I(inode)->i_otime = inode->i_mtime;
6239
Chris Mason5f39d392007-10-15 16:14:19 -04006240 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6241 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006242 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006243 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006244 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006245
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006246 if (name) {
6247 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6248 struct btrfs_inode_ref);
6249 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6250 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6251 ptr = (unsigned long)(ref + 1);
6252 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6253 }
Chris Mason9c583092008-01-29 15:15:18 -05006254
Chris Mason5f39d392007-10-15 16:14:19 -04006255 btrfs_mark_buffer_dirty(path->nodes[0]);
6256 btrfs_free_path(path);
6257
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006258 btrfs_inherit_iflags(inode, dir);
6259
Al Viro569254b2011-07-24 17:08:40 -04006260 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006261 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006262 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006263 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006264 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6265 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006266 }
6267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006268 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006269
6270 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006271 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006272
Alexander Block8ea05e32012-07-25 17:35:53 +02006273 btrfs_update_root_times(trans, root);
6274
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006275 ret = btrfs_inode_inherit_props(trans, inode, dir);
6276 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006278 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006279 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006280
Chris Mason39279cc2007-06-12 06:35:45 -04006281 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006282
6283fail_unlock:
6284 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006285fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006286 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006287 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006288 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006289 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006290 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006291}
6292
6293static inline u8 btrfs_inode_type(struct inode *inode)
6294{
6295 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6296}
6297
Chris Masond352ac62008-09-29 15:18:18 -04006298/*
6299 * utility function to add 'inode' into 'parent_inode' with
6300 * a give name and a given sequence number.
6301 * if 'add_backref' is true, also insert a backref from the
6302 * inode to the parent directory.
6303 */
Chris Masone02119d2008-09-05 16:13:11 -04006304int btrfs_add_link(struct btrfs_trans_handle *trans,
6305 struct inode *parent_inode, struct inode *inode,
6306 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006309 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006310 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006311 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006312 u64 ino = btrfs_ino(BTRFS_I(inode));
6313 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006314
Li Zefan33345d012011-04-20 10:31:50 +08006315 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006316 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6317 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006318 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006319 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006320 key.offset = 0;
6321 }
Chris Mason39279cc2007-06-12 06:35:45 -04006322
Li Zefan33345d012011-04-20 10:31:50 +08006323 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006324 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6325 root->root_key.objectid, parent_ino,
6326 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006327 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006328 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6329 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006330 }
6331
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006332 /* Nothing to clean up yet */
6333 if (ret)
6334 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006335
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006336 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6337 parent_inode, &key,
6338 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006339 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006340 goto fail_dir_item;
6341 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006342 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006343 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006344 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006345
6346 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6347 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006348 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006349 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006350 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006351 ret = btrfs_update_inode(trans, root, parent_inode);
6352 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006353 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006354 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006355
6356fail_dir_item:
6357 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6358 u64 local_index;
6359 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006360 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6361 root->root_key.objectid, parent_ino,
6362 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006363
6364 } else if (add_backref) {
6365 u64 local_index;
6366 int err;
6367
6368 err = btrfs_del_inode_ref(trans, root, name, name_len,
6369 ino, parent_ino, &local_index);
6370 }
6371 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006372}
6373
6374static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006375 struct inode *dir, struct dentry *dentry,
6376 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006377{
Josef Bacika1b075d2010-11-19 20:36:11 +00006378 int err = btrfs_add_link(trans, dir, inode,
6379 dentry->d_name.name, dentry->d_name.len,
6380 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006381 if (err > 0)
6382 err = -EEXIST;
6383 return err;
6384}
6385
Josef Bacik618e21d2007-07-11 10:18:17 -04006386static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006387 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006388{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006389 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006390 struct btrfs_trans_handle *trans;
6391 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006392 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006393 int err;
6394 int drop_inode = 0;
6395 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006396 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006397
Josef Bacik9ed74f22009-09-11 16:12:44 -04006398 /*
6399 * 2 for inode item and ref
6400 * 2 for dir items
6401 * 1 for xattr if selinux is on
6402 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006403 trans = btrfs_start_transaction(root, 5);
6404 if (IS_ERR(trans))
6405 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006406
Li Zefan581bb052011-04-20 10:06:11 +08006407 err = btrfs_find_free_ino(root, &objectid);
6408 if (err)
6409 goto out_unlock;
6410
Josef Bacikaec74772008-07-24 12:12:38 -04006411 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006412 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6413 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006414 if (IS_ERR(inode)) {
6415 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006416 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006417 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006418
Casey Schauflerad19db72011-12-15 10:09:07 -05006419 /*
6420 * If the active LSM wants to access the inode during
6421 * d_instantiate it needs these. Smack checks to see
6422 * if the filesystem supports xattrs by looking at the
6423 * ops vector.
6424 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006425 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006426 init_special_inode(inode, inode->i_mode, rdev);
6427
6428 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006429 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006430 goto out_unlock_inode;
6431
6432 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6433 if (err) {
6434 goto out_unlock_inode;
6435 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006436 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006437 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006438 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006439 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006440
Josef Bacik618e21d2007-07-11 10:18:17 -04006441out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006442 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006443 btrfs_balance_delayed_items(fs_info);
6444 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006445 if (drop_inode) {
6446 inode_dec_link_count(inode);
6447 iput(inode);
6448 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006449 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006450
6451out_unlock_inode:
6452 drop_inode = 1;
6453 unlock_new_inode(inode);
6454 goto out_unlock;
6455
Josef Bacik618e21d2007-07-11 10:18:17 -04006456}
6457
Chris Mason39279cc2007-06-12 06:35:45 -04006458static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006459 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006460{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006462 struct btrfs_trans_handle *trans;
6463 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006464 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006465 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006466 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006467 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006468 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006469
Josef Bacik9ed74f22009-09-11 16:12:44 -04006470 /*
6471 * 2 for inode item and ref
6472 * 2 for dir items
6473 * 1 for xattr if selinux is on
6474 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006475 trans = btrfs_start_transaction(root, 5);
6476 if (IS_ERR(trans))
6477 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006478
Li Zefan581bb052011-04-20 10:06:11 +08006479 err = btrfs_find_free_ino(root, &objectid);
6480 if (err)
6481 goto out_unlock;
6482
Josef Bacikaec74772008-07-24 12:12:38 -04006483 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006484 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6485 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006486 if (IS_ERR(inode)) {
6487 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006488 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006489 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006490 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006491 /*
6492 * If the active LSM wants to access the inode during
6493 * d_instantiate it needs these. Smack checks to see
6494 * if the filesystem supports xattrs by looking at the
6495 * ops vector.
6496 */
6497 inode->i_fop = &btrfs_file_operations;
6498 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006499 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006500
6501 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6502 if (err)
6503 goto out_unlock_inode;
6504
6505 err = btrfs_update_inode(trans, root, inode);
6506 if (err)
6507 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006508
Josef Bacika1b075d2010-11-19 20:36:11 +00006509 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006510 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006511 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006512
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006513 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006514 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006515 d_instantiate(dentry, inode);
6516
Chris Mason39279cc2007-06-12 06:35:45 -04006517out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006518 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006519 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006520 inode_dec_link_count(inode);
6521 iput(inode);
6522 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006523 btrfs_balance_delayed_items(fs_info);
6524 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006525 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006526
6527out_unlock_inode:
6528 unlock_new_inode(inode);
6529 goto out_unlock;
6530
Chris Mason39279cc2007-06-12 06:35:45 -04006531}
6532
6533static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6534 struct dentry *dentry)
6535{
Filipe Manana271dba452016-01-05 16:24:05 +00006536 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006537 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006538 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006540 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006541 int err;
6542 int drop_inode = 0;
6543
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006544 /* do not allow sys_link's with other subvols of the same device */
6545 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006546 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006547
Mark Fashehf1863732012-08-08 11:32:27 -07006548 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006549 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006550
Chris Mason3de45862008-11-17 21:02:50 -05006551 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006552 if (err)
6553 goto fail;
6554
Yan, Zhenga22285a2010-05-16 10:48:46 -04006555 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006556 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006557 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006558 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006559 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006560 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006561 if (IS_ERR(trans)) {
6562 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006563 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006564 goto fail;
6565 }
Chris Mason5f39d392007-10-15 16:14:19 -04006566
Miao Xie67de1172013-12-26 13:07:06 +08006567 /* There are several dir indexes for this inode, clear the cache. */
6568 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006569 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006570 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006571 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006572 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006573 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006574
Josef Bacika1b075d2010-11-19 20:36:11 +00006575 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006576
Yan, Zhenga5719522009-09-24 09:17:31 -04006577 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006578 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006579 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006580 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006581 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006582 if (err)
6583 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006584 if (inode->i_nlink == 1) {
6585 /*
6586 * If new hard link count is 1, it's a file created
6587 * with open(2) O_TMPFILE flag.
6588 */
6589 err = btrfs_orphan_del(trans, inode);
6590 if (err)
6591 goto fail;
6592 }
Al Viro08c422c2011-12-23 07:58:13 -05006593 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006594 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006595 }
Chris Mason39279cc2007-06-12 06:35:45 -04006596
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006597 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006598fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006599 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006600 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 if (drop_inode) {
6602 inode_dec_link_count(inode);
6603 iput(inode);
6604 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006605 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 return err;
6607}
6608
Al Viro18bb1db2011-07-26 01:41:39 -04006609static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006610{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006611 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006612 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006613 struct btrfs_trans_handle *trans;
6614 struct btrfs_root *root = BTRFS_I(dir)->root;
6615 int err = 0;
6616 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006617 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006618 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006619
Josef Bacik9ed74f22009-09-11 16:12:44 -04006620 /*
6621 * 2 items for inode and ref
6622 * 2 items for dir items
6623 * 1 for xattr if selinux is on
6624 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006625 trans = btrfs_start_transaction(root, 5);
6626 if (IS_ERR(trans))
6627 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006628
Li Zefan581bb052011-04-20 10:06:11 +08006629 err = btrfs_find_free_ino(root, &objectid);
6630 if (err)
6631 goto out_fail;
6632
Josef Bacikaec74772008-07-24 12:12:38 -04006633 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006634 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6635 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006636 if (IS_ERR(inode)) {
6637 err = PTR_ERR(inode);
6638 goto out_fail;
6639 }
Chris Mason5f39d392007-10-15 16:14:19 -04006640
Chris Mason39279cc2007-06-12 06:35:45 -04006641 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006642 /* these must be set before we unlock the inode */
6643 inode->i_op = &btrfs_dir_inode_operations;
6644 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006645
Eric Paris2a7dba32011-02-01 11:05:39 -05006646 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006647 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006648 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006649
Chris Masondbe674a2008-07-17 12:54:05 -04006650 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006651 err = btrfs_update_inode(trans, root, inode);
6652 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006653 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006654
Josef Bacika1b075d2010-11-19 20:36:11 +00006655 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6656 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006657 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006658 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006659
Chris Mason39279cc2007-06-12 06:35:45 -04006660 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006661 /*
6662 * mkdir is special. We're unlocking after we call d_instantiate
6663 * to avoid a race with nfsd calling d_instantiate.
6664 */
6665 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006666 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006667
6668out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006669 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006670 if (drop_on_err) {
6671 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006672 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006673 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674 btrfs_balance_delayed_items(fs_info);
6675 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006676 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006677
6678out_fail_inode:
6679 unlock_new_inode(inode);
6680 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006681}
6682
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006683/* Find next extent map of a given extent map, caller needs to ensure locks */
6684static struct extent_map *next_extent_map(struct extent_map *em)
6685{
6686 struct rb_node *next;
6687
6688 next = rb_next(&em->rb_node);
6689 if (!next)
6690 return NULL;
6691 return container_of(next, struct extent_map, rb_node);
6692}
6693
6694static struct extent_map *prev_extent_map(struct extent_map *em)
6695{
6696 struct rb_node *prev;
6697
6698 prev = rb_prev(&em->rb_node);
6699 if (!prev)
6700 return NULL;
6701 return container_of(prev, struct extent_map, rb_node);
6702}
6703
Chris Masond352ac62008-09-29 15:18:18 -04006704/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006705 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006706 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006707 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006708 */
Chris Mason3b951512008-04-17 11:29:12 -04006709static int merge_extent_mapping(struct extent_map_tree *em_tree,
6710 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006711 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006712 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006713{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006714 struct extent_map *prev;
6715 struct extent_map *next;
6716 u64 start;
6717 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006718 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006719
Chris Masone6dcd2d2008-07-17 12:53:50 -04006720 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006721
6722 if (existing->start > map_start) {
6723 next = existing;
6724 prev = prev_extent_map(next);
6725 } else {
6726 prev = existing;
6727 next = next_extent_map(prev);
6728 }
6729
6730 start = prev ? extent_map_end(prev) : em->start;
6731 start = max_t(u64, start, em->start);
6732 end = next ? next->start : extent_map_end(em);
6733 end = min_t(u64, end, extent_map_end(em));
6734 start_diff = start - em->start;
6735 em->start = start;
6736 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006737 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6738 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006739 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006740 em->block_len -= start_diff;
6741 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006742 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006743}
6744
Chris Masonc8b97812008-10-29 14:49:59 -04006745static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006746 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006747 size_t pg_offset, u64 extent_offset,
6748 struct btrfs_file_extent_item *item)
6749{
6750 int ret;
6751 struct extent_buffer *leaf = path->nodes[0];
6752 char *tmp;
6753 size_t max_size;
6754 unsigned long inline_size;
6755 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006756 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006757
6758 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006759 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006760 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6761 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006762 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006763 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006764 if (!tmp)
6765 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006766 ptr = btrfs_file_extent_inline_start(item);
6767
6768 read_extent_buffer(leaf, tmp, ptr, inline_size);
6769
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006770 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006771 ret = btrfs_decompress(compress_type, tmp, page,
6772 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006773 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006774 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006775}
6776
Chris Masond352ac62008-09-29 15:18:18 -04006777/*
6778 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006779 * the ugly parts come from merging extents from the disk with the in-ram
6780 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006781 * where the in-ram extents might be locked pending data=ordered completion.
6782 *
6783 * This also copies inline extents directly into the page.
6784 */
Chris Masond3977122009-01-05 21:25:51 -05006785
Chris Masona52d9a82007-08-27 16:49:44 -04006786struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006787 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006788 int create)
6789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006790 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006791 int ret;
6792 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006793 u64 extent_start = 0;
6794 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006795 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006796 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006797 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006798 struct btrfs_root *root = BTRFS_I(inode)->root;
6799 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006800 struct extent_buffer *leaf;
6801 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006802 struct extent_map *em = NULL;
6803 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006804 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006805 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006806 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006807
Chris Masona52d9a82007-08-27 16:49:44 -04006808again:
Chris Mason890871b2009-09-02 16:24:52 -04006809 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006810 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006811 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006812 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006813 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006814
Chris Masona52d9a82007-08-27 16:49:44 -04006815 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006816 if (em->start > start || em->start + em->len <= start)
6817 free_extent_map(em);
6818 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006819 free_extent_map(em);
6820 else
6821 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006822 }
David Sterba172ddd62011-04-21 00:48:27 +02006823 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006824 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006825 err = -ENOMEM;
6826 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006827 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006828 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006829 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006830 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006831 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006832 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006833
6834 if (!path) {
6835 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006836 if (!path) {
6837 err = -ENOMEM;
6838 goto out;
6839 }
6840 /*
6841 * Chances are we'll be called again, so go ahead and do
6842 * readahead
6843 */
David Sterbae4058b52015-11-27 16:31:35 +01006844 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006845 }
6846
Chris Mason179e29e2007-11-01 11:28:41 -04006847 ret = btrfs_lookup_file_extent(trans, root, path,
6848 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006849 if (ret < 0) {
6850 err = ret;
6851 goto out;
6852 }
6853
6854 if (ret != 0) {
6855 if (path->slots[0] == 0)
6856 goto not_found;
6857 path->slots[0]--;
6858 }
6859
Chris Mason5f39d392007-10-15 16:14:19 -04006860 leaf = path->nodes[0];
6861 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006862 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006863 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006864 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006865 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006866 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006867 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006868 /*
6869 * If we backup past the first extent we want to move forward
6870 * and see if there is an extent in front of us, otherwise we'll
6871 * say there is a hole for our whole search range which can
6872 * cause problems.
6873 */
6874 extent_end = start;
6875 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006876 }
6877
Chris Mason5f39d392007-10-15 16:14:19 -04006878 found_type = btrfs_file_extent_type(leaf, item);
6879 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006880 if (found_type == BTRFS_FILE_EXTENT_REG ||
6881 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006882 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006883 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006884 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6885 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006886 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006887 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006888 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006889 }
Josef Bacik25a50342013-10-14 12:08:38 -04006890next:
Yan Zheng9036c102008-10-30 14:19:41 -04006891 if (start >= extent_end) {
6892 path->slots[0]++;
6893 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6894 ret = btrfs_next_leaf(root, path);
6895 if (ret < 0) {
6896 err = ret;
6897 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006898 }
Yan Zheng9036c102008-10-30 14:19:41 -04006899 if (ret > 0)
6900 goto not_found;
6901 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006902 }
Yan Zheng9036c102008-10-30 14:19:41 -04006903 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6904 if (found_key.objectid != objectid ||
6905 found_key.type != BTRFS_EXTENT_DATA_KEY)
6906 goto not_found;
6907 if (start + len <= found_key.offset)
6908 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006909 if (start > found_key.offset)
6910 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006911 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006912 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006913 em->len = found_key.offset - start;
6914 goto not_found_em;
6915 }
6916
Filipe Manana7ffbb592014-06-09 03:48:05 +01006917 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6918
Yan Zhengd899e052008-10-30 14:25:28 -04006919 if (found_type == BTRFS_FILE_EXTENT_REG ||
6920 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006921 goto insert;
6922 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006923 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006925 size_t size;
6926 size_t extent_offset;
6927 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006928
Filipe Manana7ffbb592014-06-09 03:48:05 +01006929 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006930 goto out;
Yan689f9342007-10-29 11:41:07 -04006931
Chris Mason514ac8a2014-01-03 21:07:00 -08006932 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006933 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006934 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6935 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006936 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006937 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006938 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006939 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006940 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006941 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006942 if (btrfs_file_extent_compression(leaf, item) !=
6943 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006944 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006945 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006946 if (ret) {
6947 err = ret;
6948 goto out;
6949 }
Chris Masonc8b97812008-10-29 14:49:59 -04006950 } else {
6951 map = kmap(page);
6952 read_extent_buffer(leaf, map + pg_offset, ptr,
6953 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006954 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006955 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006956 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006957 copy_size);
6958 }
Chris Masonc8b97812008-10-29 14:49:59 -04006959 kunmap(page);
6960 }
Chris Mason179e29e2007-11-01 11:28:41 -04006961 flush_dcache_page(page);
6962 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006963 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006964 if (!trans) {
6965 kunmap(page);
6966 free_extent_map(em);
6967 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006968
David Sterbab3b4aa72011-04-21 01:20:15 +02006969 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006970 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006971
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006972 if (IS_ERR(trans))
6973 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006974 goto again;
6975 }
Chris Masonc8b97812008-10-29 14:49:59 -04006976 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006977 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006978 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006979 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006980 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006981 }
Chris Masond1310b22008-01-24 16:13:08 -05006982 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006983 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006984 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006985 }
6986not_found:
6987 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006988 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006989 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006990not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006991 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006992 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006993insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006994 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006995 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006996 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006997 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6998 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006999 err = -EIO;
7000 goto out;
7001 }
Chris Masond1310b22008-01-24 16:13:08 -05007002
7003 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007004 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007005 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007006 /* it is possible that someone inserted the extent into the tree
7007 * while we had the lock dropped. It is also possible that
7008 * an overlapping map exists in the tree
7009 */
Chris Masona52d9a82007-08-27 16:49:44 -04007010 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007011 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007012
7013 ret = 0;
7014
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007015 existing = search_extent_mapping(em_tree, start, len);
7016 /*
7017 * existing will always be non-NULL, since there must be
7018 * extent causing the -EEXIST.
7019 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007020 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007021 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007022 em->block_start == existing->block_start) {
7023 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007024 * The existing extent map already encompasses the
7025 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007026 */
7027 free_extent_map(em);
7028 em = existing;
7029 err = 0;
7030
7031 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007032 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007033 /*
7034 * The existing extent map is the one nearest to
7035 * the [start, start + len) range which overlaps
7036 */
7037 err = merge_extent_mapping(em_tree, existing,
7038 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007039 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007040 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007041 free_extent_map(em);
7042 em = NULL;
7043 }
7044 } else {
7045 free_extent_map(em);
7046 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007047 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007048 }
Chris Masona52d9a82007-08-27 16:49:44 -04007049 }
Chris Mason890871b2009-09-02 16:24:52 -04007050 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007051out:
liubo1abe9b82011-03-24 11:18:59 +00007052
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007053 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00007054
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007055 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007056 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007057 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007058 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007059 err = ret;
7060 }
Chris Masona52d9a82007-08-27 16:49:44 -04007061 if (err) {
7062 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007063 return ERR_PTR(err);
7064 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007065 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007066 return em;
7067}
7068
Chris Masonec29ed52011-02-23 16:23:20 -05007069struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7070 size_t pg_offset, u64 start, u64 len,
7071 int create)
7072{
7073 struct extent_map *em;
7074 struct extent_map *hole_em = NULL;
7075 u64 range_start = start;
7076 u64 end;
7077 u64 found;
7078 u64 found_end;
7079 int err = 0;
7080
7081 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7082 if (IS_ERR(em))
7083 return em;
7084 if (em) {
7085 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007086 * if our em maps to
7087 * - a hole or
7088 * - a pre-alloc extent,
7089 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007090 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007091 if (em->block_start != EXTENT_MAP_HOLE &&
7092 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007093 return em;
7094 else
7095 hole_em = em;
7096 }
7097
7098 /* check to see if we've wrapped (len == -1 or similar) */
7099 end = start + len;
7100 if (end < start)
7101 end = (u64)-1;
7102 else
7103 end -= 1;
7104
7105 em = NULL;
7106
7107 /* ok, we didn't find anything, lets look for delalloc */
7108 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7109 end, len, EXTENT_DELALLOC, 1);
7110 found_end = range_start + found;
7111 if (found_end < range_start)
7112 found_end = (u64)-1;
7113
7114 /*
7115 * we didn't find anything useful, return
7116 * the original results from get_extent()
7117 */
7118 if (range_start > end || found_end <= start) {
7119 em = hole_em;
7120 hole_em = NULL;
7121 goto out;
7122 }
7123
7124 /* adjust the range_start to make sure it doesn't
7125 * go backwards from the start they passed in
7126 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307127 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007128 found = found_end - range_start;
7129
7130 if (found > 0) {
7131 u64 hole_start = start;
7132 u64 hole_len = len;
7133
David Sterba172ddd62011-04-21 00:48:27 +02007134 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007135 if (!em) {
7136 err = -ENOMEM;
7137 goto out;
7138 }
7139 /*
7140 * when btrfs_get_extent can't find anything it
7141 * returns one huge hole
7142 *
7143 * make sure what it found really fits our range, and
7144 * adjust to make sure it is based on the start from
7145 * the caller
7146 */
7147 if (hole_em) {
7148 u64 calc_end = extent_map_end(hole_em);
7149
7150 if (calc_end <= start || (hole_em->start > end)) {
7151 free_extent_map(hole_em);
7152 hole_em = NULL;
7153 } else {
7154 hole_start = max(hole_em->start, start);
7155 hole_len = calc_end - hole_start;
7156 }
7157 }
7158 em->bdev = NULL;
7159 if (hole_em && range_start > hole_start) {
7160 /* our hole starts before our delalloc, so we
7161 * have to return just the parts of the hole
7162 * that go until the delalloc starts
7163 */
7164 em->len = min(hole_len,
7165 range_start - hole_start);
7166 em->start = hole_start;
7167 em->orig_start = hole_start;
7168 /*
7169 * don't adjust block start at all,
7170 * it is fixed at EXTENT_MAP_HOLE
7171 */
7172 em->block_start = hole_em->block_start;
7173 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007174 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7175 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007176 } else {
7177 em->start = range_start;
7178 em->len = found;
7179 em->orig_start = range_start;
7180 em->block_start = EXTENT_MAP_DELALLOC;
7181 em->block_len = found;
7182 }
7183 } else if (hole_em) {
7184 return hole_em;
7185 }
7186out:
7187
7188 free_extent_map(hole_em);
7189 if (err) {
7190 free_extent_map(em);
7191 return ERR_PTR(err);
7192 }
7193 return em;
7194}
7195
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007196static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7197 const u64 start,
7198 const u64 len,
7199 const u64 orig_start,
7200 const u64 block_start,
7201 const u64 block_len,
7202 const u64 orig_block_len,
7203 const u64 ram_bytes,
7204 const int type)
7205{
7206 struct extent_map *em = NULL;
7207 int ret;
7208
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007209 if (type != BTRFS_ORDERED_NOCOW) {
7210 em = create_pinned_em(inode, start, len, orig_start,
7211 block_start, block_len, orig_block_len,
7212 ram_bytes, type);
7213 if (IS_ERR(em))
7214 goto out;
7215 }
7216 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7217 len, block_len, type);
7218 if (ret) {
7219 if (em) {
7220 free_extent_map(em);
7221 btrfs_drop_extent_cache(inode, start,
7222 start + len - 1, 0);
7223 }
7224 em = ERR_PTR(ret);
7225 }
7226 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007227
7228 return em;
7229}
7230
Josef Bacik4b46fce2010-05-23 11:00:55 -04007231static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7232 u64 start, u64 len)
7233{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007235 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007236 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237 struct btrfs_key ins;
7238 u64 alloc_hint;
7239 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007242 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007243 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007244 if (ret)
7245 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007247 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7248 ins.objectid, ins.offset, ins.offset,
7249 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007250 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007251 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007252 btrfs_free_reserved_extent(fs_info, ins.objectid,
7253 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007254
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 return em;
7256}
7257
Chris Mason46bfbb52010-05-26 11:04:10 -04007258/*
7259 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7260 * block must be cow'd
7261 */
Josef Bacik00361582013-08-14 14:02:47 -04007262noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007263 u64 *orig_start, u64 *orig_block_len,
7264 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007265{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007266 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007267 struct btrfs_path *path;
7268 int ret;
7269 struct extent_buffer *leaf;
7270 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007271 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007272 struct btrfs_file_extent_item *fi;
7273 struct btrfs_key key;
7274 u64 disk_bytenr;
7275 u64 backref_offset;
7276 u64 extent_end;
7277 u64 num_bytes;
7278 int slot;
7279 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007280 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007281
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 path = btrfs_alloc_path();
7283 if (!path)
7284 return -ENOMEM;
7285
David Sterbaf85b7372017-01-20 14:54:07 +01007286 ret = btrfs_lookup_file_extent(NULL, root, path,
7287 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007288 if (ret < 0)
7289 goto out;
7290
7291 slot = path->slots[0];
7292 if (ret == 1) {
7293 if (slot == 0) {
7294 /* can't find the item, must cow */
7295 ret = 0;
7296 goto out;
7297 }
7298 slot--;
7299 }
7300 ret = 0;
7301 leaf = path->nodes[0];
7302 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007303 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007304 key.type != BTRFS_EXTENT_DATA_KEY) {
7305 /* not our file or wrong item type, must cow */
7306 goto out;
7307 }
7308
7309 if (key.offset > offset) {
7310 /* Wrong offset, must cow */
7311 goto out;
7312 }
7313
7314 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7315 found_type = btrfs_file_extent_type(leaf, fi);
7316 if (found_type != BTRFS_FILE_EXTENT_REG &&
7317 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7318 /* not a regular extent, must cow */
7319 goto out;
7320 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007321
7322 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7323 goto out;
7324
Miao Xiee77751a2013-12-27 21:11:50 +08007325 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7326 if (extent_end <= offset)
7327 goto out;
7328
Chris Mason46bfbb52010-05-26 11:04:10 -04007329 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007330 if (disk_bytenr == 0)
7331 goto out;
7332
7333 if (btrfs_file_extent_compression(leaf, fi) ||
7334 btrfs_file_extent_encryption(leaf, fi) ||
7335 btrfs_file_extent_other_encoding(leaf, fi))
7336 goto out;
7337
Chris Mason46bfbb52010-05-26 11:04:10 -04007338 backref_offset = btrfs_file_extent_offset(leaf, fi);
7339
Josef Bacik7ee9e442013-06-21 16:37:03 -04007340 if (orig_start) {
7341 *orig_start = key.offset - backref_offset;
7342 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7343 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7344 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007345
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007346 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007347 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007348
7349 num_bytes = min(offset + *len, extent_end) - offset;
7350 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7351 u64 range_end;
7352
Jeff Mahoneyda170662016-06-15 09:22:56 -04007353 range_end = round_up(offset + num_bytes,
7354 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007355 ret = test_range_bit(io_tree, offset, range_end,
7356 EXTENT_DELALLOC, 0, NULL);
7357 if (ret) {
7358 ret = -EAGAIN;
7359 goto out;
7360 }
7361 }
7362
Josef Bacik1bda19e2013-10-18 12:10:36 -04007363 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007364
7365 /*
7366 * look for other files referencing this extent, if we
7367 * find any we must cow
7368 */
Josef Bacik00361582013-08-14 14:02:47 -04007369
Liu Boe4c3b2d2017-01-30 12:25:28 -08007370 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007371 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007372 if (ret) {
7373 ret = 0;
7374 goto out;
7375 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007376
7377 /*
7378 * adjust disk_bytenr and num_bytes to cover just the bytes
7379 * in this extent we are about to write. If there
7380 * are any csums in that range we have to cow in order
7381 * to keep the csums correct
7382 */
7383 disk_bytenr += backref_offset;
7384 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007385 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7386 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007387 /*
7388 * all of the above have passed, it is safe to overwrite this extent
7389 * without cow
7390 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007391 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 ret = 1;
7393out:
7394 btrfs_free_path(path);
7395 return ret;
7396}
7397
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007398bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7399{
7400 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7401 int found = false;
7402 void **pagep = NULL;
7403 struct page *page = NULL;
7404 int start_idx;
7405 int end_idx;
7406
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007407 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007408
7409 /*
7410 * end is the last byte in the last page. end == start is legal
7411 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007412 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007413
7414 rcu_read_lock();
7415
7416 /* Most of the code in this while loop is lifted from
7417 * find_get_page. It's been modified to begin searching from a
7418 * page and return just the first page found in that range. If the
7419 * found idx is less than or equal to the end idx then we know that
7420 * a page exists. If no pages are found or if those pages are
7421 * outside of the range then we're fine (yay!) */
7422 while (page == NULL &&
7423 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7424 page = radix_tree_deref_slot(pagep);
7425 if (unlikely(!page))
7426 break;
7427
7428 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007429 if (radix_tree_deref_retry(page)) {
7430 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007431 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007432 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007433 /*
7434 * Otherwise, shmem/tmpfs must be storing a swap entry
7435 * here as an exceptional entry: so return it without
7436 * attempting to raise page count.
7437 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007438 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007439 break; /* TODO: Is this relevant for this use case? */
7440 }
7441
Filipe Manana91405152014-06-05 13:22:24 +01007442 if (!page_cache_get_speculative(page)) {
7443 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007444 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007445 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007446
7447 /*
7448 * Has the page moved?
7449 * This is part of the lockless pagecache protocol. See
7450 * include/linux/pagemap.h for details.
7451 */
7452 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007453 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007454 page = NULL;
7455 }
7456 }
7457
7458 if (page) {
7459 if (page->index <= end_idx)
7460 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007461 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007462 }
7463
7464 rcu_read_unlock();
7465 return found;
7466}
7467
Josef Bacikeb838e72012-07-31 16:28:48 -04007468static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7469 struct extent_state **cached_state, int writing)
7470{
7471 struct btrfs_ordered_extent *ordered;
7472 int ret = 0;
7473
7474 while (1) {
7475 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007476 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007477 /*
7478 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007479 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007480 * extents in this range.
7481 */
7482 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7483 lockend - lockstart + 1);
7484
7485 /*
7486 * We need to make sure there are no buffered pages in this
7487 * range either, we could have raced between the invalidate in
7488 * generic_file_direct_write and locking the extent. The
7489 * invalidate needs to happen so that reads after a write do not
7490 * get stale data.
7491 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007492 if (!ordered &&
7493 (!writing ||
7494 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 break;
7496
7497 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7498 cached_state, GFP_NOFS);
7499
7500 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007501 /*
7502 * If we are doing a DIO read and the ordered extent we
7503 * found is for a buffered write, we can not wait for it
7504 * to complete and retry, because if we do so we can
7505 * deadlock with concurrent buffered writes on page
7506 * locks. This happens only if our DIO read covers more
7507 * than one extent map, if at this point has already
7508 * created an ordered extent for a previous extent map
7509 * and locked its range in the inode's io tree, and a
7510 * concurrent write against that previous extent map's
7511 * range and this range started (we unlock the ranges
7512 * in the io tree only when the bios complete and
7513 * buffered writes always lock pages before attempting
7514 * to lock range in the io tree).
7515 */
7516 if (writing ||
7517 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7518 btrfs_start_ordered_extent(inode, ordered, 1);
7519 else
7520 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 btrfs_put_ordered_extent(ordered);
7522 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007523 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007524 * We could trigger writeback for this range (and wait
7525 * for it to complete) and then invalidate the pages for
7526 * this range (through invalidate_inode_pages2_range()),
7527 * but that can lead us to a deadlock with a concurrent
7528 * call to readpages() (a buffered read or a defrag call
7529 * triggered a readahead) on a page lock due to an
7530 * ordered dio extent we created before but did not have
7531 * yet a corresponding bio submitted (whence it can not
7532 * complete), which makes readpages() wait for that
7533 * ordered extent to complete while holding a lock on
7534 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007536 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007537 }
7538
Filipe Mananaade77022016-02-18 14:28:55 +00007539 if (ret)
7540 break;
7541
Josef Bacikeb838e72012-07-31 16:28:48 -04007542 cond_resched();
7543 }
7544
7545 return ret;
7546}
7547
Josef Bacik69ffb542012-09-11 15:40:07 -04007548static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7549 u64 len, u64 orig_start,
7550 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007551 u64 orig_block_len, u64 ram_bytes,
7552 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007553{
7554 struct extent_map_tree *em_tree;
7555 struct extent_map *em;
7556 struct btrfs_root *root = BTRFS_I(inode)->root;
7557 int ret;
7558
7559 em_tree = &BTRFS_I(inode)->extent_tree;
7560 em = alloc_extent_map();
7561 if (!em)
7562 return ERR_PTR(-ENOMEM);
7563
7564 em->start = start;
7565 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007566 em->mod_start = start;
7567 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007568 em->len = len;
7569 em->block_len = block_len;
7570 em->block_start = block_start;
7571 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007572 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007573 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007574 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007575 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7576 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007577 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007578
7579 do {
7580 btrfs_drop_extent_cache(inode, em->start,
7581 em->start + em->len - 1, 0);
7582 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007583 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007584 write_unlock(&em_tree->lock);
7585 } while (ret == -EEXIST);
7586
7587 if (ret) {
7588 free_extent_map(em);
7589 return ERR_PTR(ret);
7590 }
7591
7592 return em;
7593}
7594
Filipe Manana9c9464c2015-11-04 09:52:04 +00007595static void adjust_dio_outstanding_extents(struct inode *inode,
7596 struct btrfs_dio_data *dio_data,
7597 const u64 len)
7598{
David Sterba823bb202017-01-04 11:09:51 +01007599 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007600
Filipe Manana9c9464c2015-11-04 09:52:04 +00007601 /*
7602 * If we have an outstanding_extents count still set then we're
7603 * within our reservation, otherwise we need to adjust our inode
7604 * counter appropriately.
7605 */
Liu Boc2931662016-12-22 17:13:54 -08007606 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007607 dio_data->outstanding_extents -= num_extents;
7608 } else {
Liu Boc2931662016-12-22 17:13:54 -08007609 /*
7610 * If dio write length has been split due to no large enough
7611 * contiguous space, we need to compensate our inode counter
7612 * appropriately.
7613 */
7614 u64 num_needed = num_extents - dio_data->outstanding_extents;
7615
Filipe Manana9c9464c2015-11-04 09:52:04 +00007616 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007617 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007618 spin_unlock(&BTRFS_I(inode)->lock);
7619 }
7620}
7621
Josef Bacik4b46fce2010-05-23 11:00:55 -04007622static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7623 struct buffer_head *bh_result, int create)
7624{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007625 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307628 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007631 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007632 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007633 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007634
Miao Xie172a5042013-02-21 02:48:22 -07007635 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007636 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007637 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007638 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007639
Josef Bacikc3298612012-08-03 16:49:19 -04007640 lockstart = start;
7641 lockend = start + len - 1;
7642
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007643 if (current->journal_info) {
7644 /*
7645 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007646 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007647 * confused.
7648 */
chandan50745b02015-08-28 21:10:13 +05307649 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007650 current->journal_info = NULL;
7651 }
7652
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 /*
7654 * If this errors out it's because we couldn't invalidate pagecache for
7655 * this range and we need to fallback to buffered.
7656 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007657 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7658 create)) {
7659 ret = -ENOTBLK;
7660 goto err;
7661 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007662
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 if (IS_ERR(em)) {
7665 ret = PTR_ERR(em);
7666 goto unlock_err;
7667 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
7669 /*
7670 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7671 * io. INLINE is special, and we could probably kludge it in here, but
7672 * it's still buffered so for safety lets just fall back to the generic
7673 * buffered path.
7674 *
7675 * For COMPRESSED we _have_ to read the entire extent in so we can
7676 * decompress it, so there will be buffering required no matter what we
7677 * do, so go ahead and fallback to buffered.
7678 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007679 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680 * to buffered IO. Don't blame me, this is the price we pay for using
7681 * the generic code.
7682 */
7683 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7684 em->block_start == EXTENT_MAP_INLINE) {
7685 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 ret = -ENOTBLK;
7687 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007688 }
7689
7690 /* Just a good old fashioned hole, return */
7691 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7692 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7693 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007694 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007695 }
7696
7697 /*
7698 * We don't allocate a new extent in the following cases
7699 *
7700 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7701 * existing extent.
7702 * 2) The extent is marked as PREALLOC. We're good to go here and can
7703 * just use the extent.
7704 *
7705 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007706 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007707 len = min(len, em->len - (start - em->start));
7708 lockstart = start + len;
7709 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007710 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007711
7712 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7713 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7714 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007715 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007716 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717
7718 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7719 type = BTRFS_ORDERED_PREALLOC;
7720 else
7721 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007722 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007724
Josef Bacik00361582013-08-14 14:02:47 -04007725 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007726 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007727 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007728 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007729
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007730 em2 = btrfs_create_dio_extent(inode, start, len,
7731 orig_start, block_start,
7732 len, orig_block_len,
7733 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007734 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 if (type == BTRFS_ORDERED_PREALLOC) {
7736 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007737 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007738 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007739 if (em2 && IS_ERR(em2)) {
7740 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007741 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007742 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007743 /*
7744 * For inode marked NODATACOW or extent marked PREALLOC,
7745 * use the existing or preallocated extent, so does not
7746 * need to adjust btrfs_space_info's bytes_may_use.
7747 */
7748 btrfs_free_reserved_data_space_noquota(inode,
7749 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007750 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007752 }
Josef Bacik00361582013-08-14 14:02:47 -04007753
Chris Mason46bfbb52010-05-26 11:04:10 -04007754 /*
7755 * this will cow the extent, reset the len in case we changed
7756 * it above
7757 */
7758 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007759 free_extent_map(em);
7760 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 if (IS_ERR(em)) {
7762 ret = PTR_ERR(em);
7763 goto unlock_err;
7764 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007765 len = min(len, em->len - (start - em->start));
7766unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7768 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007769 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 bh_result->b_bdev = em->bdev;
7771 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007772 if (create) {
7773 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7774 set_buffer_new(bh_result);
7775
7776 /*
7777 * Need to update the i_size under the extent lock so buffered
7778 * readers will get the updated i_size when we unlock.
7779 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007780 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007781 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007782
Filipe Manana9c9464c2015-11-04 09:52:04 +00007783 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307784 WARN_ON(dio_data->reserve < len);
7785 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007786 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307787 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007788 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007789
Josef Bacikeb838e72012-07-31 16:28:48 -04007790 /*
7791 * In the case of write we need to clear and unlock the entire range,
7792 * in the case of read we need to unlock only the end area that we
7793 * aren't using if there is any left over space.
7794 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007795 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007796 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7797 lockend, unlock_bits, 1, 0,
7798 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007799 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007800 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007801 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007802
Josef Bacik4b46fce2010-05-23 11:00:55 -04007803 free_extent_map(em);
7804
7805 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007806
7807unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007808 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7809 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007810err:
chandan50745b02015-08-28 21:10:13 +05307811 if (dio_data)
7812 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007813 /*
7814 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7815 * write less data then expected, so that we don't underflow our inode's
7816 * outstanding extents counter.
7817 */
7818 if (create && dio_data)
7819 adjust_dio_outstanding_extents(inode, dio_data, len);
7820
Josef Bacikeb838e72012-07-31 16:28:48 -04007821 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822}
7823
Miao Xie8b110e32014-09-12 18:44:03 +08007824static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007825 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007826{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007827 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007828 int ret;
7829
Mike Christie37226b22016-06-05 14:31:52 -05007830 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007831
7832 bio_get(bio);
7833
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007834 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007835 if (ret)
7836 goto err;
7837
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007838 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007839err:
7840 bio_put(bio);
7841 return ret;
7842}
7843
7844static int btrfs_check_dio_repairable(struct inode *inode,
7845 struct bio *failed_bio,
7846 struct io_failure_record *failrec,
7847 int failed_mirror)
7848{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007850 int num_copies;
7851
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007852 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007853 if (num_copies == 1) {
7854 /*
7855 * we only have a single copy of the data, so don't bother with
7856 * all the retry and error correction code that follows. no
7857 * matter what the error is, it is very likely to persist.
7858 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007859 btrfs_debug(fs_info,
7860 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7861 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007862 return 0;
7863 }
7864
7865 failrec->failed_mirror = failed_mirror;
7866 failrec->this_mirror++;
7867 if (failrec->this_mirror == failed_mirror)
7868 failrec->this_mirror++;
7869
7870 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007871 btrfs_debug(fs_info,
7872 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7873 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007874 return 0;
7875 }
7876
7877 return 1;
7878}
7879
7880static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307881 struct page *page, unsigned int pgoff,
7882 u64 start, u64 end, int failed_mirror,
7883 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007884{
7885 struct io_failure_record *failrec;
7886 struct bio *bio;
7887 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007888 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007889 int ret;
7890
Mike Christie37226b22016-06-05 14:31:52 -05007891 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007892
7893 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7894 if (ret)
7895 return ret;
7896
7897 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7898 failed_mirror);
7899 if (!ret) {
7900 free_io_failure(inode, failrec);
7901 return -EIO;
7902 }
7903
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307904 if ((failed_bio->bi_vcnt > 1)
7905 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007906 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007907 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007908
7909 isector = start - btrfs_io_bio(failed_bio)->logical;
7910 isector >>= inode->i_sb->s_blocksize_bits;
7911 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307912 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007913 if (!bio) {
7914 free_io_failure(inode, failrec);
7915 return -EIO;
7916 }
Mike Christie37226b22016-06-05 14:31:52 -05007917 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007918
7919 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7920 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7921 read_mode, failrec->this_mirror, failrec->in_validation);
7922
Mike Christie81a75f672016-06-05 14:31:54 -05007923 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007924 if (ret) {
7925 free_io_failure(inode, failrec);
7926 bio_put(bio);
7927 }
7928
7929 return ret;
7930}
7931
7932struct btrfs_retry_complete {
7933 struct completion done;
7934 struct inode *inode;
7935 u64 start;
7936 int uptodate;
7937};
7938
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007939static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007940{
7941 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007943 struct bio_vec *bvec;
7944 int i;
7945
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007946 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007947 goto end;
7948
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307949 ASSERT(bio->bi_vcnt == 1);
7950 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007951 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952
Miao Xie8b110e32014-09-12 18:44:03 +08007953 done->uptodate = 1;
7954 bio_for_each_segment_all(bvec, bio, i)
7955 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7956end:
7957 complete(&done->done);
7958 bio_put(bio);
7959}
7960
7961static int __btrfs_correct_data_nocsum(struct inode *inode,
7962 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007963{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007965 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007966 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007967 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968 unsigned int pgoff;
7969 u32 sectorsize;
7970 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007971 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007972 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007973
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007975 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976
Miao Xiec1dc0892014-09-12 18:43:56 +08007977 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007978 done.inode = inode;
7979
7980 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7982 pgoff = bvec->bv_offset;
7983
7984next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007985 done.uptodate = 0;
7986 done.start = start;
7987 init_completion(&done.done);
7988
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7990 pgoff, start, start + sectorsize - 1,
7991 io_bio->mirror_num,
7992 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007993 if (ret)
7994 return ret;
7995
7996 wait_for_completion(&done.done);
7997
7998 if (!done.uptodate) {
7999 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008001 }
8002
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003 start += sectorsize;
8004
8005 if (nr_sectors--) {
8006 pgoff += sectorsize;
8007 goto next_block_or_try_again;
8008 }
Miao Xie8b110e32014-09-12 18:44:03 +08008009 }
8010
8011 return 0;
8012}
8013
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008014static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008015{
8016 struct btrfs_retry_complete *done = bio->bi_private;
8017 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008019 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008021 int uptodate;
8022 int ret;
8023 int i;
8024
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008025 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008026 goto end;
8027
8028 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029
8030 start = done->start;
8031
8032 ASSERT(bio->bi_vcnt == 1);
8033 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008034 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035
Miao Xie8b110e32014-09-12 18:44:03 +08008036 bio_for_each_segment_all(bvec, bio, i) {
8037 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 bvec->bv_page, bvec->bv_offset,
8039 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008040 if (!ret)
8041 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008043 else
8044 uptodate = 0;
8045 }
8046
8047 done->uptodate = uptodate;
8048end:
8049 complete(&done->done);
8050 bio_put(bio);
8051}
8052
8053static int __btrfs_subio_endio_read(struct inode *inode,
8054 struct btrfs_io_bio *io_bio, int err)
8055{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008057 struct bio_vec *bvec;
8058 struct btrfs_retry_complete done;
8059 u64 start;
8060 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 u32 sectorsize;
8062 int nr_sectors;
8063 unsigned int pgoff;
8064 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008065 int i;
8066 int ret;
8067
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008069 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070
Miao Xie8b110e32014-09-12 18:44:03 +08008071 err = 0;
8072 start = io_bio->logical;
8073 done.inode = inode;
8074
Miao Xiec1dc0892014-09-12 18:43:56 +08008075 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8077
8078 pgoff = bvec->bv_offset;
8079next_block:
8080 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8081 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8082 bvec->bv_page, pgoff, start,
8083 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008084 if (likely(!ret))
8085 goto next;
8086try_again:
8087 done.uptodate = 0;
8088 done.start = start;
8089 init_completion(&done.done);
8090
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8092 pgoff, start, start + sectorsize - 1,
8093 io_bio->mirror_num,
8094 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008095 if (ret) {
8096 err = ret;
8097 goto next;
8098 }
8099
8100 wait_for_completion(&done.done);
8101
8102 if (!done.uptodate) {
8103 /* We might have another mirror, so try again */
8104 goto try_again;
8105 }
8106next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107 offset += sectorsize;
8108 start += sectorsize;
8109
8110 ASSERT(nr_sectors);
8111
8112 if (--nr_sectors) {
8113 pgoff += sectorsize;
8114 goto next_block;
8115 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008116 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008117
8118 return err;
8119}
8120
Miao Xie8b110e32014-09-12 18:44:03 +08008121static int btrfs_subio_endio_read(struct inode *inode,
8122 struct btrfs_io_bio *io_bio, int err)
8123{
8124 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8125
8126 if (skip_csum) {
8127 if (unlikely(err))
8128 return __btrfs_correct_data_nocsum(inode, io_bio);
8129 else
8130 return 0;
8131 } else {
8132 return __btrfs_subio_endio_read(inode, io_bio, err);
8133 }
8134}
8135
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008136static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008137{
8138 struct btrfs_dio_private *dip = bio->bi_private;
8139 struct inode *inode = dip->inode;
8140 struct bio *dio_bio;
8141 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008142 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008143
Miao Xie8b110e32014-09-12 18:44:03 +08008144 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8145 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008146
Josef Bacik4b46fce2010-05-23 11:00:55 -04008147 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008148 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008149 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008150
Josef Bacik4b46fce2010-05-23 11:00:55 -04008151 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008152
Filipe Manana1636d1d2016-02-15 16:20:26 +00008153 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008154 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008155
8156 if (io_bio->end_io)
8157 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008158 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008159}
8160
Filipe Manana14543772015-11-24 16:23:54 +00008161static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8162 const u64 offset,
8163 const u64 bytes,
8164 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008168 u64 ordered_offset = offset;
8169 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170 int ret;
8171
Chris Mason163cf092010-11-28 19:56:33 -05008172again:
8173 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8174 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008175 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008176 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008177 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008178 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179
Liu Bo9e0af232014-08-15 23:36:53 +08008180 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8181 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008182 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008183out_test:
8184 /*
8185 * our bio might span multiple ordered extents. If we haven't
8186 * completed the accounting for the whole dio, go back and try again
8187 */
Filipe Manana14543772015-11-24 16:23:54 +00008188 if (ordered_offset < offset + bytes) {
8189 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008190 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008191 goto again;
8192 }
Filipe Manana14543772015-11-24 16:23:54 +00008193}
8194
8195static void btrfs_endio_direct_write(struct bio *bio)
8196{
8197 struct btrfs_dio_private *dip = bio->bi_private;
8198 struct bio *dio_bio = dip->dio_bio;
8199
8200 btrfs_endio_direct_write_update_ordered(dip->inode,
8201 dip->logical_offset,
8202 dip->bytes,
8203 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008206
Filipe Manana1636d1d2016-02-15 16:20:26 +00008207 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008208 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008209 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008210}
8211
Mike Christie81a75f672016-06-05 14:31:54 -05008212static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008213 struct bio *bio, int mirror_num,
8214 unsigned long bio_flags, u64 offset)
8215{
8216 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008217 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008218 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008219 return 0;
8220}
8221
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008222static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008223{
8224 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008225 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008226
Miao Xie8b110e32014-09-12 18:44:03 +08008227 if (err)
8228 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008229 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008230 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8231 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008232 (unsigned long long)bio->bi_iter.bi_sector,
8233 bio->bi_iter.bi_size, err);
8234
8235 if (dip->subio_endio)
8236 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008237
8238 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008239 dip->errors = 1;
8240
8241 /*
8242 * before atomic variable goto zero, we must make sure
8243 * dip->errors is perceived to be set.
8244 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008245 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008246 }
8247
8248 /* if there are more bios still pending for this dio, just exit */
8249 if (!atomic_dec_and_test(&dip->pending_bios))
8250 goto out;
8251
Chris Mason9be33952013-05-17 18:30:14 -04008252 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008253 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008254 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008255 dip->dio_bio->bi_error = 0;
8256 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008257 }
8258out:
8259 bio_put(bio);
8260}
8261
8262static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8263 u64 first_sector, gfp_t gfp_flags)
8264{
Chris Masonda2f0f72015-07-02 13:57:22 -07008265 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008266 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008267 if (bio)
8268 bio_associate_current(bio);
8269 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008270}
8271
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008272static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008273 struct btrfs_dio_private *dip,
8274 struct bio *bio,
8275 u64 file_offset)
8276{
8277 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8278 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8279 int ret;
8280
8281 /*
8282 * We load all the csum data we need when we submit
8283 * the first bio to reduce the csum tree search and
8284 * contention.
8285 */
8286 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008287 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008288 file_offset);
8289 if (ret)
8290 return ret;
8291 }
8292
8293 if (bio == dip->orig_bio)
8294 return 0;
8295
8296 file_offset -= dip->logical_offset;
8297 file_offset >>= inode->i_sb->s_blocksize_bits;
8298 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8299
8300 return 0;
8301}
8302
Miao Xiee65e1532010-11-22 03:04:43 +00008303static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008304 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008305 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008307 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008308 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008309 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008310 int ret;
8311
Josef Bacikb812ce22012-11-16 13:56:32 -05008312 if (async_submit)
8313 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8314
Miao Xiee65e1532010-11-22 03:04:43 +00008315 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008316
8317 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008318 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008319 if (ret)
8320 goto err;
8321 }
Miao Xiee65e1532010-11-22 03:04:43 +00008322
Josef Bacik1ae39932011-04-06 14:41:34 -04008323 if (skip_sum)
8324 goto map;
8325
8326 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008327 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8328 file_offset,
8329 __btrfs_submit_bio_start_direct_io,
8330 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008331 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008332 } else if (write) {
8333 /*
8334 * If we aren't doing async submit, calculate the csum of the
8335 * bio now.
8336 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008337 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008338 if (ret)
8339 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008340 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008341 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008342 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008343 if (ret)
8344 goto err;
8345 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008346map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008347 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008348err:
8349 bio_put(bio);
8350 return ret;
8351}
8352
Mike Christie81a75f672016-06-05 14:31:54 -05008353static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008354 int skip_sum)
8355{
8356 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008358 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008359 struct bio *bio;
8360 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008361 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008362 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008363 u64 file_offset = dip->logical_offset;
8364 u64 submit_len = 0;
8365 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008366 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008367 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308368 int nr_sectors;
8369 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008370 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008371
Kent Overstreet4f024f32013-10-11 15:44:27 -07008372 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008373 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8374 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008375 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008376 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008377
Kent Overstreet4f024f32013-10-11 15:44:27 -07008378 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008379 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008380 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008381 goto submit;
8382 }
8383
David Woodhouse53b381b2013-01-29 18:40:14 -05008384 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008385 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008386 async_submit = 0;
8387 else
8388 async_submit = 1;
8389
Josef Bacik02f57c72011-04-06 14:25:44 -04008390 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8391 if (!bio)
8392 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008393
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008394 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008395 bio->bi_private = dip;
8396 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008397 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008398 atomic_inc(&dip->pending_bios);
8399
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008400 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008401 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308402 i = 0;
8403next_block:
8404 if (unlikely(map_length < submit_len + blocksize ||
8405 bio_add_page(bio, bvec->bv_page, blocksize,
8406 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008407 /*
8408 * inc the count before we submit the bio so
8409 * we know the end IO handler won't happen before
8410 * we inc the count. Otherwise, the dip might get freed
8411 * before we're done setting it up
8412 */
8413 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008414 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008415 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008416 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008417 if (ret) {
8418 bio_put(bio);
8419 atomic_dec(&dip->pending_bios);
8420 goto out_err;
8421 }
8422
Miao Xiee65e1532010-11-22 03:04:43 +00008423 start_sector += submit_len >> 9;
8424 file_offset += submit_len;
8425
8426 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008427
8428 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8429 start_sector, GFP_NOFS);
8430 if (!bio)
8431 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008432 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008433 bio->bi_private = dip;
8434 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008435 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008436
Kent Overstreet4f024f32013-10-11 15:44:27 -07008437 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008438 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008439 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008440 &map_length, NULL, 0);
8441 if (ret) {
8442 bio_put(bio);
8443 goto out_err;
8444 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308445
8446 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008447 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308448 submit_len += blocksize;
8449 if (--nr_sectors) {
8450 i++;
8451 goto next_block;
8452 }
Miao Xiee65e1532010-11-22 03:04:43 +00008453 }
8454 }
8455
Josef Bacik02f57c72011-04-06 14:25:44 -04008456submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008457 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008458 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008459 if (!ret)
8460 return 0;
8461
8462 bio_put(bio);
8463out_err:
8464 dip->errors = 1;
8465 /*
8466 * before atomic variable goto zero, we must
8467 * make sure dip->errors is perceived to be set.
8468 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008469 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008470 if (atomic_dec_and_test(&dip->pending_bios))
8471 bio_io_error(dip->orig_bio);
8472
8473 /* bio_end_io() will handle error, so we needn't return it */
8474 return 0;
8475}
8476
Mike Christie8a4c1e42016-06-05 14:31:50 -05008477static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8478 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008479{
Filipe Manana61de7182015-07-01 12:13:10 +01008480 struct btrfs_dio_private *dip = NULL;
8481 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008482 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008483 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008484 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008485 int ret = 0;
8486
8487 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8488
Chris Mason9be33952013-05-17 18:30:14 -04008489 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008490 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008491 ret = -ENOMEM;
8492 goto free_ordered;
8493 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008494
Miao Xiec1dc0892014-09-12 18:43:56 +08008495 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008496 if (!dip) {
8497 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008498 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008499 }
8500
8501 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008502 dip->inode = inode;
8503 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008504 dip->bytes = dio_bio->bi_iter.bi_size;
8505 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008506 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008507 dip->orig_bio = io_bio;
8508 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008509 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008510 btrfs_bio = btrfs_io_bio(io_bio);
8511 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512
Miao Xiec1dc0892014-09-12 18:43:56 +08008513 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008514 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008515 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008516 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008517 dip->subio_endio = btrfs_subio_endio_read;
8518 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008519
Filipe Mananaf28a4922015-12-08 19:23:20 +00008520 /*
8521 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8522 * even if we fail to submit a bio, because in such case we do the
8523 * corresponding error handling below and it must not be done a second
8524 * time by btrfs_direct_IO().
8525 */
8526 if (write) {
8527 struct btrfs_dio_data *dio_data = current->journal_info;
8528
8529 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8530 dip->bytes;
8531 dio_data->unsubmitted_oe_range_start =
8532 dio_data->unsubmitted_oe_range_end;
8533 }
8534
Mike Christie81a75f672016-06-05 14:31:54 -05008535 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008536 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008537 return;
Chris Mason9be33952013-05-17 18:30:14 -04008538
Miao Xie23ea8e52014-09-12 18:43:54 +08008539 if (btrfs_bio->end_io)
8540 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008541
Josef Bacik4b46fce2010-05-23 11:00:55 -04008542free_ordered:
8543 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008544 * If we arrived here it means either we failed to submit the dip
8545 * or we either failed to clone the dio_bio or failed to allocate the
8546 * dip. If we cloned the dio_bio and allocated the dip, we can just
8547 * call bio_endio against our io_bio so that we get proper resource
8548 * cleanup if we fail to submit the dip, otherwise, we must do the
8549 * same as btrfs_endio_direct_[write|read] because we can't call these
8550 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008551 */
Filipe Manana61de7182015-07-01 12:13:10 +01008552 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008553 io_bio->bi_error = -EIO;
8554 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008555 /*
8556 * The end io callbacks free our dip, do the final put on io_bio
8557 * and all the cleanup and final put for dio_bio (through
8558 * dio_end_io()).
8559 */
8560 dip = NULL;
8561 io_bio = NULL;
8562 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008563 if (write)
8564 btrfs_endio_direct_write_update_ordered(inode,
8565 file_offset,
8566 dio_bio->bi_iter.bi_size,
8567 0);
8568 else
Filipe Manana61de7182015-07-01 12:13:10 +01008569 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8570 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008571
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008572 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008573 /*
8574 * Releases and cleans up our dio_bio, no need to bio_put()
8575 * nor bio_endio()/bio_io_error() against dio_bio.
8576 */
8577 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008578 }
Filipe Manana61de7182015-07-01 12:13:10 +01008579 if (io_bio)
8580 bio_put(io_bio);
8581 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008582}
8583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008584static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8585 struct kiocb *iocb,
8586 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008587{
8588 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008589 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008590 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008591 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008592
8593 if (offset & blocksize_mask)
8594 goto out;
8595
Al Viro28060d52014-03-22 05:15:17 -04008596 if (iov_iter_alignment(iter) & blocksize_mask)
8597 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008598
Al Viro28060d52014-03-22 05:15:17 -04008599 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008600 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008601 return 0;
8602 /*
8603 * Check to make sure we don't have duplicate iov_base's in this
8604 * iovec, if so return EINVAL, otherwise we'll get csum errors
8605 * when reading back.
8606 */
8607 for (seg = 0; seg < iter->nr_segs; seg++) {
8608 for (i = seg + 1; i < iter->nr_segs; i++) {
8609 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008610 goto out;
8611 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008612 }
8613 retval = 0;
8614out:
8615 return retval;
8616}
Josef Bacikeb838e72012-07-31 16:28:48 -04008617
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008618static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008619{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008620 struct file *file = iocb->ki_filp;
8621 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008622 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308623 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008624 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008625 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008626 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008627 bool wakeup = true;
8628 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008629 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008630
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008631 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008632 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008633
Jens Axboefe0f07d2015-04-15 17:05:48 -06008634 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008635 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008636
Josef Bacik0e267c42013-07-02 10:38:02 -04008637 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008638 * The generic stuff only does filemap_write_and_wait_range, which
8639 * isn't enough if we've written compressed pages to this area, so
8640 * we need to flush the dirty pages again to make absolutely sure
8641 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008642 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008643 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008644 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8645 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008646 filemap_fdatawrite_range(inode->i_mapping, offset,
8647 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008648
Omar Sandoval6f673762015-03-16 04:33:52 -07008649 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008650 /*
8651 * If the write DIO is beyond the EOF, we need update
8652 * the isize, but it is protected by i_mutex. So we can
8653 * not unlock the i_mutex at this case.
8654 */
8655 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008656 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008657 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008658 relock = true;
8659 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008660 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008661 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008662 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008663 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008664
8665 /*
8666 * We need to know how many extents we reserved so that we can
8667 * do the accounting properly if we go over the number we
8668 * originally calculated. Abuse current->journal_info for this.
8669 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008670 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008671 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008672 dio_data.unsubmitted_oe_range_start = (u64)offset;
8673 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308674 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308675 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008676 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8677 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008678 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008679 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8680 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008681 }
8682
Omar Sandoval17f8c842015-03-16 04:33:50 -07008683 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008684 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008685 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008686 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008687 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308688 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008689 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008690 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308691 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008692 btrfs_delalloc_release_space(inode, offset,
8693 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008694 /*
8695 * On error we might have left some ordered extents
8696 * without submitting corresponding bios for them, so
8697 * cleanup them up to avoid other tasks getting them
8698 * and waiting for them to complete forever.
8699 */
8700 if (dio_data.unsubmitted_oe_range_start <
8701 dio_data.unsubmitted_oe_range_end)
8702 btrfs_endio_direct_write_update_ordered(inode,
8703 dio_data.unsubmitted_oe_range_start,
8704 dio_data.unsubmitted_oe_range_end -
8705 dio_data.unsubmitted_oe_range_start,
8706 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008707 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008708 btrfs_delalloc_release_space(inode, offset,
8709 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008710 }
Miao Xie38851cc2013-02-08 07:04:11 +00008711out:
Miao Xie2e60a512013-02-08 07:01:08 +00008712 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008713 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008714 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008715 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008716
8717 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008718}
8719
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008720#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8721
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008722static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8723 __u64 start, __u64 len)
8724{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008725 int ret;
8726
8727 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8728 if (ret)
8729 return ret;
8730
Chris Masonec29ed52011-02-23 16:23:20 -05008731 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008732}
8733
Chris Mason9ebefb182007-06-15 13:50:00 -04008734int btrfs_readpage(struct file *file, struct page *page)
8735{
Chris Masond1310b22008-01-24 16:13:08 -05008736 struct extent_io_tree *tree;
8737 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008738 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008739}
Chris Mason1832a6d2007-12-21 16:27:21 -05008740
Chris Mason39279cc2007-06-12 06:35:45 -04008741static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8742{
Chris Masond1310b22008-01-24 16:13:08 -05008743 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008744 struct inode *inode = page->mapping->host;
8745 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008746
8747 if (current->flags & PF_MEMALLOC) {
8748 redirty_page_for_writepage(wbc, page);
8749 unlock_page(page);
8750 return 0;
8751 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008752
8753 /*
8754 * If we are under memory pressure we will call this directly from the
8755 * VM, we need to make sure we have the inode referenced for the ordered
8756 * extent. If not just return like we didn't do anything.
8757 */
8758 if (!igrab(inode)) {
8759 redirty_page_for_writepage(wbc, page);
8760 return AOP_WRITEPAGE_ACTIVATE;
8761 }
Chris Masond1310b22008-01-24 16:13:08 -05008762 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008763 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8764 btrfs_add_delayed_iput(inode);
8765 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008766}
Chris Mason39279cc2007-06-12 06:35:45 -04008767
Eric Sandeen48a3b632013-04-25 20:41:01 +00008768static int btrfs_writepages(struct address_space *mapping,
8769 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008770{
Chris Masond1310b22008-01-24 16:13:08 -05008771 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008772
Chris Masond1310b22008-01-24 16:13:08 -05008773 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008774 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8775}
8776
Chris Mason3ab2fb52007-11-08 10:59:22 -05008777static int
8778btrfs_readpages(struct file *file, struct address_space *mapping,
8779 struct list_head *pages, unsigned nr_pages)
8780{
Chris Masond1310b22008-01-24 16:13:08 -05008781 struct extent_io_tree *tree;
8782 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008783 return extent_readpages(tree, mapping, pages, nr_pages,
8784 btrfs_get_extent);
8785}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008786static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008787{
Chris Masond1310b22008-01-24 16:13:08 -05008788 struct extent_io_tree *tree;
8789 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008790 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008791
Chris Masond1310b22008-01-24 16:13:08 -05008792 tree = &BTRFS_I(page->mapping->host)->io_tree;
8793 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008794 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008795 if (ret == 1) {
8796 ClearPagePrivate(page);
8797 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008798 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008799 }
8800 return ret;
8801}
Chris Mason39279cc2007-06-12 06:35:45 -04008802
Chris Masone6dcd2d2008-07-17 12:53:50 -04008803static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8804{
Chris Mason98509cf2008-09-11 15:51:43 -04008805 if (PageWriteback(page) || PageDirty(page))
8806 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008807 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808}
8809
Lukas Czernerd47992f2013-05-21 23:17:23 -04008810static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8811 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008812{
Josef Bacik5fd02042012-05-02 14:00:54 -04008813 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008814 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008816 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008817 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008818 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308819 u64 start;
8820 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008821 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008822
Chris Mason8b62b722009-09-02 16:53:46 -04008823 /*
8824 * we have the page locked, so new writeback can't start,
8825 * and the dirty bit won't be cleared while we are here.
8826 *
8827 * Wait for IO on this page so that we can safely clear
8828 * the PagePrivate2 bit and do ordered accounting
8829 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008831
Josef Bacik5fd02042012-05-02 14:00:54 -04008832 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 if (offset) {
8834 btrfs_releasepage(page, GFP_NOFS);
8835 return;
8836 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008837
8838 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008839 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308840again:
8841 start = page_start;
8842 ordered = btrfs_lookup_ordered_range(inode, start,
8843 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308845 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008846 /*
8847 * IO on this page will never be started, so we need
8848 * to account for any ordered extents now
8849 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308851 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008852 EXTENT_DIRTY | EXTENT_DELALLOC |
8853 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8854 EXTENT_DEFRAG, 1, 0, &cached_state,
8855 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008856 /*
8857 * whoever cleared the private bit is responsible
8858 * for the finish_ordered_io
8859 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008860 if (TestClearPagePrivate2(page)) {
8861 struct btrfs_ordered_inode_tree *tree;
8862 u64 new_len;
8863
8864 tree = &BTRFS_I(inode)->ordered_tree;
8865
8866 spin_lock_irq(&tree->lock);
8867 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008869 if (new_len < ordered->truncated_len)
8870 ordered->truncated_len = new_len;
8871 spin_unlock_irq(&tree->lock);
8872
8873 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874 start,
8875 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008876 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008877 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008878 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879 if (!inode_evicting) {
8880 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308881 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008882 &cached_state);
8883 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308884
8885 start = end + 1;
8886 if (start < page_end)
8887 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008888 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008889
Qu Wenruob9d0b382015-09-29 10:35:16 +08008890 /*
8891 * Qgroup reserved space handler
8892 * Page here will be either
8893 * 1) Already written to disk
8894 * In this case, its reserved space is released from data rsv map
8895 * and will be freed by delayed_ref handler finally.
8896 * So even we call qgroup_free_data(), it won't decrease reserved
8897 * space.
8898 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008899 * This means the reserved space should be freed here. However,
8900 * if a truncate invalidates the page (by clearing PageDirty)
8901 * and the page is accounted for while allocating extent
8902 * in btrfs_check_data_free_space() we let delayed_ref to
8903 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008904 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008905 if (PageDirty(page))
8906 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008907 if (!inode_evicting) {
8908 clear_extent_bit(tree, page_start, page_end,
8909 EXTENT_LOCKED | EXTENT_DIRTY |
8910 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8911 EXTENT_DEFRAG, 1, 1,
8912 &cached_state, GFP_NOFS);
8913
8914 __btrfs_releasepage(page, GFP_NOFS);
8915 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916
Chris Mason4a096752008-07-21 10:29:44 -04008917 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008918 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008919 ClearPagePrivate(page);
8920 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008921 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008922 }
Chris Mason39279cc2007-06-12 06:35:45 -04008923}
8924
Chris Mason9ebefb182007-06-15 13:50:00 -04008925/*
8926 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8927 * called from a page fault handler when a page is first dirtied. Hence we must
8928 * be careful to check for EOF conditions here. We set the page up correctly
8929 * for a written page which means we get ENOSPC checking when writing into
8930 * holes and correct delalloc and unwritten extent mapping on filesystems that
8931 * support these features.
8932 *
8933 * We are not allowed to take the i_mutex here so we have to play games to
8934 * protect against truncate races as the page could now be beyond EOF. Because
8935 * vmtruncate() writes the inode size before removing pages, once we have the
8936 * page lock we can determine safely if the page is beyond EOF. If it is not
8937 * beyond EOF, then the page is guaranteed safe against truncation until we
8938 * unlock the page.
8939 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008940int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008941{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008942 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008943 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8946 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008947 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948 char *kaddr;
8949 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008951 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008952 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308953 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008954 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308956 u64 end;
8957
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008958 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008959
Jan Karab2b5ef52012-06-12 16:20:45 +02008960 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008961 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008962 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308963 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008964
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308965 /*
8966 * Reserving delalloc space after obtaining the page lock can lead to
8967 * deadlock. For example, if a dirty page is locked by this function
8968 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8969 * dirty page write out, then the btrfs_writepage() function could
8970 * end up waiting indefinitely to get a lock on the page currently
8971 * being processed by btrfs_page_mkwrite() function.
8972 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008973 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308974 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008975 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008976 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008977 reserved = 1;
8978 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008979 if (ret) {
8980 if (ret == -ENOMEM)
8981 ret = VM_FAULT_OOM;
8982 else /* -ENOSPC, -EIO, etc */
8983 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008984 if (reserved)
8985 goto out;
8986 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008987 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008988
Nick Piggin56a76f82009-03-31 15:23:23 -07008989 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008990again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008991 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008992 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008993
Chris Mason9ebefb182007-06-15 13:50:00 -04008994 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008996 /* page got truncated out from underneath us */
8997 goto out_unlock;
8998 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008999 wait_on_page_writeback(page);
9000
David Sterbaff13db42015-12-03 14:30:40 +01009001 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009002 set_page_extent_mapped(page);
9003
Chris Masoneb84ae02008-07-17 13:53:27 -04009004 /*
9005 * we can't set the delalloc bits if there are pending ordered
9006 * extents. Drop our locks and wait for them to finish
9007 */
Liu Bo9a9239a2017-01-24 15:58:51 -08009008 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009010 unlock_extent_cached(io_tree, page_start, page_end,
9011 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009013 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 btrfs_put_ordered_extent(ordered);
9015 goto again;
9016 }
9017
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009019 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009020 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 end = page_start + reserved_space - 1;
9023 spin_lock(&BTRFS_I(inode)->lock);
9024 BTRFS_I(inode)->outstanding_extents++;
9025 spin_unlock(&BTRFS_I(inode)->lock);
9026 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309028 }
9029 }
9030
Josef Bacikfbf19082009-10-01 17:10:23 -04009031 /*
Liu Bo54160342016-12-13 12:15:19 -08009032 * page_mkwrite gets called when the page is firstly dirtied after it's
9033 * faulted in, but write(2) could also dirty a page and set delalloc
9034 * bits, thus in this case for space account reason, we still need to
9035 * clear any delalloc bits within this page range since we have to
9036 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009037 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309038 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009039 EXTENT_DIRTY | EXTENT_DELALLOC |
9040 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009041 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009042
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309043 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009044 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009045 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009046 unlock_extent_cached(io_tree, page_start, page_end,
9047 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009048 ret = VM_FAULT_SIGBUS;
9049 goto out_unlock;
9050 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009051 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009052
9053 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009054 if (page_start + PAGE_SIZE > size)
9055 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009056 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009059 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009060 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009061 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009062 flush_dcache_page(page);
9063 kunmap(page);
9064 }
Chris Mason247e7432008-07-17 12:53:51 -04009065 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009066 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009067 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009068
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009069 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009070 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009071 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009072
Josef Bacik2ac55d42010-02-03 19:33:23 +00009073 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009074
9075out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009076 if (!ret) {
9077 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009078 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009079 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009080 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009081out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309082 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009083out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009084 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009085 return ret;
9086}
9087
Josef Bacika41ad392011-01-31 15:30:16 -05009088static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009089{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009091 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009092 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009093 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009094 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009095 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009096 u64 mask = fs_info->sectorsize - 1;
9097 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009098
Josef Bacik0ef8b722013-10-25 16:13:35 -04009099 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9100 (u64)-1);
9101 if (ret)
9102 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009103
Josef Bacikfcb80c22011-05-03 10:40:22 -04009104 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009105 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009106 * 3 things going on here
9107 *
9108 * 1) We need to reserve space for our orphan item and the space to
9109 * delete our orphan item. Lord knows we don't want to have a dangling
9110 * orphan item because we didn't reserve space to remove it.
9111 *
9112 * 2) We need to reserve space to update our inode.
9113 *
9114 * 3) We need to have something to cache all the space that is going to
9115 * be free'd up by the truncate operation, but also have some slack
9116 * space reserved in case it uses space during the truncate (thank you
9117 * very much snapshotting).
9118 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009119 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009120 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009121 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009122 * doesn't end up using space reserved for updating the inode or
9123 * removing the orphan item. We also need to be able to stop the
9124 * transaction and start a new one, which means we need to be able to
9125 * update the inode several times, and we have no idea of knowing how
9126 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009127 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009128 * Then there is the orphan item, which does indeed need to be held on
9129 * to for the whole operation, and we need nobody to touch this reserved
9130 * space except the orphan code.
9131 *
9132 * So that leaves us with
9133 *
9134 * 1) root->orphan_block_rsv - for the orphan deletion.
9135 * 2) rsv - for the truncate reservation, which we will steal from the
9136 * transaction reservation.
9137 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9138 * updating the inode.
9139 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009140 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009141 if (!rsv)
9142 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009143 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009144 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009145
Josef Bacik907cbce2011-08-08 13:46:15 -04009146 /*
Josef Bacik07127182011-08-19 10:29:59 -04009147 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009148 * 1 for updating the inode.
9149 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009150 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009151 if (IS_ERR(trans)) {
9152 err = PTR_ERR(trans);
9153 goto out;
9154 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009155
Josef Bacik907cbce2011-08-08 13:46:15 -04009156 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009157 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009158 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009159 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009160
Chris Mason5a3f23d2009-03-31 13:27:11 -04009161 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009162 * So if we truncate and then write and fsync we normally would just
9163 * write the extents that changed, which is a problem if we need to
9164 * first truncate that entire inode. So set this flag so we write out
9165 * all of the extents in the inode to the sync log so we're completely
9166 * safe.
9167 */
9168 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009169 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009170
Yan, Zheng80825102009-11-12 09:35:36 +00009171 while (1) {
9172 ret = btrfs_truncate_inode_items(trans, root, inode,
9173 inode->i_size,
9174 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009175 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009176 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009177 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009178 }
Chris Mason39279cc2007-06-12 06:35:45 -04009179
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009180 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009181 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009182 if (ret) {
9183 err = ret;
9184 break;
9185 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009186
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009187 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009188 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009189
9190 trans = btrfs_start_transaction(root, 2);
9191 if (IS_ERR(trans)) {
9192 ret = err = PTR_ERR(trans);
9193 trans = NULL;
9194 break;
9195 }
9196
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009197 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009198 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009199 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009200 BUG_ON(ret); /* shouldn't happen */
9201 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009202 }
9203
9204 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009205 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009206 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009207 if (ret)
9208 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009209 }
9210
Chris Mason917c16b2011-11-08 14:49:59 -05009211 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009212 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009213 ret = btrfs_update_inode(trans, root, inode);
9214 if (ret && !err)
9215 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009216
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009217 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009218 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009219 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009221 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009222
Josef Bacik3893e332011-01-31 16:03:11 -05009223 if (ret && !err)
9224 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009225
Josef Bacik3893e332011-01-31 16:03:11 -05009226 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009227}
9228
Sven Wegener3b963622008-06-09 21:57:42 -04009229/*
Chris Masond352ac62008-09-29 15:18:18 -04009230 * create a new subvolume directory/inode (helper for the ioctl).
9231 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009232int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009233 struct btrfs_root *new_root,
9234 struct btrfs_root *parent_root,
9235 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009236{
Chris Mason39279cc2007-06-12 06:35:45 -04009237 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009238 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009239 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009240
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009241 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9242 new_dirid, new_dirid,
9243 S_IFDIR | (~current_umask() & S_IRWXUGO),
9244 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009245 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009246 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009247 inode->i_op = &btrfs_dir_inode_operations;
9248 inode->i_fop = &btrfs_dir_file_operations;
9249
Miklos Szeredibfe86842011-10-28 14:13:29 +02009250 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009251 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009252 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009253
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009254 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9255 if (err)
9256 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009257 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009258 new_root->root_key.objectid, err);
9259
Yan, Zheng76dda932009-09-21 16:00:26 -04009260 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009261
Yan, Zheng76dda932009-09-21 16:00:26 -04009262 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009263 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009264}
9265
Chris Mason39279cc2007-06-12 06:35:45 -04009266struct inode *btrfs_alloc_inode(struct super_block *sb)
9267{
9268 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009270
9271 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9272 if (!ei)
9273 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274
9275 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009276 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009277 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009278 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009279 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009280 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009281 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009282 ei->disk_i_size = 0;
9283 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009284 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009286 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009288 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009289 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009290
Josef Bacik9e0baf62011-07-15 15:16:44 +00009291 spin_lock_init(&ei->lock);
9292 ei->outstanding_extents = 0;
9293 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009294
Josef Bacik72ac3c02012-05-23 14:13:11 -04009295 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009296 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009297
Miao Xie16cdcec2011-04-22 18:12:22 +08009298 ei->delayed_node = NULL;
9299
chandan r9cc97d62012-07-04 12:48:07 +05309300 ei->i_otime.tv_sec = 0;
9301 ei->i_otime.tv_nsec = 0;
9302
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009304 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009305 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9306 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009307 ei->io_tree.track_uptodate = 1;
9308 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009309 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009311 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009312 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009314 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009316 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317
9318 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009319}
9320
Josef Bacikaaedb552013-10-11 14:44:09 -04009321#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9322void btrfs_test_destroy_inode(struct inode *inode)
9323{
9324 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9325 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9326}
9327#endif
9328
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009329static void btrfs_i_callback(struct rcu_head *head)
9330{
9331 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009332 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9333}
9334
Chris Mason39279cc2007-06-12 06:35:45 -04009335void btrfs_destroy_inode(struct inode *inode)
9336{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009337 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009338 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009339 struct btrfs_root *root = BTRFS_I(inode)->root;
9340
Al Virob3d9b7a2012-06-09 13:51:19 -04009341 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009342 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009343 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9344 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009345 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9346 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009347 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009348
Chris Mason5a3f23d2009-03-31 13:27:11 -04009349 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009350 * This can happen where we create an inode, but somebody else also
9351 * created the same inode and we need to destroy the one we already
9352 * created.
9353 */
9354 if (!root)
9355 goto free;
9356
Josef Bacik8a35d952012-05-23 14:26:42 -04009357 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9358 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009359 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009360 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009361 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009362 }
Josef Bacik7b128762008-07-24 12:17:14 -04009363
Chris Masond3977122009-01-05 21:25:51 -05009364 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009365 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9366 if (!ordered)
9367 break;
9368 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009369 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009370 "found ordered extent %llu %llu on inode cleanup",
9371 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009372 btrfs_remove_ordered_extent(inode, ordered);
9373 btrfs_put_ordered_extent(ordered);
9374 btrfs_put_ordered_extent(ordered);
9375 }
9376 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009377 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009378 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009379 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009380free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009381 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009382}
9383
Al Viro45321ac2010-06-07 13:43:19 -04009384int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009385{
9386 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009387
Naohiro Aota6379ef92013-06-06 09:56:34 +00009388 if (root == NULL)
9389 return 1;
9390
Liu Bofa6ac872013-02-20 14:10:23 +00009391 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009392 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009393 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009394 else
Al Viro45321ac2010-06-07 13:43:19 -04009395 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009396}
9397
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009398static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009399{
9400 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9401
9402 inode_init_once(&ei->vfs_inode);
9403}
9404
9405void btrfs_destroy_cachep(void)
9406{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009407 /*
9408 * Make sure all delayed rcu free inodes are flushed before we
9409 * destroy cache.
9410 */
9411 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009412 kmem_cache_destroy(btrfs_inode_cachep);
9413 kmem_cache_destroy(btrfs_trans_handle_cachep);
9414 kmem_cache_destroy(btrfs_transaction_cachep);
9415 kmem_cache_destroy(btrfs_path_cachep);
9416 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009417}
9418
9419int btrfs_init_cachep(void)
9420{
David Sterba837e1972012-09-07 03:00:48 -06009421 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009422 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009423 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9424 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009425 if (!btrfs_inode_cachep)
9426 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009427
David Sterba837e1972012-09-07 03:00:48 -06009428 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009429 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009430 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009431 if (!btrfs_trans_handle_cachep)
9432 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009433
David Sterba837e1972012-09-07 03:00:48 -06009434 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009435 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009436 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009437 if (!btrfs_transaction_cachep)
9438 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009439
David Sterba837e1972012-09-07 03:00:48 -06009440 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009441 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009442 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009443 if (!btrfs_path_cachep)
9444 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009445
David Sterba837e1972012-09-07 03:00:48 -06009446 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009447 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009448 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009449 if (!btrfs_free_space_cachep)
9450 goto fail;
9451
Chris Mason39279cc2007-06-12 06:35:45 -04009452 return 0;
9453fail:
9454 btrfs_destroy_cachep();
9455 return -ENOMEM;
9456}
9457
9458static int btrfs_getattr(struct vfsmount *mnt,
9459 struct dentry *dentry, struct kstat *stat)
9460{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009461 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009462 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009463 u32 blocksize = inode->i_sb->s_blocksize;
9464
Chris Mason39279cc2007-06-12 06:35:45 -04009465 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009466 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009467
9468 spin_lock(&BTRFS_I(inode)->lock);
9469 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9470 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009471 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009472 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009473 return 0;
9474}
9475
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009476static int btrfs_rename_exchange(struct inode *old_dir,
9477 struct dentry *old_dentry,
9478 struct inode *new_dir,
9479 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009481 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009482 struct btrfs_trans_handle *trans;
9483 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009484 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 struct inode *new_inode = new_dentry->d_inode;
9486 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009487 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009489 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9490 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 u64 old_idx = 0;
9492 u64 new_idx = 0;
9493 u64 root_objectid;
9494 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009495 bool root_log_pinned = false;
9496 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497
9498 /* we only allow rename subvolume link between subvolumes */
9499 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9500 return -EXDEV;
9501
9502 /* close the race window with snapshot create/destroy ioctl */
9503 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009504 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009506 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507
9508 /*
9509 * We want to reserve the absolute worst case amount of items. So if
9510 * both inodes are subvols and we need to unlink them then that would
9511 * require 4 item modifications, but if they are both normal inodes it
9512 * would require 5 item modifications, so we'll assume their normal
9513 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9514 * should cover the worst case number of items we'll modify.
9515 */
9516 trans = btrfs_start_transaction(root, 12);
9517 if (IS_ERR(trans)) {
9518 ret = PTR_ERR(trans);
9519 goto out_notrans;
9520 }
9521
9522 /*
9523 * We need to find a free sequence number both in the source and
9524 * in the destination directory for the exchange.
9525 */
9526 ret = btrfs_set_inode_index(new_dir, &old_idx);
9527 if (ret)
9528 goto out_fail;
9529 ret = btrfs_set_inode_index(old_dir, &new_idx);
9530 if (ret)
9531 goto out_fail;
9532
9533 BTRFS_I(old_inode)->dir_index = 0ULL;
9534 BTRFS_I(new_inode)->dir_index = 0ULL;
9535
9536 /* Reference for the source. */
9537 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9538 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009539 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009541 btrfs_pin_log_trans(root);
9542 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 ret = btrfs_insert_inode_ref(trans, dest,
9544 new_dentry->d_name.name,
9545 new_dentry->d_name.len,
9546 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009547 btrfs_ino(BTRFS_I(new_dir)),
9548 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 if (ret)
9550 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 }
9552
9553 /* And now for the dest. */
9554 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9555 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009556 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009558 btrfs_pin_log_trans(dest);
9559 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 ret = btrfs_insert_inode_ref(trans, root,
9561 old_dentry->d_name.name,
9562 old_dentry->d_name.len,
9563 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009564 btrfs_ino(BTRFS_I(old_dir)),
9565 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 if (ret)
9567 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009568 }
9569
9570 /* Update inode version and ctime/mtime. */
9571 inode_inc_iversion(old_dir);
9572 inode_inc_iversion(new_dir);
9573 inode_inc_iversion(old_inode);
9574 inode_inc_iversion(new_inode);
9575 old_dir->i_ctime = old_dir->i_mtime = ctime;
9576 new_dir->i_ctime = new_dir->i_mtime = ctime;
9577 old_inode->i_ctime = ctime;
9578 new_inode->i_ctime = ctime;
9579
9580 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009581 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9582 BTRFS_I(old_inode), 1);
9583 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9584 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 }
9586
9587 /* src is a subvolume */
9588 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9589 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9590 ret = btrfs_unlink_subvol(trans, root, old_dir,
9591 root_objectid,
9592 old_dentry->d_name.name,
9593 old_dentry->d_name.len);
9594 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009595 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9596 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 old_dentry->d_name.name,
9598 old_dentry->d_name.len);
9599 if (!ret)
9600 ret = btrfs_update_inode(trans, root, old_inode);
9601 }
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 /* dest is a subvolume */
9608 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9609 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9610 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9611 root_objectid,
9612 new_dentry->d_name.name,
9613 new_dentry->d_name.len);
9614 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009615 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9616 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 new_dentry->d_name.name,
9618 new_dentry->d_name.len);
9619 if (!ret)
9620 ret = btrfs_update_inode(trans, dest, new_inode);
9621 }
9622 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009623 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 goto out_fail;
9625 }
9626
9627 ret = btrfs_add_link(trans, new_dir, old_inode,
9628 new_dentry->d_name.name,
9629 new_dentry->d_name.len, 0, old_idx);
9630 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009631 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009632 goto out_fail;
9633 }
9634
9635 ret = btrfs_add_link(trans, old_dir, new_inode,
9636 old_dentry->d_name.name,
9637 old_dentry->d_name.len, 0, new_idx);
9638 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009639 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 goto out_fail;
9641 }
9642
9643 if (old_inode->i_nlink == 1)
9644 BTRFS_I(old_inode)->dir_index = old_idx;
9645 if (new_inode->i_nlink == 1)
9646 BTRFS_I(new_inode)->dir_index = new_idx;
9647
Filipe Manana86e8aa02016-05-05 02:02:27 +01009648 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009650 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9651 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009653 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009655 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009657 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9658 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009660 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 }
9662out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009663 /*
9664 * If we have pinned a log and an error happened, we unpin tasks
9665 * trying to sync the log and force them to fallback to a transaction
9666 * commit if the log currently contains any of the inodes involved in
9667 * this rename operation (to ensure we do not persist a log with an
9668 * inconsistent state for any of these inodes or leading to any
9669 * inconsistencies when replayed). If the transaction was aborted, the
9670 * abortion reason is propagated to userspace when attempting to commit
9671 * the transaction. If the log does not contain any of these inodes, we
9672 * allow the tasks to sync it.
9673 */
9674 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009675 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9676 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9677 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009678 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009679 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009680 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009681
9682 if (root_log_pinned) {
9683 btrfs_end_log_trans(root);
9684 root_log_pinned = false;
9685 }
9686 if (dest_log_pinned) {
9687 btrfs_end_log_trans(dest);
9688 dest_log_pinned = false;
9689 }
9690 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009691 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692out_notrans:
9693 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009694 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697
9698 return ret;
9699}
9700
9701static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9702 struct btrfs_root *root,
9703 struct inode *dir,
9704 struct dentry *dentry)
9705{
9706 int ret;
9707 struct inode *inode;
9708 u64 objectid;
9709 u64 index;
9710
9711 ret = btrfs_find_free_ino(root, &objectid);
9712 if (ret)
9713 return ret;
9714
9715 inode = btrfs_new_inode(trans, root, dir,
9716 dentry->d_name.name,
9717 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009718 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 objectid,
9720 S_IFCHR | WHITEOUT_MODE,
9721 &index);
9722
9723 if (IS_ERR(inode)) {
9724 ret = PTR_ERR(inode);
9725 return ret;
9726 }
9727
9728 inode->i_op = &btrfs_special_inode_operations;
9729 init_special_inode(inode, inode->i_mode,
9730 WHITEOUT_DEV);
9731
9732 ret = btrfs_init_inode_security(trans, inode, dir,
9733 &dentry->d_name);
9734 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009735 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736
9737 ret = btrfs_add_nondir(trans, dir, dentry,
9738 inode, 0, index);
9739 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009740 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741
9742 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009743out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009745 if (ret)
9746 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747 iput(inode);
9748
Filipe Mananac9901612016-05-05 01:41:57 +01009749 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750}
9751
Chris Mason39279cc2007-06-12 06:35:45 -04009752static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753 struct inode *new_dir, struct dentry *new_dentry,
9754 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009756 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009757 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009758 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009759 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9760 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009761 struct inode *new_inode = d_inode(new_dentry);
9762 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009763 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009764 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009765 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009766 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009767 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009768
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009769 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009770 return -EPERM;
9771
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009772 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009773 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009774 return -EXDEV;
9775
Li Zefan33345d012011-04-20 10:31:50 +08009776 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009777 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009778 return -ENOTEMPTY;
9779
Chris Mason39279cc2007-06-12 06:35:45 -04009780 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009781 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009782 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009783
9784
9785 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009786 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009787 new_dentry->d_name.name,
9788 new_dentry->d_name.len);
9789
9790 if (ret) {
9791 if (ret == -EEXIST) {
9792 /* we shouldn't get
9793 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309794 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009795 return ret;
9796 }
9797 } else {
9798 /* maybe -EOVERFLOW */
9799 return ret;
9800 }
9801 }
9802 ret = 0;
9803
Chris Mason5a3f23d2009-03-31 13:27:11 -04009804 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009805 * we're using rename to replace one file with another. Start IO on it
9806 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009807 */
Chris Mason8d875f92014-08-12 10:47:42 -07009808 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009809 filemap_flush(old_inode->i_mapping);
9810
Yan, Zheng76dda932009-09-21 16:00:26 -04009811 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009812 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009813 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009814 /*
9815 * We want to reserve the absolute worst case amount of items. So if
9816 * both inodes are subvols and we need to unlink them then that would
9817 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009819 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9820 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009821 * If our rename has the whiteout flag, we need more 5 units for the
9822 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9823 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009824 */
Filipe Manana5062af32016-05-05 10:26:26 +01009825 trans_num_items = 11;
9826 if (flags & RENAME_WHITEOUT)
9827 trans_num_items += 5;
9828 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009829 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009830 ret = PTR_ERR(trans);
9831 goto out_notrans;
9832 }
Chris Mason5f39d392007-10-15 16:14:19 -04009833
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009834 if (dest != root)
9835 btrfs_record_root_in_trans(trans, dest);
9836
Yan, Zhenga5719522009-09-24 09:17:31 -04009837 ret = btrfs_set_inode_index(new_dir, &index);
9838 if (ret)
9839 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009840
Miao Xie67de1172013-12-26 13:07:06 +08009841 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009842 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009844 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009846 btrfs_pin_log_trans(root);
9847 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009848 ret = btrfs_insert_inode_ref(trans, dest,
9849 new_dentry->d_name.name,
9850 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009851 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009852 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009853 if (ret)
9854 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009855 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009856
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009857 inode_inc_iversion(old_dir);
9858 inode_inc_iversion(new_dir);
9859 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009860 old_dir->i_ctime = old_dir->i_mtime =
9861 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009862 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009863
Chris Mason12fcfd22009-03-24 10:24:20 -04009864 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009865 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9866 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009867
Li Zefan33345d012011-04-20 10:31:50 +08009868 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9870 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9871 old_dentry->d_name.name,
9872 old_dentry->d_name.len);
9873 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009874 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9875 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009876 old_dentry->d_name.name,
9877 old_dentry->d_name.len);
9878 if (!ret)
9879 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009880 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009882 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009883 goto out_fail;
9884 }
Chris Mason39279cc2007-06-12 06:35:45 -04009885
9886 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009887 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009888 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009889 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9891 root_objectid = BTRFS_I(new_inode)->location.objectid;
9892 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9893 root_objectid,
9894 new_dentry->d_name.name,
9895 new_dentry->d_name.len);
9896 BUG_ON(new_inode->i_nlink == 0);
9897 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009898 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9899 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 new_dentry->d_name.name,
9901 new_dentry->d_name.len);
9902 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009903 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009904 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009905 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009906 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009907 goto out_fail;
9908 }
Chris Mason39279cc2007-06-12 06:35:45 -04009909 }
Josef Bacikaec74772008-07-24 12:12:38 -04009910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009911 ret = btrfs_add_link(trans, new_dir, old_inode,
9912 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009913 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009915 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009916 goto out_fail;
9917 }
Chris Mason39279cc2007-06-12 06:35:45 -04009918
Miao Xie67de1172013-12-26 13:07:06 +08009919 if (old_inode->i_nlink == 1)
9920 BTRFS_I(old_inode)->dir_index = index;
9921
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009922 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009923 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009924
David Sterbaf85b7372017-01-20 14:54:07 +01009925 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9926 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009928 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009930
9931 if (flags & RENAME_WHITEOUT) {
9932 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9933 old_dentry);
9934
9935 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009936 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009937 goto out_fail;
9938 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009939 }
Chris Mason39279cc2007-06-12 06:35:45 -04009940out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009941 /*
9942 * If we have pinned the log and an error happened, we unpin tasks
9943 * trying to sync the log and force them to fallback to a transaction
9944 * commit if the log currently contains any of the inodes involved in
9945 * this rename operation (to ensure we do not persist a log with an
9946 * inconsistent state for any of these inodes or leading to any
9947 * inconsistencies when replayed). If the transaction was aborted, the
9948 * abortion reason is propagated to userspace when attempting to commit
9949 * the transaction. If the log does not contain any of these inodes, we
9950 * allow the tasks to sync it.
9951 */
9952 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009953 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9954 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9955 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009956 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009957 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009958 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009959
9960 btrfs_end_log_trans(root);
9961 log_pinned = false;
9962 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009963 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009964out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009965 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009966 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009967
Chris Mason39279cc2007-06-12 06:35:45 -04009968 return ret;
9969}
9970
Miklos Szeredi80ace852014-07-23 15:15:32 +02009971static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9972 struct inode *new_dir, struct dentry *new_dentry,
9973 unsigned int flags)
9974{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009975 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009976 return -EINVAL;
9977
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009978 if (flags & RENAME_EXCHANGE)
9979 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9980 new_dentry);
9981
9982 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009983}
9984
Miao Xie8ccf6f192012-10-25 09:28:04 +00009985static void btrfs_run_delalloc_work(struct btrfs_work *work)
9986{
9987 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009988 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009989
9990 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9991 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009992 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009993 filemap_flush(inode->i_mapping);
9994 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9995 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009996 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997
9998 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009999 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010001 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002 complete(&delalloc_work->completion);
10003}
10004
10005struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010006 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007{
10008 struct btrfs_delalloc_work *work;
10009
David Sterba100d5702015-12-08 14:39:32 +010010010 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010011 if (!work)
10012 return NULL;
10013
10014 init_completion(&work->completion);
10015 INIT_LIST_HEAD(&work->list);
10016 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010017 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010018 WARN_ON_ONCE(!inode);
10019 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10020 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010021
10022 return work;
10023}
10024
10025void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10026{
10027 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010028 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010029}
10030
Chris Masond352ac62008-09-29 15:18:18 -040010031/*
10032 * some fairly slow code that needs optimization. This walks the list
10033 * of all the inodes with pending delalloc and forces them to disk.
10034 */
Miao Xie6c255e62014-03-06 13:55:01 +080010035static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10036 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010037{
Chris Masonea8c2812008-08-04 23:17:27 -040010038 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010039 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040 struct btrfs_delalloc_work *work, *next;
10041 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010042 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010043 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010044
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010046 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010047
Miao Xie573bfb72014-03-06 13:55:03 +080010048 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 spin_lock(&root->delalloc_lock);
10050 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010051 while (!list_empty(&splice)) {
10052 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010053 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010054
Miao Xieeb73c1b2013-05-15 07:48:22 +000010055 list_move_tail(&binode->delalloc_inodes,
10056 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010057 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010058 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010059 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010060 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010061 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010063
David Sterba651d4942015-11-27 19:24:16 +010010064 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010065 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010066 if (delay_iput)
10067 btrfs_add_delayed_iput(inode);
10068 else
10069 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010070 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010071 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010072 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010073 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010074 btrfs_queue_work(root->fs_info->flush_workers,
10075 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010076 ret++;
10077 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010078 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010079 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010081 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010083
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010084out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010085 list_for_each_entry_safe(work, next, &works, list) {
10086 list_del_init(&work->list);
10087 btrfs_wait_and_free_delalloc_work(work);
10088 }
10089
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 if (!list_empty_careful(&splice)) {
10091 spin_lock(&root->delalloc_lock);
10092 list_splice_tail(&splice, &root->delalloc_inodes);
10093 spin_unlock(&root->delalloc_lock);
10094 }
Miao Xie573bfb72014-03-06 13:55:03 +080010095 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 return ret;
10097}
10098
10099int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10100{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010101 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010102 int ret;
10103
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010104 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105 return -EROFS;
10106
Miao Xie6c255e62014-03-06 13:55:01 +080010107 ret = __start_delalloc_inodes(root, delay_iput, -1);
10108 if (ret > 0)
10109 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 /*
10111 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010112 * we have to make sure the IO is actually started and that
10113 * ordered extents get created before we return
10114 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010115 atomic_inc(&fs_info->async_submit_draining);
10116 while (atomic_read(&fs_info->nr_async_submits) ||
10117 atomic_read(&fs_info->async_delalloc_pages)) {
10118 wait_event(fs_info->async_submit_wait,
10119 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10120 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010121 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010122 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 return ret;
10124}
10125
Miao Xie6c255e62014-03-06 13:55:01 +080010126int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10127 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128{
10129 struct btrfs_root *root;
10130 struct list_head splice;
10131 int ret;
10132
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010133 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010134 return -EROFS;
10135
10136 INIT_LIST_HEAD(&splice);
10137
Miao Xie573bfb72014-03-06 13:55:03 +080010138 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010139 spin_lock(&fs_info->delalloc_root_lock);
10140 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010141 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010142 root = list_first_entry(&splice, struct btrfs_root,
10143 delalloc_root);
10144 root = btrfs_grab_fs_root(root);
10145 BUG_ON(!root);
10146 list_move_tail(&root->delalloc_root,
10147 &fs_info->delalloc_roots);
10148 spin_unlock(&fs_info->delalloc_root_lock);
10149
Miao Xie6c255e62014-03-06 13:55:01 +080010150 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010152 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010153 goto out;
10154
Miao Xie6c255e62014-03-06 13:55:01 +080010155 if (nr != -1) {
10156 nr -= ret;
10157 WARN_ON(nr < 0);
10158 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 spin_lock(&fs_info->delalloc_root_lock);
10160 }
10161 spin_unlock(&fs_info->delalloc_root_lock);
10162
Miao Xie6c255e62014-03-06 13:55:01 +080010163 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010164 atomic_inc(&fs_info->async_submit_draining);
10165 while (atomic_read(&fs_info->nr_async_submits) ||
10166 atomic_read(&fs_info->async_delalloc_pages)) {
10167 wait_event(fs_info->async_submit_wait,
10168 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10169 atomic_read(&fs_info->async_delalloc_pages) == 0));
10170 }
10171 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010172out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010173 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174 spin_lock(&fs_info->delalloc_root_lock);
10175 list_splice_tail(&splice, &fs_info->delalloc_roots);
10176 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010177 }
Miao Xie573bfb72014-03-06 13:55:03 +080010178 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010179 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010180}
10181
Chris Mason39279cc2007-06-12 06:35:45 -040010182static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10183 const char *symname)
10184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010185 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010186 struct btrfs_trans_handle *trans;
10187 struct btrfs_root *root = BTRFS_I(dir)->root;
10188 struct btrfs_path *path;
10189 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010190 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010191 int err;
10192 int drop_inode = 0;
10193 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010194 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010195 int name_len;
10196 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010197 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010198 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010199 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010200
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010201 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010202 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010203 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010204
Josef Bacik9ed74f22009-09-11 16:12:44 -040010205 /*
10206 * 2 items for inode item and ref
10207 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010208 * 1 item for updating parent inode item
10209 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010210 * 1 item for xattr if selinux is on
10211 */
Filipe Manana9269d122015-12-31 18:16:29 +000010212 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010213 if (IS_ERR(trans))
10214 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010215
Li Zefan581bb052011-04-20 10:06:11 +080010216 err = btrfs_find_free_ino(root, &objectid);
10217 if (err)
10218 goto out_unlock;
10219
Josef Bacikaec74772008-07-24 12:12:38 -040010220 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010221 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10222 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010223 if (IS_ERR(inode)) {
10224 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010225 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010226 }
Chris Mason39279cc2007-06-12 06:35:45 -040010227
Casey Schauflerad19db72011-12-15 10:09:07 -050010228 /*
10229 * If the active LSM wants to access the inode during
10230 * d_instantiate it needs these. Smack checks to see
10231 * if the filesystem supports xattrs by looking at the
10232 * ops vector.
10233 */
10234 inode->i_fop = &btrfs_file_operations;
10235 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010236 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010237 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10238
10239 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10240 if (err)
10241 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010242
Chris Mason39279cc2007-06-12 06:35:45 -040010243 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010244 if (!path) {
10245 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010246 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010247 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010248 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010249 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010250 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010251 datasize = btrfs_file_extent_calc_inline_size(name_len);
10252 err = btrfs_insert_empty_item(trans, root, path, &key,
10253 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010254 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010255 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010256 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010257 }
Chris Mason5f39d392007-10-15 16:14:19 -040010258 leaf = path->nodes[0];
10259 ei = btrfs_item_ptr(leaf, path->slots[0],
10260 struct btrfs_file_extent_item);
10261 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10262 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010263 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010264 btrfs_set_file_extent_encryption(leaf, ei, 0);
10265 btrfs_set_file_extent_compression(leaf, ei, 0);
10266 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10267 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10268
Chris Mason39279cc2007-06-12 06:35:45 -040010269 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010270 write_extent_buffer(leaf, symname, ptr, name_len);
10271 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010272 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010273
Chris Mason39279cc2007-06-12 06:35:45 -040010274 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010275 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010276 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010277 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010278 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010279 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010280 /*
10281 * Last step, add directory indexes for our symlink inode. This is the
10282 * last step to avoid extra cleanup of these indexes if an error happens
10283 * elsewhere above.
10284 */
10285 if (!err)
10286 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010287 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010288 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010289 goto out_unlock_inode;
10290 }
10291
10292 unlock_new_inode(inode);
10293 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010294
10295out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010296 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010297 if (drop_inode) {
10298 inode_dec_link_count(inode);
10299 iput(inode);
10300 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010301 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010302 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010303
10304out_unlock_inode:
10305 drop_inode = 1;
10306 unlock_new_inode(inode);
10307 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010308}
Chris Mason16432982008-04-10 10:23:21 -040010309
Josef Bacik0af3d002010-06-21 14:48:16 -040010310static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10311 u64 start, u64 num_bytes, u64 min_size,
10312 loff_t actual_len, u64 *alloc_hint,
10313 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010316 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10317 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010318 struct btrfs_root *root = BTRFS_I(inode)->root;
10319 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010320 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010321 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010322 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010323 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010324 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010325 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010326 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010327
Josef Bacik0af3d002010-06-21 14:48:16 -040010328 if (trans)
10329 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010330 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010331 if (own_trans) {
10332 trans = btrfs_start_transaction(root, 3);
10333 if (IS_ERR(trans)) {
10334 ret = PTR_ERR(trans);
10335 break;
10336 }
Yan Zhengd899e052008-10-30 14:25:28 -040010337 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338
Byongho Leeee221842015-12-15 01:42:10 +090010339 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010340 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010341 /*
10342 * If we are severely fragmented we could end up with really
10343 * small allocations, so if the allocator is returning small
10344 * chunks lets make its job easier by only searching for those
10345 * sized chunks.
10346 */
10347 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010348 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10349 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010350 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010351 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010352 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010353 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010354 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010355 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010356
Josef Bacik0b670dc2015-09-23 17:11:16 -040010357 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010358 ret = insert_reserved_file_extent(trans, inode,
10359 cur_offset, ins.objectid,
10360 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010361 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010362 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010363 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010364 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010365 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010366 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010367 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010368 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010369 break;
10370 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010371
Chris Masona1ed8352009-09-11 12:27:37 -040010372 btrfs_drop_extent_cache(inode, cur_offset,
10373 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010374
Josef Bacik5dc562c2012-08-17 13:14:17 -040010375 em = alloc_extent_map();
10376 if (!em) {
10377 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10378 &BTRFS_I(inode)->runtime_flags);
10379 goto next;
10380 }
10381
10382 em->start = cur_offset;
10383 em->orig_start = cur_offset;
10384 em->len = ins.offset;
10385 em->block_start = ins.objectid;
10386 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010387 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010388 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010389 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010390 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10391 em->generation = trans->transid;
10392
10393 while (1) {
10394 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010395 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010396 write_unlock(&em_tree->lock);
10397 if (ret != -EEXIST)
10398 break;
10399 btrfs_drop_extent_cache(inode, cur_offset,
10400 cur_offset + ins.offset - 1,
10401 0);
10402 }
10403 free_extent_map(em);
10404next:
Yan Zhengd899e052008-10-30 14:25:28 -040010405 num_bytes -= ins.offset;
10406 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010407 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010408
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010409 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010410 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010411 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010412 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010413 (actual_len > inode->i_size) &&
10414 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010415 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010416 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010417 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010418 i_size = cur_offset;
10419 i_size_write(inode, i_size);
10420 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010421 }
10422
Yan Zhengd899e052008-10-30 14:25:28 -040010423 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010424
10425 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010426 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010427 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010428 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010429 break;
10430 }
Yan Zhengd899e052008-10-30 14:25:28 -040010431
Josef Bacik0af3d002010-06-21 14:48:16 -040010432 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010433 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010434 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010435 if (cur_offset < end)
10436 btrfs_free_reserved_data_space(inode, cur_offset,
10437 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010438 return ret;
10439}
10440
Josef Bacik0af3d002010-06-21 14:48:16 -040010441int btrfs_prealloc_file_range(struct inode *inode, int mode,
10442 u64 start, u64 num_bytes, u64 min_size,
10443 loff_t actual_len, u64 *alloc_hint)
10444{
10445 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10446 min_size, actual_len, alloc_hint,
10447 NULL);
10448}
10449
10450int btrfs_prealloc_file_range_trans(struct inode *inode,
10451 struct btrfs_trans_handle *trans, int mode,
10452 u64 start, u64 num_bytes, u64 min_size,
10453 loff_t actual_len, u64 *alloc_hint)
10454{
10455 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10456 min_size, actual_len, alloc_hint, trans);
10457}
10458
Chris Masone6dcd2d2008-07-17 12:53:50 -040010459static int btrfs_set_page_dirty(struct page *page)
10460{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010461 return __set_page_dirty_nobuffers(page);
10462}
10463
Al Viro10556cb2011-06-20 19:28:19 -040010464static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010465{
Li Zefanb83cc962010-12-20 16:04:08 +080010466 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010467 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010468
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010469 if (mask & MAY_WRITE &&
10470 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10471 if (btrfs_root_readonly(root))
10472 return -EROFS;
10473 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10474 return -EACCES;
10475 }
Al Viro2830ba72011-06-20 19:16:29 -040010476 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010477}
Chris Mason39279cc2007-06-12 06:35:45 -040010478
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010479static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10480{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010481 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482 struct btrfs_trans_handle *trans;
10483 struct btrfs_root *root = BTRFS_I(dir)->root;
10484 struct inode *inode = NULL;
10485 u64 objectid;
10486 u64 index;
10487 int ret = 0;
10488
10489 /*
10490 * 5 units required for adding orphan entry
10491 */
10492 trans = btrfs_start_transaction(root, 5);
10493 if (IS_ERR(trans))
10494 return PTR_ERR(trans);
10495
10496 ret = btrfs_find_free_ino(root, &objectid);
10497 if (ret)
10498 goto out;
10499
10500 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010501 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010502 if (IS_ERR(inode)) {
10503 ret = PTR_ERR(inode);
10504 inode = NULL;
10505 goto out;
10506 }
10507
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010508 inode->i_fop = &btrfs_file_operations;
10509 inode->i_op = &btrfs_file_inode_operations;
10510
10511 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010512 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10513
Chris Masonb0d5d102014-09-08 13:08:51 -070010514 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10515 if (ret)
10516 goto out_inode;
10517
10518 ret = btrfs_update_inode(trans, root, inode);
10519 if (ret)
10520 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010521 ret = btrfs_orphan_add(trans, inode);
10522 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010523 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010524
Filipe Manana5762b5c2014-08-01 00:10:32 +010010525 /*
10526 * We set number of links to 0 in btrfs_new_inode(), and here we set
10527 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10528 * through:
10529 *
10530 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10531 */
10532 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010533 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010534 d_tmpfile(dentry, inode);
10535 mark_inode_dirty(inode);
10536
10537out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010538 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010539 if (ret)
10540 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010541 btrfs_balance_delayed_items(fs_info);
10542 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010543 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010544
10545out_inode:
10546 unlock_new_inode(inode);
10547 goto out;
10548
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010549}
10550
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010551static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010552 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010553 .lookup = btrfs_lookup,
10554 .create = btrfs_create,
10555 .unlink = btrfs_unlink,
10556 .link = btrfs_link,
10557 .mkdir = btrfs_mkdir,
10558 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010559 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010560 .symlink = btrfs_symlink,
10561 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010562 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010563 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010564 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010565 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010566 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010567 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010568 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010569};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010570static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010571 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010572 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010573 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010574};
Yan, Zheng76dda932009-09-21 16:00:26 -040010575
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010576static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010577 .llseek = generic_file_llseek,
10578 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010579 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010580 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010581#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010582 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010583#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010584 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010585 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010586};
10587
David Sterba20e55062015-11-19 11:42:28 +010010588static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010589 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010590 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010591 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010592 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010593 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010594 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010595 .set_bit_hook = btrfs_set_bit_hook,
10596 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010597 .merge_extent_hook = btrfs_merge_extent_hook,
10598 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010599};
10600
Chris Mason35054392009-01-21 13:11:13 -050010601/*
10602 * btrfs doesn't support the bmap operation because swapfiles
10603 * use bmap to make a mapping of extents in the file. They assume
10604 * these extents won't change over the life of the file and they
10605 * use the bmap result to do IO directly to the drive.
10606 *
10607 * the btrfs bmap call would return logical addresses that aren't
10608 * suitable for IO and they also will change frequently as COW
10609 * operations happen. So, swapfile + btrfs == corruption.
10610 *
10611 * For now we're avoiding this by dropping bmap.
10612 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010613static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010614 .readpage = btrfs_readpage,
10615 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010616 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010617 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010618 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010619 .invalidatepage = btrfs_invalidatepage,
10620 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010621 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010622 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010623};
10624
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010625static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010626 .readpage = btrfs_readpage,
10627 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010628 .invalidatepage = btrfs_invalidatepage,
10629 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010630};
10631
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010632static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .getattr = btrfs_getattr,
10634 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010635 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010636 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010637 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010638 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010639 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010640 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010641};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010642static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010643 .getattr = btrfs_getattr,
10644 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010645 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010646 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010647 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010648 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010649 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010650};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010651static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010652 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010653 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010654 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010655 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010656 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010657 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010658};
Yan, Zheng76dda932009-09-21 16:00:26 -040010659
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010660const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010661 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010662 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010663};