blob: 151d192b88be3563131dbc27e95784629da4071c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400319 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400320 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400321out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800322 /*
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
327 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300328 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000329 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400330 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400331 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400332}
333
Chris Mason771ed682008-11-06 22:02:51 -0500334struct async_extent {
335 u64 start;
336 u64 ram_size;
337 u64 compressed_size;
338 struct page **pages;
339 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800340 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500341 struct list_head list;
342};
343
344struct async_cow {
345 struct inode *inode;
346 struct btrfs_root *root;
347 struct page *locked_page;
348 u64 start;
349 u64 end;
350 struct list_head extents;
351 struct btrfs_work work;
352};
353
354static noinline int add_async_extent(struct async_cow *cow,
355 u64 start, u64 ram_size,
356 u64 compressed_size,
357 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800358 unsigned long nr_pages,
359 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500360{
361 struct async_extent *async_extent;
362
363 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100364 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500365 async_extent->start = start;
366 async_extent->ram_size = ram_size;
367 async_extent->compressed_size = compressed_size;
368 async_extent->pages = pages;
369 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 list_add_tail(&async_extent->list, &cow->extents);
372 return 0;
373}
374
Wang Shilongf79707b2014-07-17 11:44:09 +0800375static inline int inode_need_compress(struct inode *inode)
376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800378
379 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400380 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800381 return 1;
382 /* bad compression ratios */
383 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
384 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400385 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800386 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 BTRFS_I(inode)->force_compress)
388 return 1;
389 return 0;
390}
391
Anand Jain26d30f82016-12-19 19:09:06 +0800392static inline void inode_should_defrag(struct inode *inode,
393 u64 start, u64 end, u64 num_bytes, u64 small_write)
394{
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes < small_write &&
397 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 btrfs_add_inode_defrag(NULL, inode);
399}
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401/*
Chris Mason771ed682008-11-06 22:02:51 -0500402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400405 *
Chris Mason771ed682008-11-06 22:02:51 -0500406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 *
Chris Mason771ed682008-11-06 22:02:51 -0500412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300415 * are written in the same order that the flusher thread sent them
416 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400417 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100418static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page,
420 u64 start, u64 end,
421 struct async_cow *async_cow,
422 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400426 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400431 struct page **pages = NULL;
432 unsigned long nr_pages;
433 unsigned long nr_pages_ret = 0;
434 unsigned long total_compressed = 0;
435 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900436 unsigned long max_compressed = SZ_128K;
437 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400438 int i;
439 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400440 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400441 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400442
Anand Jain26d30f82016-12-19 19:09:06 +0800443 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400444
Chris Mason42dc7ba2008-12-15 11:44:56 -0500445 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400446again:
447 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
449 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400450
Chris Masonf03d9301f2009-02-04 09:31:06 -0500451 /*
452 * we don't want to send crud past the end of i_size through
453 * compression, that's just a waste of CPU time. So, if the
454 * end of the file is before the start of our current
455 * requested range of bytes, we bail out to the uncompressed
456 * cleanup code that can deal with all of this.
457 *
458 * It isn't really the fastest way to fix things, but this is a
459 * very uncommon corner.
460 */
461 if (actual_end <= start)
462 goto cleanup_and_bail_uncompressed;
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_compressed = actual_end - start;
465
Shilong Wang4bcbb332014-10-07 18:44:35 -0400466 /*
467 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400468 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400469 */
470 if (total_compressed <= blocksize &&
471 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 goto cleanup_and_bail_uncompressed;
473
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we want to make sure that amount of ram required to uncompress
475 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500476 * of a compressed extent to 128k. This is a crucial number
477 * because it also controls how easily we can spread reads across
478 * cpus for decompression.
479 *
480 * We also want to make sure the amount of IO required to do
481 * a random read is reasonably small, so we limit the size of
482 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400483 */
484 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000485 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500486 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400487 total_in = 0;
488 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400489
Chris Mason771ed682008-11-06 22:02:51 -0500490 /*
491 * we do compression for mount -o compress and when the
492 * inode has not been flagged as nocompress. This flag can
493 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400494 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800495 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400496 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100497 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800498 if (!pages) {
499 /* just bail out to the uncompressed code */
500 goto cont;
501 }
Chris Mason179e29e2007-11-01 11:28:41 -0400502
Li Zefan261507a02010-12-17 14:21:50 +0800503 if (BTRFS_I(inode)->force_compress)
504 compress_type = BTRFS_I(inode)->force_compress;
505
Chris Mason4adaa612013-03-26 13:07:00 -0400506 /*
507 * we need to call clear_page_dirty_for_io on each
508 * page in the range. Otherwise applications with the file
509 * mmap'd can wander in and change the page contents while
510 * we are compressing them.
511 *
512 * If the compression fails for any reason, we set the pages
513 * dirty again later on.
514 */
515 extent_range_clear_dirty_for_io(inode, start, end);
516 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800517 ret = btrfs_compress_pages(compress_type,
518 inode->i_mapping, start,
519 total_compressed, pages,
520 nr_pages, &nr_pages_ret,
521 &total_in,
522 &total_compressed,
523 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400524
525 if (!ret) {
526 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300527 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 struct page *page = pages[nr_pages_ret - 1];
529 char *kaddr;
530
531 /* zero the tail end of the last page, we might be
532 * sending it down to disk
533 */
534 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800535 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300537 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800538 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400539 }
540 will_compress = 1;
541 }
542 }
Li Zefan560f7d72011-09-08 10:22:01 +0800543cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400544 if (start == 0) {
545 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500546 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400547 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500548 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400549 */
Josef Bacik00361582013-08-14 14:02:47 -0400550 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800551 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500553 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400554 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000555 total_compressed,
556 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400557 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400559 unsigned long clear_flags = EXTENT_DELALLOC |
560 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100561 unsigned long page_error_op;
562
Josef Bacik151a41b2013-07-29 13:22:24 -0400563 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100564 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400565
Chris Mason771ed682008-11-06 22:02:51 -0500566 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 * inline extent creation worked or returned error,
568 * we don't need to create any more async work items.
569 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500570 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800571 extent_clear_unlock_delalloc(inode, start, end, end,
572 NULL, clear_flags,
573 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400574 PAGE_CLEAR_DIRTY |
575 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400577 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800578 btrfs_free_reserved_data_space_noquota(inode, start,
579 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400580 goto free_pages_out;
581 }
582 }
583
584 if (will_compress) {
585 /*
586 * we aren't doing an inline extent round the compressed size
587 * up to a block size boundary so the allocator does sane
588 * things
589 */
Qu Wenruofda28322013-02-26 08:10:22 +0000590 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400591
592 /*
593 * one last check to make sure the compression is really a
594 * win, compare the page count read with the blocks on disk
595 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 if (total_compressed >= total_in) {
598 will_compress = 0;
599 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400600 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700601 *num_added += 1;
602
603 /*
604 * The async work queues will take care of doing actual
605 * allocation on disk for these compressed pages, and
606 * will submit them to the elevator.
607 */
608 add_async_extent(async_cow, start, num_bytes,
609 total_compressed, pages, nr_pages_ret,
610 compress_type);
611
612 if (start + num_bytes < end) {
613 start += num_bytes;
614 pages = NULL;
615 cond_resched();
616 goto again;
617 }
618 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400619 }
620 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400622 /*
623 * the compression code ran but failed to make things smaller,
624 * free any pages it allocated and our page pointer array
625 */
626 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400627 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
630 kfree(pages);
631 pages = NULL;
632 total_compressed = 0;
633 nr_pages_ret = 0;
634
635 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400636 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500637 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500638 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500639 }
Chris Masonc8b97812008-10-29 14:49:59 -0400640 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500641cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 /*
643 * No compression, but we still need to write the pages in the file
644 * we've been given so far. redirty the locked page if it corresponds
645 * to our extent and set things up for the async work queue to run
646 * cow_file_range to do the normal delalloc dance.
647 */
648 if (page_offset(locked_page) >= start &&
649 page_offset(locked_page) <= end)
650 __set_page_dirty_nobuffers(locked_page);
651 /* unlocked later on in the async handlers */
652
653 if (redirty)
654 extent_range_redirty_for_io(inode, start, end);
655 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
656 BTRFS_COMPRESS_NONE);
657 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500658
Filipe Mananac44f6492014-10-09 21:15:44 +0100659 return;
Chris Mason771ed682008-11-06 22:02:51 -0500660
661free_pages_out:
662 for (i = 0; i < nr_pages_ret; i++) {
663 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300664 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500665 }
Chris Masond3977122009-01-05 21:25:51 -0500666 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500667}
Chris Mason771ed682008-11-06 22:02:51 -0500668
Filipe Manana40ae8372014-10-06 22:14:24 +0100669static void free_async_extent_pages(struct async_extent *async_extent)
670{
671 int i;
672
673 if (!async_extent->pages)
674 return;
675
676 for (i = 0; i < async_extent->nr_pages; i++) {
677 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100679 }
680 kfree(async_extent->pages);
681 async_extent->nr_pages = 0;
682 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500683}
684
685/*
686 * phase two of compressed writeback. This is the ordered portion
687 * of the code, which only gets called in the order the work was
688 * queued. We walk all the async extents created by compress_file_range
689 * and send them down to the disk.
690 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100691static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500692 struct async_cow *async_cow)
693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500695 struct async_extent *async_extent;
696 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500697 struct btrfs_key ins;
698 struct extent_map *em;
699 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500700 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500702
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703again:
Chris Masond3977122009-01-05 21:25:51 -0500704 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500705 async_extent = list_entry(async_cow->extents.next,
706 struct async_extent, list);
707 list_del(&async_extent->list);
708
709 io_tree = &BTRFS_I(inode)->io_tree;
710
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711retry:
Chris Mason771ed682008-11-06 22:02:51 -0500712 /* did the compression code fall back to uncompressed IO? */
713 if (!async_extent->pages) {
714 int page_started = 0;
715 unsigned long nr_written = 0;
716
717 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000718 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
721 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722 ret = cow_file_range(inode, async_cow->locked_page,
723 async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800726 async_extent->start +
727 async_extent->ram_size - 1,
728 &page_started, &nr_written, 0,
729 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500730
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100731 /* JDM XXX */
732
Chris Mason771ed682008-11-06 22:02:51 -0500733 /*
734 * if page_started, cow_file_range inserted an
735 * inline extent and took care of all the unlocking
736 * and IO for us. Otherwise, we need to submit
737 * all those pages down to the drive.
738 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500740 extent_write_locked_range(io_tree,
741 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500743 async_extent->ram_size - 1,
744 btrfs_get_extent,
745 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 else if (ret)
747 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500748 kfree(async_extent);
749 cond_resched();
750 continue;
751 }
752
753 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100754 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500755
Wang Xiaoguang18513092016-07-25 15:51:40 +0800756 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500757 async_extent->compressed_size,
758 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800759 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500760 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100761 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400763 if (ret == -ENOSPC) {
764 unlock_extent(io_tree, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800767
768 /*
769 * we need to redirty the pages if we decide to
770 * fallback to uncompressed IO, otherwise we
771 * will not submit these pages down to lower
772 * layers.
773 */
774 extent_range_redirty_for_io(inode,
775 async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1);
778
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400780 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000783 /*
784 * here we're doing allocation and writeback of the
785 * compressed pages
786 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800787 em = create_io_em(inode, async_extent->start,
788 async_extent->ram_size, /* len */
789 async_extent->start, /* orig_start */
790 ins.objectid, /* block_start */
791 ins.offset, /* block_len */
792 ins.offset, /* orig_block_len */
793 async_extent->ram_size, /* ram_bytes */
794 async_extent->compress_type,
795 BTRFS_ORDERED_COMPRESSED);
796 if (IS_ERR(em))
797 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800799 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800
Li Zefan261507a02010-12-17 14:21:50 +0800801 ret = btrfs_add_ordered_extent_compress(inode,
802 async_extent->start,
803 ins.objectid,
804 async_extent->ram_size,
805 ins.offset,
806 BTRFS_ORDERED_COMPRESSED,
807 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100808 if (ret) {
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100813 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400814 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500815
Chris Mason771ed682008-11-06 22:02:51 -0500816 /*
817 * clear dirty, set writeback and unlock the pages.
818 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400819 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400820 async_extent->start +
821 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800822 async_extent->start +
823 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400824 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
825 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400826 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500827 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500828 async_extent->start,
829 async_extent->ram_size,
830 ins.objectid,
831 ins.offset, async_extent->pages,
832 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100833 if (ret) {
834 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
835 struct page *p = async_extent->pages[0];
836 const u64 start = async_extent->start;
837 const u64 end = start + async_extent->ram_size - 1;
838
839 p->mapping = inode->i_mapping;
840 tree->ops->writepage_end_io_hook(p, start, end,
841 NULL, 0);
842 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800843 extent_clear_unlock_delalloc(inode, start, end, end,
844 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 PAGE_END_WRITEBACK |
846 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100847 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100848 }
Chris Mason771ed682008-11-06 22:02:51 -0500849 alloc_hint = ins.objectid + ins.offset;
850 kfree(async_extent);
851 cond_resched();
852 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100853 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400855 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400856 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100857out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400858 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500859 async_extent->start +
860 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800861 async_extent->start +
862 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400863 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400864 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
865 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100866 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100868 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100869 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500871}
872
Josef Bacik4b46fce2010-05-23 11:00:55 -0400873static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
874 u64 num_bytes)
875{
876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 struct extent_map *em;
878 u64 alloc_hint = 0;
879
880 read_lock(&em_tree->lock);
881 em = search_extent_mapping(em_tree, start, num_bytes);
882 if (em) {
883 /*
884 * if block start isn't an actual block number then find the
885 * first block in this inode and use that as a hint. If that
886 * block is also bogus then just don't worry about it.
887 */
888 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
889 free_extent_map(em);
890 em = search_extent_mapping(em_tree, 0, 0);
891 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
892 alloc_hint = em->block_start;
893 if (em)
894 free_extent_map(em);
895 } else {
896 alloc_hint = em->block_start;
897 free_extent_map(em);
898 }
899 }
900 read_unlock(&em_tree->lock);
901
902 return alloc_hint;
903}
904
Chris Mason771ed682008-11-06 22:02:51 -0500905/*
906 * when extent_io.c finds a delayed allocation range in the file,
907 * the call backs end up in this code. The basic idea is to
908 * allocate extents on disk for the range, and create ordered data structs
909 * in ram to track those extents.
910 *
911 * locked_page is the page that writepage had locked already. We use
912 * it to make sure we don't do extra locks or unlocks.
913 *
914 * *page_started is set to one if we unlock locked_page and do everything
915 * required to start IO on it. It may be clean and already done with
916 * IO when we return.
917 */
Josef Bacik00361582013-08-14 14:02:47 -0400918static noinline int cow_file_range(struct inode *inode,
919 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800920 u64 start, u64 end, u64 delalloc_end,
921 int *page_started, unsigned long *nr_written,
922 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500923{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400925 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500926 u64 alloc_hint = 0;
927 u64 num_bytes;
928 unsigned long ram_size;
929 u64 disk_num_bytes;
930 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400931 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500932 struct btrfs_key ins;
933 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500934 int ret = 0;
935
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400936 if (btrfs_is_free_space_inode(inode)) {
937 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500938 ret = -EINVAL;
939 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400940 }
Chris Mason771ed682008-11-06 22:02:51 -0500941
Qu Wenruofda28322013-02-26 08:10:22 +0000942 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500943 num_bytes = max(blocksize, num_bytes);
944 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500945
Anand Jain26d30f82016-12-19 19:09:06 +0800946 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400947
Chris Mason771ed682008-11-06 22:02:51 -0500948 if (start == 0) {
949 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800950 ret = cow_file_range_inline(root, inode, start, end, 0,
951 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500952 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800953 extent_clear_unlock_delalloc(inode, start, end,
954 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400955 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400956 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
958 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800959 btrfs_free_reserved_data_space_noquota(inode, start,
960 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500961 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300962 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500963 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500964 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500967 }
968 }
Chris Masonc8b97812008-10-29 14:49:59 -0400969
970 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400971 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400972
Josef Bacik4b46fce2010-05-23 11:00:55 -0400973 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400974 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400975
Chris Masond3977122009-01-05 21:25:51 -0500976 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400977 unsigned long op;
978
Josef Bacik287a0ab2010-03-19 18:07:23 +0000979 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800982 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400983 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500985
Chris Mason771ed682008-11-06 22:02:51 -0500986 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800987 em = create_io_em(inode, start, ins.offset, /* len */
988 start, /* orig_start */
989 ins.objectid, /* block_start */
990 ins.offset, /* block_len */
991 ins.offset, /* orig_block_len */
992 ram_size, /* ram_bytes */
993 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800994 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800995 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000996 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800997 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400998
Chris Mason98d20f62008-04-14 09:46:10 -0400999 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001000 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001001 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001002 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001003 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001004
Yan Zheng17d217f2008-12-12 10:03:38 -05001005 if (root->root_key.objectid ==
1006 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1007 ret = btrfs_reloc_clone_csums(inode, start,
1008 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001009 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001010 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001011 }
1012
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001014
Chris Masond3977122009-01-05 21:25:51 -05001015 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001016 break;
Chris Masond3977122009-01-05 21:25:51 -05001017
Chris Masonc8b97812008-10-29 14:49:59 -04001018 /* we're not doing compressed IO, don't unlock the first
1019 * page (which the caller expects to stay locked), don't
1020 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001021 *
1022 * Do set the Private2 bit so we know this page was properly
1023 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001024 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001025 op = unlock ? PAGE_UNLOCK : 0;
1026 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001027
Josef Bacikc2790a22013-07-29 11:20:47 -04001028 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001029 start + ram_size - 1,
1030 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 EXTENT_LOCKED | EXTENT_DELALLOC,
1032 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001033 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001034 num_bytes -= cur_alloc_size;
1035 alloc_hint = ins.objectid + ins.offset;
1036 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001037 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001039 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001040
Filipe Mananad9f85962014-08-25 10:43:00 +01001041out_drop_extent_cache:
1042 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001043out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001044 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001045 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001047 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1048 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001049 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1050 EXTENT_DELALLOC | EXTENT_DEFRAG,
1051 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1052 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001053 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001054}
Chris Masonc8b97812008-10-29 14:49:59 -04001055
Chris Mason771ed682008-11-06 22:02:51 -05001056/*
1057 * work queue call back to started compression on a file and pages
1058 */
1059static noinline void async_cow_start(struct btrfs_work *work)
1060{
1061 struct async_cow *async_cow;
1062 int num_added = 0;
1063 async_cow = container_of(work, struct async_cow, work);
1064
1065 compress_file_range(async_cow->inode, async_cow->locked_page,
1066 async_cow->start, async_cow->end, async_cow,
1067 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001068 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001069 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001070 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 }
Chris Mason771ed682008-11-06 22:02:51 -05001072}
1073
1074/*
1075 * work queue call back to submit previously compressed pages
1076 */
1077static noinline void async_cow_submit(struct btrfs_work *work)
1078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001079 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001080 struct async_cow *async_cow;
1081 struct btrfs_root *root;
1082 unsigned long nr_pages;
1083
1084 async_cow = container_of(work, struct async_cow, work);
1085
1086 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1089 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001090
David Sterbaee863952015-02-16 19:41:40 +01001091 /*
1092 * atomic_sub_return implies a barrier for waitqueue_active
1093 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001094 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001095 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001096 waitqueue_active(&fs_info->async_submit_wait))
1097 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001098
Chris Masond3977122009-01-05 21:25:51 -05001099 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001100 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001101}
Chris Masonc8b97812008-10-29 14:49:59 -04001102
Chris Mason771ed682008-11-06 22:02:51 -05001103static noinline void async_cow_free(struct btrfs_work *work)
1104{
1105 struct async_cow *async_cow;
1106 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001107 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001108 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001109 kfree(async_cow);
1110}
1111
1112static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1113 u64 start, u64 end, int *page_started,
1114 unsigned long *nr_written)
1115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001117 struct async_cow *async_cow;
1118 struct btrfs_root *root = BTRFS_I(inode)->root;
1119 unsigned long nr_pages;
1120 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001121
Chris Masona3429ab2009-10-08 12:30:20 -04001122 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1123 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001124 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001125 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001126 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001127 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001128 async_cow->root = root;
1129 async_cow->locked_page = locked_page;
1130 async_cow->start = start;
1131
Wang Shilongf79707b2014-07-17 11:44:09 +08001132 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001133 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001134 cur_end = end;
1135 else
Byongho Leeee221842015-12-15 01:42:10 +09001136 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001137
1138 async_cow->end = cur_end;
1139 INIT_LIST_HEAD(&async_cow->extents);
1140
Liu Bo9e0af232014-08-15 23:36:53 +08001141 btrfs_init_work(&async_cow->work,
1142 btrfs_delalloc_helper,
1143 async_cow_start, async_cow_submit,
1144 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001145
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001146 nr_pages = (cur_end - start + PAGE_SIZE) >>
1147 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001148 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001149
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001151
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 while (atomic_read(&fs_info->async_submit_draining) &&
1153 atomic_read(&fs_info->async_delalloc_pages)) {
1154 wait_event(fs_info->async_submit_wait,
1155 (atomic_read(&fs_info->async_delalloc_pages) ==
1156 0));
Chris Mason771ed682008-11-06 22:02:51 -05001157 }
1158
1159 *nr_written += nr_pages;
1160 start = cur_end + 1;
1161 }
1162 *page_started = 1;
1163 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001164}
1165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001166static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001167 u64 bytenr, u64 num_bytes)
1168{
1169 int ret;
1170 struct btrfs_ordered_sum *sums;
1171 LIST_HEAD(list);
1172
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001174 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001175 if (ret == 0 && list_empty(&list))
1176 return 0;
1177
1178 while (!list_empty(&list)) {
1179 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1180 list_del(&sums->list);
1181 kfree(sums);
1182 }
1183 return 1;
1184}
1185
Chris Masond352ac62008-09-29 15:18:18 -04001186/*
1187 * when nowcow writeback call back. This checks for snapshots or COW copies
1188 * of the extents that exist in the file, and COWs the file as required.
1189 *
1190 * If no cow copies or snapshots exist, we write directly to the existing
1191 * blocks on disk
1192 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001193static noinline int run_delalloc_nocow(struct inode *inode,
1194 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001195 u64 start, u64 end, int *page_started, int force,
1196 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001204 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 u64 cow_start;
1206 u64 cur_offset;
1207 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 disk_bytenr;
1210 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001211 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001212 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001215 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int nocow;
1217 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001218 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001219 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001220
1221 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001222 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001223 extent_clear_unlock_delalloc(inode, start, end, end,
1224 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001225 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 PAGE_CLEAR_DIRTY |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001231 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001232 }
Li Zefan82d59022011-04-20 10:33:24 +08001233
Liu Bo83eea1f2012-07-10 05:28:39 -06001234 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001235
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 cow_start = (u64)-1;
1237 cur_offset = start;
1238 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001239 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001241 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1244 leaf = path->nodes[0];
1245 btrfs_item_key_to_cpu(leaf, &found_key,
1246 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001247 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 found_key.type == BTRFS_EXTENT_DATA_KEY)
1249 path->slots[0]--;
1250 }
1251 check_prev = 0;
1252next_slot:
1253 leaf = path->nodes[0];
1254 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1255 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001256 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001257 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 if (ret > 0)
1259 break;
1260 leaf = path->nodes[0];
1261 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001262
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 nocow = 0;
1264 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001265 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Filipe Manana1d512cb2015-11-09 00:33:58 +00001268 if (found_key.objectid > ino)
1269 break;
1270 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1271 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1272 path->slots[0]++;
1273 goto next_slot;
1274 }
1275 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 found_key.offset > end)
1277 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (found_key.offset > cur_offset) {
1280 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001281 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 goto out_check;
1283 }
Chris Masonc31f8832008-01-08 15:46:31 -05001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 fi = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_file_extent_item);
1287 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1291 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001293 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 extent_end = found_key.offset +
1295 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001296 disk_num_bytes =
1297 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 if (extent_end <= start) {
1299 path->slots[0]++;
1300 goto next_slot;
1301 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001302 if (disk_bytenr == 0)
1303 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 if (btrfs_file_extent_compression(leaf, fi) ||
1305 btrfs_file_extent_encryption(leaf, fi) ||
1306 btrfs_file_extent_other_encoding(leaf, fi))
1307 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001308 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1309 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001310 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001312 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 found_key.offset -
1314 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001315 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 disk_bytenr += cur_offset - found_key.offset;
1318 num_bytes = min(end + 1, extent_end) - cur_offset;
1319 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001320 * if there are pending snapshots for this root,
1321 * we fall into common COW way.
1322 */
1323 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001324 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001325 if (!err)
1326 goto out_check;
1327 }
1328 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 * force cow if csum exists in the range.
1330 * this ensure that csum for a given extent are
1331 * either valid or do not exist.
1332 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001333 if (csum_exist_in_range(fs_info, disk_bytenr,
1334 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001336 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001337 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 nocow = 1;
1339 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1340 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001341 btrfs_file_extent_inline_len(leaf,
1342 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001343 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001344 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 } else {
1346 BUG_ON(1);
1347 }
1348out_check:
1349 if (extent_end <= start) {
1350 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001351 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001352 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001353 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001354 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 goto next_slot;
1356 }
1357 if (!nocow) {
1358 if (cow_start == (u64)-1)
1359 cow_start = cur_offset;
1360 cur_offset = extent_end;
1361 if (cur_offset > end)
1362 break;
1363 path->slots[0]++;
1364 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001365 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
David Sterbab3b4aa72011-04-21 01:20:15 +02001367 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001369 ret = cow_file_range(inode, locked_page,
1370 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001371 end, page_started, nr_written, 1,
1372 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 if (ret) {
1374 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001375 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001376 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001377 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001378 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001379 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001380 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001382 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001383
Yan Zhengd899e052008-10-30 14:25:28 -04001384 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001385 u64 orig_start = found_key.offset - extent_offset;
1386
1387 em = create_io_em(inode, cur_offset, num_bytes,
1388 orig_start,
1389 disk_bytenr, /* block_start */
1390 num_bytes, /* block_len */
1391 disk_num_bytes, /* orig_block_len */
1392 ram_bytes, BTRFS_COMPRESS_NONE,
1393 BTRFS_ORDERED_PREALLOC);
1394 if (IS_ERR(em)) {
1395 if (!nolock && nocow)
1396 btrfs_end_write_no_snapshoting(root);
1397 if (nocow)
1398 btrfs_dec_nocow_writers(fs_info,
1399 disk_bytenr);
1400 ret = PTR_ERR(em);
1401 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001402 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001403 free_extent_map(em);
1404 }
1405
1406 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001407 type = BTRFS_ORDERED_PREALLOC;
1408 } else {
1409 type = BTRFS_ORDERED_NOCOW;
1410 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
1412 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001413 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001414 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001415 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001416 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001417
Yan, Zhengefa56462010-05-16 10:49:59 -04001418 if (root->root_key.objectid ==
1419 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1421 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (ret) {
1423 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001424 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001427 }
1428
Josef Bacikc2790a22013-07-29 11:20:47 -04001429 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001430 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001431 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001432 EXTENT_DELALLOC |
1433 EXTENT_CLEAR_DATA_RESV,
1434 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1435
Wang Shilonge9894fd2014-03-27 11:12:25 +08001436 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001437 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 cur_offset = extent_end;
1439 if (cur_offset > end)
1440 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001441 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001442 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443
Josef Bacik17ca04a2012-05-31 15:58:55 -04001444 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001446 cur_offset = end;
1447 }
1448
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001450 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1451 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001452 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 }
1455
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001457 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001458 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001459 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001460 EXTENT_DELALLOC | EXTENT_DEFRAG |
1461 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1462 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001463 PAGE_SET_WRITEBACK |
1464 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001467}
1468
Wang Shilong47059d92014-07-03 18:22:07 +08001469static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1470{
1471
1472 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1473 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1474 return 0;
1475
1476 /*
1477 * @defrag_bytes is a hint value, no spinlock held here,
1478 * if is not zero, it means the file is defragging.
1479 * Force cow if given extent needs to be defragged.
1480 */
1481 if (BTRFS_I(inode)->defrag_bytes &&
1482 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1483 EXTENT_DEFRAG, 0, NULL))
1484 return 1;
1485
1486 return 0;
1487}
1488
Chris Masond352ac62008-09-29 15:18:18 -04001489/*
1490 * extent_io.c call back to do delayed allocation processing
1491 */
Chris Masonc8b97812008-10-29 14:49:59 -04001492static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001493 u64 start, u64 end, int *page_started,
1494 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001495{
Chris Masonbe20aa92007-12-17 20:14:01 -05001496 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001497 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001498
Wang Shilong47059d92014-07-03 18:22:07 +08001499 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001500 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001501 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001502 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001503 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001504 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001505 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001506 ret = cow_file_range(inode, locked_page, start, end, end,
1507 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001508 } else {
1509 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1510 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001511 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001512 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001513 }
Chris Masonb888db22007-08-27 16:49:44 -04001514 return ret;
1515}
1516
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001517static void btrfs_split_extent_hook(struct inode *inode,
1518 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001520 u64 size;
1521
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525
Josef Bacikdcab6a32015-02-11 15:08:59 -05001526 size = orig->end - orig->start + 1;
1527 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001528 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001529 u64 new_size;
1530
1531 /*
Josef Bacikba117212015-03-13 15:01:24 -04001532 * See the explanation in btrfs_merge_extent_hook, the same
1533 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001534 */
1535 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001536 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001537 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001538 num_extents += count_max_extents(new_size);
1539 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001540 return;
1541 }
1542
Josef Bacik9e0baf62011-07-15 15:16:44 +00001543 spin_lock(&BTRFS_I(inode)->lock);
1544 BTRFS_I(inode)->outstanding_extents++;
1545 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546}
1547
1548/*
1549 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1550 * extents so we can keep track of new extents that are just merged onto old
1551 * extents, such as when we are doing sequential writes, so we can properly
1552 * account for the metadata space we'll need.
1553 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554static void btrfs_merge_extent_hook(struct inode *inode,
1555 struct extent_state *new,
1556 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001557{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001559 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001560
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561 /* not delalloc, ignore it */
1562 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001563 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001564
Josef Bacik8461a3d2015-03-13 15:12:08 -04001565 if (new->start > other->start)
1566 new_size = new->end - other->start + 1;
1567 else
1568 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001569
1570 /* we're not bigger than the max, unreserve the space and go */
1571 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1572 spin_lock(&BTRFS_I(inode)->lock);
1573 BTRFS_I(inode)->outstanding_extents--;
1574 spin_unlock(&BTRFS_I(inode)->lock);
1575 return;
1576 }
1577
1578 /*
Josef Bacikba117212015-03-13 15:01:24 -04001579 * We have to add up either side to figure out how many extents were
1580 * accounted for before we merged into one big extent. If the number of
1581 * extents we accounted for is <= the amount we need for the new range
1582 * then we can return, otherwise drop. Think of it like this
1583 *
1584 * [ 4k][MAX_SIZE]
1585 *
1586 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1587 * need 2 outstanding extents, on one side we have 1 and the other side
1588 * we have 1 so they are == and we can return. But in this case
1589 *
1590 * [MAX_SIZE+4k][MAX_SIZE+4k]
1591 *
1592 * Each range on their own accounts for 2 extents, but merged together
1593 * they are only 3 extents worth of accounting, so we need to drop in
1594 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001595 */
Josef Bacikba117212015-03-13 15:01:24 -04001596 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001597 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001598 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001599 num_extents += count_max_extents(old_size);
1600 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001601 return;
1602
Josef Bacik9e0baf62011-07-15 15:16:44 +00001603 spin_lock(&BTRFS_I(inode)->lock);
1604 BTRFS_I(inode)->outstanding_extents--;
1605 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001606}
1607
Miao Xieeb73c1b2013-05-15 07:48:22 +00001608static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1609 struct inode *inode)
1610{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001611 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1612
Miao Xieeb73c1b2013-05-15 07:48:22 +00001613 spin_lock(&root->delalloc_lock);
1614 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1615 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1616 &root->delalloc_inodes);
1617 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1618 &BTRFS_I(inode)->runtime_flags);
1619 root->nr_delalloc_inodes++;
1620 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001621 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001622 BUG_ON(!list_empty(&root->delalloc_root));
1623 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 &fs_info->delalloc_roots);
1625 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001626 }
1627 }
1628 spin_unlock(&root->delalloc_lock);
1629}
1630
1631static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1632 struct inode *inode)
1633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001634 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1635
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636 spin_lock(&root->delalloc_lock);
1637 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1638 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1639 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1640 &BTRFS_I(inode)->runtime_flags);
1641 root->nr_delalloc_inodes--;
1642 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001643 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001644 BUG_ON(list_empty(&root->delalloc_root));
1645 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001647 }
1648 }
1649 spin_unlock(&root->delalloc_lock);
1650}
1651
Chris Masond352ac62008-09-29 15:18:18 -04001652/*
1653 * extent_io.c set_bit_hook, used to track delayed allocation
1654 * bytes in this file, and to maintain the list of inodes that
1655 * have pending delalloc work to be done.
1656 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001657static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001658 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001659{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001660
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1662
Wang Shilong47059d92014-07-03 18:22:07 +08001663 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1664 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001665 /*
1666 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001667 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001668 * bit, which is only set or cleared with irqs on
1669 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001670 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001671 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001672 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001673 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674
Josef Bacik9e0baf62011-07-15 15:16:44 +00001675 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001676 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001677 } else {
1678 spin_lock(&BTRFS_I(inode)->lock);
1679 BTRFS_I(inode)->outstanding_extents++;
1680 spin_unlock(&BTRFS_I(inode)->lock);
1681 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001682
Josef Bacik6a3891c2015-03-16 17:38:52 -04001683 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001684 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001685 return;
1686
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1688 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001689 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001691 if (*bits & EXTENT_DEFRAG)
1692 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001693 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001694 &BTRFS_I(inode)->runtime_flags))
1695 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001696 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001697 }
Chris Mason291d6732008-01-29 15:55:23 -05001698}
1699
Chris Masond352ac62008-09-29 15:18:18 -04001700/*
1701 * extent_io.c clear_bit_hook, see set_bit_hook for why
1702 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001703static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001704 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001705 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001708 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001709 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001710
1711 spin_lock(&BTRFS_I(inode)->lock);
1712 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1713 BTRFS_I(inode)->defrag_bytes -= len;
1714 spin_unlock(&BTRFS_I(inode)->lock);
1715
Chris Mason75eff682008-12-15 15:54:40 -05001716 /*
1717 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001718 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001719 * bit, which is only set or cleared with irqs on
1720 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001721 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001722 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001723 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001724
Josef Bacik9e0baf62011-07-15 15:16:44 +00001725 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001727 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1728 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001729 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001730 spin_unlock(&BTRFS_I(inode)->lock);
1731 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001732
Josef Bacikb6d08f02013-09-27 14:57:43 -04001733 /*
1734 * We don't reserve metadata space for space cache inodes so we
1735 * don't need to call dellalloc_release_metadata if there is an
1736 * error.
1737 */
1738 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001740 btrfs_delalloc_release_metadata(inode, len);
1741
Josef Bacik6a3891c2015-03-16 17:38:52 -04001742 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001744 return;
1745
Josef Bacik0cb59c92010-07-02 12:14:14 -04001746 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001747 && do_list && !(state->state & EXTENT_NORESERVE)
1748 && (*bits & (EXTENT_DO_ACCOUNTING |
1749 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001750 btrfs_free_reserved_data_space_noquota(inode,
1751 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1754 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001755 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001757 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001758 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 &BTRFS_I(inode)->runtime_flags))
1760 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001762 }
Chris Mason291d6732008-01-29 15:55:23 -05001763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1767 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001768 *
1769 * return 1 if page cannot be merged to bio
1770 * return 0 if page can be merged to bio
1771 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001772 */
Mike Christie81a75f672016-06-05 14:31:54 -05001773int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001774 size_t size, struct bio *bio,
1775 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 struct inode *inode = page->mapping->host;
1778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001780 u64 length = 0;
1781 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001782 int ret;
1783
Chris Mason771ed682008-11-06 22:02:51 -05001784 if (bio_flags & EXTENT_BIO_COMPRESSED)
1785 return 0;
1786
Kent Overstreet4f024f32013-10-11 15:44:27 -07001787 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001788 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1790 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001791 if (ret < 0)
1792 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001793 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001794 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001795 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001796}
1797
Chris Masond352ac62008-09-29 15:18:18 -04001798/*
1799 * in order to insert checksums into the metadata in large chunks,
1800 * we wait until bio submission time. All the pages in the bio are
1801 * checksummed and sums are attached onto the ordered extent record.
1802 *
1803 * At IO completion time the cums attached on the ordered extent record
1804 * are inserted into the btree
1805 */
Mike Christie81a75f672016-06-05 14:31:54 -05001806static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1807 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001808 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001809{
Chris Mason065631f2008-02-20 12:07:25 -05001810 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001812 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001814 return 0;
1815}
Chris Masone0156402008-04-16 11:15:20 -04001816
Chris Mason4a69a412008-11-06 22:03:00 -05001817/*
1818 * in order to insert checksums into the metadata in large chunks,
1819 * we wait until bio submission time. All the pages in the bio are
1820 * checksummed and sums are attached onto the ordered extent record.
1821 *
1822 * At IO completion time the cums attached on the ordered extent record
1823 * are inserted into the btree
1824 */
Mike Christie81a75f672016-06-05 14:31:54 -05001825static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001826 int mirror_num, unsigned long bio_flags,
1827 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001828{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001830 int ret;
1831
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001832 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001833 if (ret) {
1834 bio->bi_error = ret;
1835 bio_endio(bio);
1836 }
Stefan Behrens61891922012-11-05 18:51:52 +01001837 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001838}
1839
Chris Masond352ac62008-09-29 15:18:18 -04001840/*
Chris Masoncad321a2008-12-17 14:51:42 -05001841 * extent_io.c submission hook. This does the right thing for csum calculation
1842 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001843 */
Mike Christie81a75f672016-06-05 14:31:54 -05001844static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001845 int mirror_num, unsigned long bio_flags,
1846 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001847{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001849 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301850 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001851 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001852 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001853 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001854
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001855 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001856
Liu Bo83eea1f2012-07-10 05:28:39 -06001857 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301858 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001859
Mike Christie37226b22016-06-05 14:31:52 -05001860 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001861 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001862 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001863 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001864
Chris Masond20f7042008-12-08 16:58:54 -05001865 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001866 ret = btrfs_submit_compressed_read(inode, bio,
1867 mirror_num,
1868 bio_flags);
1869 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001870 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001871 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001872 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001873 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001874 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001875 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001876 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001877 /* csum items have already been cloned */
1878 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1879 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001880 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1882 bio_flags, bio_offset,
1883 __btrfs_submit_bio_start,
1884 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001885 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001886 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001887 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001888 if (ret)
1889 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001890 }
1891
Chris Mason0b86a832008-03-24 15:01:56 -04001892mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001893 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001894
1895out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001896 if (ret < 0) {
1897 bio->bi_error = ret;
1898 bio_endio(bio);
1899 }
Stefan Behrens61891922012-11-05 18:51:52 +01001900 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001901}
Chris Mason6885f302008-02-20 16:11:05 -05001902
Chris Masond352ac62008-09-29 15:18:18 -04001903/*
1904 * given a list of ordered sums record them in the inode. This happens
1905 * at IO completion time based on sums calculated at bio submission time.
1906 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001907static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001908 struct inode *inode, u64 file_offset,
1909 struct list_head *list)
1910{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001911 struct btrfs_ordered_sum *sum;
1912
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001913 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001914 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001915 btrfs_csum_file_blocks(trans,
1916 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001917 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001918 }
1919 return 0;
1920}
1921
Josef Bacik2ac55d42010-02-03 19:33:23 +00001922int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001923 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001924{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001925 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001926 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001927 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001928}
1929
Chris Masond352ac62008-09-29 15:18:18 -04001930/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001931struct btrfs_writepage_fixup {
1932 struct page *page;
1933 struct btrfs_work work;
1934};
1935
Christoph Hellwigb2950862008-12-02 09:54:17 -05001936static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001937{
1938 struct btrfs_writepage_fixup *fixup;
1939 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001940 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001941 struct page *page;
1942 struct inode *inode;
1943 u64 page_start;
1944 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001945 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001946
1947 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1948 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001949again:
Chris Mason247e7432008-07-17 12:53:51 -04001950 lock_page(page);
1951 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1952 ClearPageChecked(page);
1953 goto out_page;
1954 }
1955
1956 inode = page->mapping->host;
1957 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001958 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001959
David Sterbaff13db42015-12-03 14:30:40 +01001960 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001961 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001962
1963 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001964 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001965 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001966
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05301967 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001968 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001969 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001970 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1971 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001972 unlock_page(page);
1973 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001974 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001975 goto again;
1976 }
Chris Mason247e7432008-07-17 12:53:51 -04001977
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001978 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001979 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001980 if (ret) {
1981 mapping_set_error(page->mapping, ret);
1982 end_extent_writepage(page, ret, page_start, page_end);
1983 ClearPageChecked(page);
1984 goto out;
1985 }
1986
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001987 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1988 0);
Chris Mason247e7432008-07-17 12:53:51 -04001989 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001990 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001991out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001992 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1993 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001994out_page:
1995 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001996 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001997 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001998}
1999
2000/*
2001 * There are a few paths in the higher layers of the kernel that directly
2002 * set the page dirty bit without asking the filesystem if it is a
2003 * good idea. This causes problems because we want to make sure COW
2004 * properly happens and the data=ordered rules are followed.
2005 *
Chris Masonc8b97812008-10-29 14:49:59 -04002006 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002007 * hasn't been properly setup for IO. We kick off an async process
2008 * to fix it up. The async helper will wait for ordered extents, set
2009 * the delalloc bit and make it safe to write the page.
2010 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002011static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002012{
2013 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002015 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002016
Chris Mason8b62b722009-09-02 16:53:46 -04002017 /* this page is properly in the ordered list */
2018 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002019 return 0;
2020
2021 if (PageChecked(page))
2022 return -EAGAIN;
2023
2024 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2025 if (!fixup)
2026 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002027
Chris Mason247e7432008-07-17 12:53:51 -04002028 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002029 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002030 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2031 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002032 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002033 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002034 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002035}
2036
Yan Zhengd899e052008-10-30 14:25:28 -04002037static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2038 struct inode *inode, u64 file_pos,
2039 u64 disk_bytenr, u64 disk_num_bytes,
2040 u64 num_bytes, u64 ram_bytes,
2041 u8 compression, u8 encryption,
2042 u16 other_encoding, int extent_type)
2043{
2044 struct btrfs_root *root = BTRFS_I(inode)->root;
2045 struct btrfs_file_extent_item *fi;
2046 struct btrfs_path *path;
2047 struct extent_buffer *leaf;
2048 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002049 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002050 int ret;
2051
2052 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002053 if (!path)
2054 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002055
Chris Masona1ed8352009-09-11 12:27:37 -04002056 /*
2057 * we may be replacing one extent in the tree with another.
2058 * The new extent is pinned in the extent map, and we don't want
2059 * to drop it from the cache until it is completely in the btree.
2060 *
2061 * So, tell btrfs_drop_extents to leave this extent in the cache.
2062 * the caller is expected to unpin it and allow it to be merged
2063 * with the others.
2064 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002065 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2066 file_pos + num_bytes, NULL, 0,
2067 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002068 if (ret)
2069 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002070
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002071 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002072 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002073 ins.offset = file_pos;
2074 ins.type = BTRFS_EXTENT_DATA_KEY;
2075
2076 path->leave_spinning = 1;
2077 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2078 sizeof(*fi));
2079 if (ret)
2080 goto out;
2081 }
Yan Zhengd899e052008-10-30 14:25:28 -04002082 leaf = path->nodes[0];
2083 fi = btrfs_item_ptr(leaf, path->slots[0],
2084 struct btrfs_file_extent_item);
2085 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2086 btrfs_set_file_extent_type(leaf, fi, extent_type);
2087 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2088 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2089 btrfs_set_file_extent_offset(leaf, fi, 0);
2090 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2091 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2092 btrfs_set_file_extent_compression(leaf, fi, compression);
2093 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2094 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002095
Yan Zhengd899e052008-10-30 14:25:28 -04002096 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002097 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002098
2099 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002100
2101 ins.objectid = disk_bytenr;
2102 ins.offset = disk_num_bytes;
2103 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002104 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002105 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002106 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002107 * Release the reserved range from inode dirty range map, as it is
2108 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002109 */
2110 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002111out:
Yan Zhengd899e052008-10-30 14:25:28 -04002112 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002113
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002114 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002115}
2116
Liu Bo38c227d2013-01-29 03:18:40 +00002117/* snapshot-aware defrag */
2118struct sa_defrag_extent_backref {
2119 struct rb_node node;
2120 struct old_sa_defrag_extent *old;
2121 u64 root_id;
2122 u64 inum;
2123 u64 file_pos;
2124 u64 extent_offset;
2125 u64 num_bytes;
2126 u64 generation;
2127};
2128
2129struct old_sa_defrag_extent {
2130 struct list_head list;
2131 struct new_sa_defrag_extent *new;
2132
2133 u64 extent_offset;
2134 u64 bytenr;
2135 u64 offset;
2136 u64 len;
2137 int count;
2138};
2139
2140struct new_sa_defrag_extent {
2141 struct rb_root root;
2142 struct list_head head;
2143 struct btrfs_path *path;
2144 struct inode *inode;
2145 u64 file_pos;
2146 u64 len;
2147 u64 bytenr;
2148 u64 disk_len;
2149 u8 compress_type;
2150};
2151
2152static int backref_comp(struct sa_defrag_extent_backref *b1,
2153 struct sa_defrag_extent_backref *b2)
2154{
2155 if (b1->root_id < b2->root_id)
2156 return -1;
2157 else if (b1->root_id > b2->root_id)
2158 return 1;
2159
2160 if (b1->inum < b2->inum)
2161 return -1;
2162 else if (b1->inum > b2->inum)
2163 return 1;
2164
2165 if (b1->file_pos < b2->file_pos)
2166 return -1;
2167 else if (b1->file_pos > b2->file_pos)
2168 return 1;
2169
2170 /*
2171 * [------------------------------] ===> (a range of space)
2172 * |<--->| |<---->| =============> (fs/file tree A)
2173 * |<---------------------------->| ===> (fs/file tree B)
2174 *
2175 * A range of space can refer to two file extents in one tree while
2176 * refer to only one file extent in another tree.
2177 *
2178 * So we may process a disk offset more than one time(two extents in A)
2179 * and locate at the same extent(one extent in B), then insert two same
2180 * backrefs(both refer to the extent in B).
2181 */
2182 return 0;
2183}
2184
2185static void backref_insert(struct rb_root *root,
2186 struct sa_defrag_extent_backref *backref)
2187{
2188 struct rb_node **p = &root->rb_node;
2189 struct rb_node *parent = NULL;
2190 struct sa_defrag_extent_backref *entry;
2191 int ret;
2192
2193 while (*p) {
2194 parent = *p;
2195 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2196
2197 ret = backref_comp(backref, entry);
2198 if (ret < 0)
2199 p = &(*p)->rb_left;
2200 else
2201 p = &(*p)->rb_right;
2202 }
2203
2204 rb_link_node(&backref->node, parent, p);
2205 rb_insert_color(&backref->node, root);
2206}
2207
2208/*
2209 * Note the backref might has changed, and in this case we just return 0.
2210 */
2211static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2212 void *ctx)
2213{
2214 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002215 struct old_sa_defrag_extent *old = ctx;
2216 struct new_sa_defrag_extent *new = old->new;
2217 struct btrfs_path *path = new->path;
2218 struct btrfs_key key;
2219 struct btrfs_root *root;
2220 struct sa_defrag_extent_backref *backref;
2221 struct extent_buffer *leaf;
2222 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002224 int slot;
2225 int ret;
2226 u64 extent_offset;
2227 u64 num_bytes;
2228
2229 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002230 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002231 return 0;
2232
2233 key.objectid = root_id;
2234 key.type = BTRFS_ROOT_ITEM_KEY;
2235 key.offset = (u64)-1;
2236
Liu Bo38c227d2013-01-29 03:18:40 +00002237 root = btrfs_read_fs_root_no_name(fs_info, &key);
2238 if (IS_ERR(root)) {
2239 if (PTR_ERR(root) == -ENOENT)
2240 return 0;
2241 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002242 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002243 inum, offset, root_id);
2244 return PTR_ERR(root);
2245 }
2246
2247 key.objectid = inum;
2248 key.type = BTRFS_EXTENT_DATA_KEY;
2249 if (offset > (u64)-1 << 32)
2250 key.offset = 0;
2251 else
2252 key.offset = offset;
2253
2254 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302255 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002256 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002257 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002258
2259 while (1) {
2260 cond_resched();
2261
2262 leaf = path->nodes[0];
2263 slot = path->slots[0];
2264
2265 if (slot >= btrfs_header_nritems(leaf)) {
2266 ret = btrfs_next_leaf(root, path);
2267 if (ret < 0) {
2268 goto out;
2269 } else if (ret > 0) {
2270 ret = 0;
2271 goto out;
2272 }
2273 continue;
2274 }
2275
2276 path->slots[0]++;
2277
2278 btrfs_item_key_to_cpu(leaf, &key, slot);
2279
2280 if (key.objectid > inum)
2281 goto out;
2282
2283 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2284 continue;
2285
2286 extent = btrfs_item_ptr(leaf, slot,
2287 struct btrfs_file_extent_item);
2288
2289 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2290 continue;
2291
Liu Boe68afa42013-07-01 22:13:26 +08002292 /*
2293 * 'offset' refers to the exact key.offset,
2294 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2295 * (key.offset - extent_offset).
2296 */
2297 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002298 continue;
2299
Liu Boe68afa42013-07-01 22:13:26 +08002300 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002301 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002302
Liu Bo38c227d2013-01-29 03:18:40 +00002303 if (extent_offset >= old->extent_offset + old->offset +
2304 old->len || extent_offset + num_bytes <=
2305 old->extent_offset + old->offset)
2306 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002307 break;
2308 }
2309
2310 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2311 if (!backref) {
2312 ret = -ENOENT;
2313 goto out;
2314 }
2315
2316 backref->root_id = root_id;
2317 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002318 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002319 backref->num_bytes = num_bytes;
2320 backref->extent_offset = extent_offset;
2321 backref->generation = btrfs_file_extent_generation(leaf, extent);
2322 backref->old = old;
2323 backref_insert(&new->root, backref);
2324 old->count++;
2325out:
2326 btrfs_release_path(path);
2327 WARN_ON(ret);
2328 return ret;
2329}
2330
2331static noinline bool record_extent_backrefs(struct btrfs_path *path,
2332 struct new_sa_defrag_extent *new)
2333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002334 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002335 struct old_sa_defrag_extent *old, *tmp;
2336 int ret;
2337
2338 new->path = path;
2339
2340 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002341 ret = iterate_inodes_from_logical(old->bytenr +
2342 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002343 path, record_one_backref,
2344 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002345 if (ret < 0 && ret != -ENOENT)
2346 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002347
2348 /* no backref to be processed for this extent */
2349 if (!old->count) {
2350 list_del(&old->list);
2351 kfree(old);
2352 }
2353 }
2354
2355 if (list_empty(&new->head))
2356 return false;
2357
2358 return true;
2359}
2360
2361static int relink_is_mergable(struct extent_buffer *leaf,
2362 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002363 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002364{
Liu Bo116e0022013-08-02 16:30:40 +08002365 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002366 return 0;
2367
2368 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2369 return 0;
2370
Liu Bo116e0022013-08-02 16:30:40 +08002371 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2372 return 0;
2373
2374 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002375 btrfs_file_extent_other_encoding(leaf, fi))
2376 return 0;
2377
2378 return 1;
2379}
2380
2381/*
2382 * Note the backref might has changed, and in this case we just return 0.
2383 */
2384static noinline int relink_extent_backref(struct btrfs_path *path,
2385 struct sa_defrag_extent_backref *prev,
2386 struct sa_defrag_extent_backref *backref)
2387{
2388 struct btrfs_file_extent_item *extent;
2389 struct btrfs_file_extent_item *item;
2390 struct btrfs_ordered_extent *ordered;
2391 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002392 struct btrfs_root *root;
2393 struct btrfs_key key;
2394 struct extent_buffer *leaf;
2395 struct old_sa_defrag_extent *old = backref->old;
2396 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002397 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002398 struct inode *inode;
2399 struct extent_state *cached = NULL;
2400 int ret = 0;
2401 u64 start;
2402 u64 len;
2403 u64 lock_start;
2404 u64 lock_end;
2405 bool merge = false;
2406 int index;
2407
2408 if (prev && prev->root_id == backref->root_id &&
2409 prev->inum == backref->inum &&
2410 prev->file_pos + prev->num_bytes == backref->file_pos)
2411 merge = true;
2412
2413 /* step 1: get root */
2414 key.objectid = backref->root_id;
2415 key.type = BTRFS_ROOT_ITEM_KEY;
2416 key.offset = (u64)-1;
2417
Liu Bo38c227d2013-01-29 03:18:40 +00002418 index = srcu_read_lock(&fs_info->subvol_srcu);
2419
2420 root = btrfs_read_fs_root_no_name(fs_info, &key);
2421 if (IS_ERR(root)) {
2422 srcu_read_unlock(&fs_info->subvol_srcu, index);
2423 if (PTR_ERR(root) == -ENOENT)
2424 return 0;
2425 return PTR_ERR(root);
2426 }
Liu Bo38c227d2013-01-29 03:18:40 +00002427
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002428 if (btrfs_root_readonly(root)) {
2429 srcu_read_unlock(&fs_info->subvol_srcu, index);
2430 return 0;
2431 }
2432
Liu Bo38c227d2013-01-29 03:18:40 +00002433 /* step 2: get inode */
2434 key.objectid = backref->inum;
2435 key.type = BTRFS_INODE_ITEM_KEY;
2436 key.offset = 0;
2437
2438 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2439 if (IS_ERR(inode)) {
2440 srcu_read_unlock(&fs_info->subvol_srcu, index);
2441 return 0;
2442 }
2443
2444 srcu_read_unlock(&fs_info->subvol_srcu, index);
2445
2446 /* step 3: relink backref */
2447 lock_start = backref->file_pos;
2448 lock_end = backref->file_pos + backref->num_bytes - 1;
2449 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002450 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002451
2452 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2453 if (ordered) {
2454 btrfs_put_ordered_extent(ordered);
2455 goto out_unlock;
2456 }
2457
2458 trans = btrfs_join_transaction(root);
2459 if (IS_ERR(trans)) {
2460 ret = PTR_ERR(trans);
2461 goto out_unlock;
2462 }
2463
2464 key.objectid = backref->inum;
2465 key.type = BTRFS_EXTENT_DATA_KEY;
2466 key.offset = backref->file_pos;
2467
2468 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2469 if (ret < 0) {
2470 goto out_free_path;
2471 } else if (ret > 0) {
2472 ret = 0;
2473 goto out_free_path;
2474 }
2475
2476 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2477 struct btrfs_file_extent_item);
2478
2479 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2480 backref->generation)
2481 goto out_free_path;
2482
2483 btrfs_release_path(path);
2484
2485 start = backref->file_pos;
2486 if (backref->extent_offset < old->extent_offset + old->offset)
2487 start += old->extent_offset + old->offset -
2488 backref->extent_offset;
2489
2490 len = min(backref->extent_offset + backref->num_bytes,
2491 old->extent_offset + old->offset + old->len);
2492 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2493
2494 ret = btrfs_drop_extents(trans, root, inode, start,
2495 start + len, 1);
2496 if (ret)
2497 goto out_free_path;
2498again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002499 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002500 key.type = BTRFS_EXTENT_DATA_KEY;
2501 key.offset = start;
2502
Liu Boa09a0a72013-03-11 09:20:58 +00002503 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002504 if (merge) {
2505 struct btrfs_file_extent_item *fi;
2506 u64 extent_len;
2507 struct btrfs_key found_key;
2508
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002509 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002510 if (ret < 0)
2511 goto out_free_path;
2512
2513 path->slots[0]--;
2514 leaf = path->nodes[0];
2515 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2516
2517 fi = btrfs_item_ptr(leaf, path->slots[0],
2518 struct btrfs_file_extent_item);
2519 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2520
Liu Bo116e0022013-08-02 16:30:40 +08002521 if (extent_len + found_key.offset == start &&
2522 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002523 btrfs_set_file_extent_num_bytes(leaf, fi,
2524 extent_len + len);
2525 btrfs_mark_buffer_dirty(leaf);
2526 inode_add_bytes(inode, len);
2527
2528 ret = 1;
2529 goto out_free_path;
2530 } else {
2531 merge = false;
2532 btrfs_release_path(path);
2533 goto again;
2534 }
2535 }
2536
2537 ret = btrfs_insert_empty_item(trans, root, path, &key,
2538 sizeof(*extent));
2539 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002540 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002541 goto out_free_path;
2542 }
2543
2544 leaf = path->nodes[0];
2545 item = btrfs_item_ptr(leaf, path->slots[0],
2546 struct btrfs_file_extent_item);
2547 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2548 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2549 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2550 btrfs_set_file_extent_num_bytes(leaf, item, len);
2551 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2552 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2553 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2554 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2555 btrfs_set_file_extent_encryption(leaf, item, 0);
2556 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2557
2558 btrfs_mark_buffer_dirty(leaf);
2559 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002560 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002561
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002562 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002563 new->disk_len, 0,
2564 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002565 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002566 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002567 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002568 goto out_free_path;
2569 }
2570
2571 ret = 1;
2572out_free_path:
2573 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002574 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002575 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002576out_unlock:
2577 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2578 &cached, GFP_NOFS);
2579 iput(inode);
2580 return ret;
2581}
2582
Liu Bo6f519562013-10-29 10:45:05 +08002583static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2584{
2585 struct old_sa_defrag_extent *old, *tmp;
2586
2587 if (!new)
2588 return;
2589
2590 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002591 kfree(old);
2592 }
2593 kfree(new);
2594}
2595
Liu Bo38c227d2013-01-29 03:18:40 +00002596static void relink_file_extents(struct new_sa_defrag_extent *new)
2597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002598 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002599 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002600 struct sa_defrag_extent_backref *backref;
2601 struct sa_defrag_extent_backref *prev = NULL;
2602 struct inode *inode;
2603 struct btrfs_root *root;
2604 struct rb_node *node;
2605 int ret;
2606
2607 inode = new->inode;
2608 root = BTRFS_I(inode)->root;
2609
2610 path = btrfs_alloc_path();
2611 if (!path)
2612 return;
2613
2614 if (!record_extent_backrefs(path, new)) {
2615 btrfs_free_path(path);
2616 goto out;
2617 }
2618 btrfs_release_path(path);
2619
2620 while (1) {
2621 node = rb_first(&new->root);
2622 if (!node)
2623 break;
2624 rb_erase(node, &new->root);
2625
2626 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2627
2628 ret = relink_extent_backref(path, prev, backref);
2629 WARN_ON(ret < 0);
2630
2631 kfree(prev);
2632
2633 if (ret == 1)
2634 prev = backref;
2635 else
2636 prev = NULL;
2637 cond_resched();
2638 }
2639 kfree(prev);
2640
2641 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002642out:
Liu Bo6f519562013-10-29 10:45:05 +08002643 free_sa_defrag_extent(new);
2644
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002645 atomic_dec(&fs_info->defrag_running);
2646 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002647}
2648
2649static struct new_sa_defrag_extent *
2650record_old_file_extents(struct inode *inode,
2651 struct btrfs_ordered_extent *ordered)
2652{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002653 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002654 struct btrfs_root *root = BTRFS_I(inode)->root;
2655 struct btrfs_path *path;
2656 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002657 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002658 struct new_sa_defrag_extent *new;
2659 int ret;
2660
2661 new = kmalloc(sizeof(*new), GFP_NOFS);
2662 if (!new)
2663 return NULL;
2664
2665 new->inode = inode;
2666 new->file_pos = ordered->file_offset;
2667 new->len = ordered->len;
2668 new->bytenr = ordered->start;
2669 new->disk_len = ordered->disk_len;
2670 new->compress_type = ordered->compress_type;
2671 new->root = RB_ROOT;
2672 INIT_LIST_HEAD(&new->head);
2673
2674 path = btrfs_alloc_path();
2675 if (!path)
2676 goto out_kfree;
2677
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002678 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002679 key.type = BTRFS_EXTENT_DATA_KEY;
2680 key.offset = new->file_pos;
2681
2682 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2683 if (ret < 0)
2684 goto out_free_path;
2685 if (ret > 0 && path->slots[0] > 0)
2686 path->slots[0]--;
2687
2688 /* find out all the old extents for the file range */
2689 while (1) {
2690 struct btrfs_file_extent_item *extent;
2691 struct extent_buffer *l;
2692 int slot;
2693 u64 num_bytes;
2694 u64 offset;
2695 u64 end;
2696 u64 disk_bytenr;
2697 u64 extent_offset;
2698
2699 l = path->nodes[0];
2700 slot = path->slots[0];
2701
2702 if (slot >= btrfs_header_nritems(l)) {
2703 ret = btrfs_next_leaf(root, path);
2704 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002705 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002706 else if (ret > 0)
2707 break;
2708 continue;
2709 }
2710
2711 btrfs_item_key_to_cpu(l, &key, slot);
2712
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002713 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002714 break;
2715 if (key.type != BTRFS_EXTENT_DATA_KEY)
2716 break;
2717 if (key.offset >= new->file_pos + new->len)
2718 break;
2719
2720 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2721
2722 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2723 if (key.offset + num_bytes < new->file_pos)
2724 goto next;
2725
2726 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2727 if (!disk_bytenr)
2728 goto next;
2729
2730 extent_offset = btrfs_file_extent_offset(l, extent);
2731
2732 old = kmalloc(sizeof(*old), GFP_NOFS);
2733 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002734 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002735
2736 offset = max(new->file_pos, key.offset);
2737 end = min(new->file_pos + new->len, key.offset + num_bytes);
2738
2739 old->bytenr = disk_bytenr;
2740 old->extent_offset = extent_offset;
2741 old->offset = offset - key.offset;
2742 old->len = end - offset;
2743 old->new = new;
2744 old->count = 0;
2745 list_add_tail(&old->list, &new->head);
2746next:
2747 path->slots[0]++;
2748 cond_resched();
2749 }
2750
2751 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002753
2754 return new;
2755
Liu Bo38c227d2013-01-29 03:18:40 +00002756out_free_path:
2757 btrfs_free_path(path);
2758out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002759 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002760 return NULL;
2761}
2762
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002763static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002764 u64 start, u64 len)
2765{
2766 struct btrfs_block_group_cache *cache;
2767
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002768 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002769 ASSERT(cache);
2770
2771 spin_lock(&cache->lock);
2772 cache->delalloc_bytes -= len;
2773 spin_unlock(&cache->lock);
2774
2775 btrfs_put_block_group(cache);
2776}
2777
Chris Masond352ac62008-09-29 15:18:18 -04002778/* as ordered data IO finishes, this gets called so we can finish
2779 * an ordered extent if the range of bytes in the file it covers are
2780 * fully written.
2781 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002782static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002783{
Josef Bacik5fd02042012-05-02 14:00:54 -04002784 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002787 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002789 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002790 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002791 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002792 int ret = 0;
2793 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002794 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002795 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002796
Liu Bo83eea1f2012-07-10 05:28:39 -06002797 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002798
Josef Bacik5fd02042012-05-02 14:00:54 -04002799 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2800 ret = -EIO;
2801 goto out;
2802 }
2803
Miao Xief6124962014-09-12 18:44:04 +08002804 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2805 ordered_extent->file_offset +
2806 ordered_extent->len - 1);
2807
Josef Bacik77cef2e2013-08-29 13:57:21 -04002808 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2809 truncated = true;
2810 logical_len = ordered_extent->truncated_len;
2811 /* Truncated the entire extent, don't bother adding */
2812 if (!logical_len)
2813 goto out;
2814 }
2815
Yan, Zhengc2167752009-11-12 09:34:21 +00002816 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002818
2819 /*
2820 * For mwrite(mmap + memset to write) case, we still reserve
2821 * space for NOCOW range.
2822 * As NOCOW won't cause a new delayed ref, just free the space
2823 */
2824 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2825 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002826 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2827 if (nolock)
2828 trans = btrfs_join_transaction_nolock(root);
2829 else
2830 trans = btrfs_join_transaction(root);
2831 if (IS_ERR(trans)) {
2832 ret = PTR_ERR(trans);
2833 trans = NULL;
2834 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002835 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002836 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002837 ret = btrfs_update_inode_fallback(trans, root, inode);
2838 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002839 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002840 goto out;
2841 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002842
Josef Bacik2ac55d42010-02-03 19:33:23 +00002843 lock_extent_bits(io_tree, ordered_extent->file_offset,
2844 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002845 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002846
Liu Bo38c227d2013-01-29 03:18:40 +00002847 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2848 ordered_extent->file_offset + ordered_extent->len - 1,
2849 EXTENT_DEFRAG, 1, cached_state);
2850 if (ret) {
2851 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002852 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002853 /* the inode is shared */
2854 new = record_old_file_extents(inode, ordered_extent);
2855
2856 clear_extent_bit(io_tree, ordered_extent->file_offset,
2857 ordered_extent->file_offset + ordered_extent->len - 1,
2858 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2859 }
2860
Josef Bacik0cb59c92010-07-02 12:14:14 -04002861 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002862 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002863 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002864 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002865 if (IS_ERR(trans)) {
2866 ret = PTR_ERR(trans);
2867 trans = NULL;
2868 goto out_unlock;
2869 }
Chris Masona79b7d42014-05-22 16:18:52 -07002870
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002871 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002872
Chris Masonc8b97812008-10-29 14:49:59 -04002873 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002874 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002875 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002876 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002877 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002878 ordered_extent->file_offset,
2879 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002880 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002881 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002882 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002883 ret = insert_reserved_file_extent(trans, inode,
2884 ordered_extent->file_offset,
2885 ordered_extent->start,
2886 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002887 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002888 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002889 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002890 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002891 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002892 ordered_extent->start,
2893 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002894 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002895 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2896 ordered_extent->file_offset, ordered_extent->len,
2897 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002898 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002899 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002900 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002901 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002902
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903 add_pending_csums(trans, inode, ordered_extent->file_offset,
2904 &ordered_extent->list);
2905
Josef Bacik6c760c02012-11-09 10:53:21 -05002906 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2907 ret = btrfs_update_inode_fallback(trans, root, inode);
2908 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002909 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002910 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002911 }
2912 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002913out_unlock:
2914 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2915 ordered_extent->file_offset +
2916 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002917out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002919 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002920 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002921 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002922
Josef Bacik77cef2e2013-08-29 13:57:21 -04002923 if (ret || truncated) {
2924 u64 start, end;
2925
2926 if (truncated)
2927 start = ordered_extent->file_offset + logical_len;
2928 else
2929 start = ordered_extent->file_offset;
2930 end = ordered_extent->file_offset + ordered_extent->len - 1;
2931 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2932
2933 /* Drop the cache for the part of the extent we didn't write. */
2934 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002935
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002936 /*
2937 * If the ordered extent had an IOERR or something else went
2938 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 * back to the allocator. We only free the extent in the
2940 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002941 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 if ((ret || !logical_len) &&
2943 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002944 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002945 btrfs_free_reserved_extent(fs_info,
2946 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002947 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002948 }
2949
2950
Josef Bacik5fd02042012-05-02 14:00:54 -04002951 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002952 * This needs to be done to make sure anybody waiting knows we are done
2953 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002954 */
2955 btrfs_remove_ordered_extent(inode, ordered_extent);
2956
Liu Bo38c227d2013-01-29 03:18:40 +00002957 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002958 if (new) {
2959 if (ret) {
2960 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002961 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002962 } else {
2963 relink_file_extents(new);
2964 }
2965 }
Liu Bo38c227d2013-01-29 03:18:40 +00002966
Chris Masone6dcd2d2008-07-17 12:53:50 -04002967 /* once for us */
2968 btrfs_put_ordered_extent(ordered_extent);
2969 /* once for the tree */
2970 btrfs_put_ordered_extent(ordered_extent);
2971
Josef Bacik5fd02042012-05-02 14:00:54 -04002972 return ret;
2973}
2974
2975static void finish_ordered_fn(struct btrfs_work *work)
2976{
2977 struct btrfs_ordered_extent *ordered_extent;
2978 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2979 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002980}
2981
Christoph Hellwigb2950862008-12-02 09:54:17 -05002982static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002983 struct extent_state *state, int uptodate)
2984{
Josef Bacik5fd02042012-05-02 14:00:54 -04002985 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002987 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002988 struct btrfs_workqueue *wq;
2989 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002990
liubo1abe9b82011-03-24 11:18:59 +00002991 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2992
Chris Mason8b62b722009-09-02 16:53:46 -04002993 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002994 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2995 end - start + 1, uptodate))
2996 return 0;
2997
Liu Bo9e0af232014-08-15 23:36:53 +08002998 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002999 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003000 func = btrfs_freespace_write_helper;
3001 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003003 func = btrfs_endio_write_helper;
3004 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003005
Liu Bo9e0af232014-08-15 23:36:53 +08003006 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3007 NULL);
3008 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003009
3010 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003011}
3012
Miao Xiedc380ae2014-09-12 18:43:55 +08003013static int __readpage_endio_check(struct inode *inode,
3014 struct btrfs_io_bio *io_bio,
3015 int icsum, struct page *page,
3016 int pgoff, u64 start, size_t len)
3017{
3018 char *kaddr;
3019 u32 csum_expected;
3020 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003021
3022 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3023
3024 kaddr = kmap_atomic(page);
3025 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003026 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003027 if (csum != csum_expected)
3028 goto zeroit;
3029
3030 kunmap_atomic(kaddr);
3031 return 0;
3032zeroit:
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003033 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3034 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003035 memset(kaddr + pgoff, 1, len);
3036 flush_dcache_page(page);
3037 kunmap_atomic(kaddr);
3038 if (csum_expected == 0)
3039 return 0;
3040 return -EIO;
3041}
3042
Chris Masond352ac62008-09-29 15:18:18 -04003043/*
Chris Masond352ac62008-09-29 15:18:18 -04003044 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003045 * if there's a match, we allow the bio to finish. If not, the code in
3046 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003047 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003048static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3049 u64 phy_offset, struct page *page,
3050 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003051{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003052 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003053 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003054 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003055 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003056
Chris Masond20f7042008-12-08 16:58:54 -05003057 if (PageChecked(page)) {
3058 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003059 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003060 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003061
3062 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003063 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003064
Yan Zheng17d217f2008-12-12 10:03:38 -05003065 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003066 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003067 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003068 return 0;
3069 }
3070
Miao Xiefacc8a222013-07-25 19:22:34 +08003071 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003072 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3073 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003074}
Chris Masonb888db22007-08-27 16:49:44 -04003075
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003076void btrfs_add_delayed_iput(struct inode *inode)
3077{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003079 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003080
3081 if (atomic_add_unless(&inode->i_count, -1, 1))
3082 return;
3083
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003084 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003085 if (binode->delayed_iput_count == 0) {
3086 ASSERT(list_empty(&binode->delayed_iput));
3087 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3088 } else {
3089 binode->delayed_iput_count++;
3090 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003091 spin_unlock(&fs_info->delayed_iput_lock);
3092}
3093
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003094void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003096
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003097 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003098 while (!list_empty(&fs_info->delayed_iputs)) {
3099 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003100
David Sterba8089fe62015-11-19 14:15:51 +01003101 inode = list_first_entry(&fs_info->delayed_iputs,
3102 struct btrfs_inode, delayed_iput);
3103 if (inode->delayed_iput_count) {
3104 inode->delayed_iput_count--;
3105 list_move_tail(&inode->delayed_iput,
3106 &fs_info->delayed_iputs);
3107 } else {
3108 list_del_init(&inode->delayed_iput);
3109 }
3110 spin_unlock(&fs_info->delayed_iput_lock);
3111 iput(&inode->vfs_inode);
3112 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003113 }
David Sterba8089fe62015-11-19 14:15:51 +01003114 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003115}
3116
Yan, Zhengd68fc572010-05-16 10:49:58 -04003117/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003118 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003119 * files in the subvolume, it removes orphan item and frees block_rsv
3120 * structure.
3121 */
3122void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3123 struct btrfs_root *root)
3124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003126 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003127 int ret;
3128
Josef Bacik8a35d952012-05-23 14:26:42 -04003129 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3131 return;
3132
Josef Bacik90290e12011-12-02 15:44:12 -05003133 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003134 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003135 spin_unlock(&root->orphan_lock);
3136 return;
3137 }
3138
3139 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3140 spin_unlock(&root->orphan_lock);
3141 return;
3142 }
3143
3144 block_rsv = root->orphan_block_rsv;
3145 root->orphan_block_rsv = NULL;
3146 spin_unlock(&root->orphan_lock);
3147
Miao Xie27cdeb72014-04-02 19:51:05 +08003148 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003150 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003151 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003152 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003153 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003154 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003155 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3156 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 }
3158
Josef Bacik90290e12011-12-02 15:44:12 -05003159 if (block_rsv) {
3160 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003161 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003162 }
3163}
3164
3165/*
Josef Bacik7b128762008-07-24 12:17:14 -04003166 * This creates an orphan entry for the given inode in case something goes
3167 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168 *
3169 * NOTE: caller of this function should reserve 5 units of metadata for
3170 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003171 */
3172int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3173{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003174 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003175 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 struct btrfs_block_rsv *block_rsv = NULL;
3177 int reserve = 0;
3178 int insert = 0;
3179 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003180
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003182 block_rsv = btrfs_alloc_block_rsv(fs_info,
3183 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003184 if (!block_rsv)
3185 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003186 }
3187
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 spin_lock(&root->orphan_lock);
3189 if (!root->orphan_block_rsv) {
3190 root->orphan_block_rsv = block_rsv;
3191 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003192 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193 block_rsv = NULL;
3194 }
Josef Bacik7b128762008-07-24 12:17:14 -04003195
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198#if 0
3199 /*
3200 * For proper ENOSPC handling, we should do orphan
3201 * cleanup when mounting. But this introduces backward
3202 * compatibility issue.
3203 */
3204 if (!xchg(&root->orphan_item_inserted, 1))
3205 insert = 2;
3206 else
3207 insert = 1;
3208#endif
3209 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003210 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 }
Josef Bacik7b128762008-07-24 12:17:14 -04003212
Josef Bacik72ac3c02012-05-23 14:13:11 -04003213 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3214 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003215 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003216 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003217
Yan, Zhengd68fc572010-05-16 10:49:58 -04003218 /* grab metadata reservation from transaction handle */
3219 if (reserve) {
3220 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003221 ASSERT(!ret);
3222 if (ret) {
3223 atomic_dec(&root->orphan_inodes);
3224 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3225 &BTRFS_I(inode)->runtime_flags);
3226 if (insert)
3227 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3228 &BTRFS_I(inode)->runtime_flags);
3229 return ret;
3230 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 }
3232
3233 /* insert an orphan item to track this unlinked/truncated file */
3234 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003235 ret = btrfs_insert_orphan_item(trans, root,
3236 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003238 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003239 if (reserve) {
3240 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3241 &BTRFS_I(inode)->runtime_flags);
3242 btrfs_orphan_release_metadata(inode);
3243 }
3244 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003245 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3246 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003247 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003248 return ret;
3249 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 }
3251 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 }
3253
3254 /* insert an orphan item to track subvolume contains orphan files */
3255 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003256 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003258 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003259 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003260 return ret;
3261 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 }
3263 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003264}
3265
3266/*
3267 * We have done the truncate/delete so we can go ahead and remove the orphan
3268 * item for this particular inode.
3269 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003270static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3271 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003272{
3273 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 int delete_item = 0;
3275 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276 int ret = 0;
3277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003279 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3280 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003282
Josef Bacik72ac3c02012-05-23 14:13:11 -04003283 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3284 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 spin_unlock(&root->orphan_lock);
3287
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003289 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003290 if (trans)
3291 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003292 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003293 }
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295 if (release_rsv)
3296 btrfs_orphan_release_metadata(inode);
3297
Josef Bacik4ef31a42013-08-13 14:10:08 -04003298 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299}
3300
3301/*
3302 * this cleans up any orphans that may be left on the list from the last use
3303 * of this root.
3304 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003305int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003308 struct btrfs_path *path;
3309 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003310 struct btrfs_key key, found_key;
3311 struct btrfs_trans_handle *trans;
3312 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003313 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003314 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3315
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003317 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003318
3319 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003320 if (!path) {
3321 ret = -ENOMEM;
3322 goto out;
3323 }
David Sterbae4058b52015-11-27 16:31:35 +01003324 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003325
3326 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003327 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003328 key.offset = (u64)-1;
3329
Josef Bacik7b128762008-07-24 12:17:14 -04003330 while (1) {
3331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003332 if (ret < 0)
3333 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003334
3335 /*
3336 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003337 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003338 * find the key and see if we have stuff that matches
3339 */
3340 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003341 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003342 if (path->slots[0] == 0)
3343 break;
3344 path->slots[0]--;
3345 }
3346
3347 /* pull out the item */
3348 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3350
3351 /* make sure the item matches what we want */
3352 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3353 break;
David Sterba962a2982014-06-04 18:41:45 +02003354 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003355 break;
3356
3357 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003358 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003359
3360 /*
3361 * this is where we are basically btrfs_lookup, without the
3362 * crossing root thing. we store the inode number in the
3363 * offset of the orphan item.
3364 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003365
3366 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003367 btrfs_err(fs_info,
3368 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003369 ret = -EINVAL;
3370 goto out;
3371 }
3372
3373 last_objectid = found_key.offset;
3374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003375 found_key.objectid = found_key.offset;
3376 found_key.type = BTRFS_INODE_ITEM_KEY;
3377 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303379 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003380 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003381 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003382
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003384 struct btrfs_root *dead_root;
3385 struct btrfs_fs_info *fs_info = root->fs_info;
3386 int is_dead_root = 0;
3387
3388 /*
3389 * this is an orphan in the tree root. Currently these
3390 * could come from 2 sources:
3391 * a) a snapshot deletion in progress
3392 * b) a free space cache inode
3393 * We need to distinguish those two, as the snapshot
3394 * orphan must not get deleted.
3395 * find_dead_roots already ran before us, so if this
3396 * is a snapshot deletion, we should find the root
3397 * in the dead_roots list
3398 */
3399 spin_lock(&fs_info->trans_lock);
3400 list_for_each_entry(dead_root, &fs_info->dead_roots,
3401 root_list) {
3402 if (dead_root->root_key.objectid ==
3403 found_key.objectid) {
3404 is_dead_root = 1;
3405 break;
3406 }
3407 }
3408 spin_unlock(&fs_info->trans_lock);
3409 if (is_dead_root) {
3410 /* prevent this orphan from being found again */
3411 key.offset = found_key.objectid - 1;
3412 continue;
3413 }
3414 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003415 /*
3416 * Inode is already gone but the orphan item is still there,
3417 * kill the orphan item.
3418 */
Filipe Manana67710892016-06-06 11:51:25 +01003419 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003420 trans = btrfs_start_transaction(root, 1);
3421 if (IS_ERR(trans)) {
3422 ret = PTR_ERR(trans);
3423 goto out;
3424 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 btrfs_debug(fs_info, "auto deleting %Lu",
3426 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 ret = btrfs_del_orphan_item(trans, root,
3428 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003429 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003430 if (ret)
3431 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003432 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433 }
Josef Bacik7b128762008-07-24 12:17:14 -04003434
Josef Bacik7b128762008-07-24 12:17:14 -04003435 /*
3436 * add this inode to the orphan list so btrfs_orphan_del does
3437 * the proper thing when we hit it
3438 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003439 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3440 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003441 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003442
Josef Bacik7b128762008-07-24 12:17:14 -04003443 /* if we have links, this was a truncate, lets do that */
3444 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303445 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003446 iput(inode);
3447 continue;
3448 }
Josef Bacik7b128762008-07-24 12:17:14 -04003449 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003450
3451 /* 1 for the orphan item deletion. */
3452 trans = btrfs_start_transaction(root, 1);
3453 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003454 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003455 ret = PTR_ERR(trans);
3456 goto out;
3457 }
3458 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003459 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003460 if (ret) {
3461 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003462 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003463 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003464
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003465 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003466 if (ret)
3467 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003468 } else {
3469 nr_unlink++;
3470 }
3471
3472 /* this will do delete_inode and everything for us */
3473 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003474 if (ret)
3475 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003476 }
Miao Xie3254c872011-11-10 20:45:05 -05003477 /* release the path since we're done with it */
3478 btrfs_release_path(path);
3479
Yan, Zhengd68fc572010-05-16 10:49:58 -04003480 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3481
3482 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003483 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003484 (u64)-1);
3485
Miao Xie27cdeb72014-04-02 19:51:05 +08003486 if (root->orphan_block_rsv ||
3487 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003488 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003490 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003491 }
Josef Bacik7b128762008-07-24 12:17:14 -04003492
3493 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003495 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497
3498out:
3499 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 btrfs_free_path(path);
3502 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003506 * very simple check to peek ahead in the leaf looking for xattrs. If we
3507 * don't find any xattrs, we know there can't be any acls.
3508 *
3509 * slot is the slot the inode is in, objectid is the objectid of the inode
3510 */
3511static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003512 int slot, u64 objectid,
3513 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003514{
3515 u32 nritems = btrfs_header_nritems(leaf);
3516 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 static u64 xattr_access = 0;
3518 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003519 int scanned = 0;
3520
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003522 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3523 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3524 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3525 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003526 }
3527
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003529 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003530 while (slot < nritems) {
3531 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3532
3533 /* we found a different objectid, there must not be acls */
3534 if (found_key.objectid != objectid)
3535 return 0;
3536
3537 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003538 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003539 if (*first_xattr_slot == -1)
3540 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 if (found_key.offset == xattr_access ||
3542 found_key.offset == xattr_default)
3543 return 1;
3544 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003545
3546 /*
3547 * we found a key greater than an xattr key, there can't
3548 * be any acls later on
3549 */
3550 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3551 return 0;
3552
3553 slot++;
3554 scanned++;
3555
3556 /*
3557 * it goes inode, inode backrefs, xattrs, extents,
3558 * so if there are a ton of hard links to an inode there can
3559 * be a lot of backrefs. Don't waste time searching too hard,
3560 * this is just an optimization
3561 */
3562 if (scanned >= 8)
3563 break;
3564 }
3565 /* we hit the end of the leaf before we found an xattr or
3566 * something larger than an xattr. We have to assume the inode
3567 * has acls
3568 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003569 if (*first_xattr_slot == -1)
3570 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003571 return 1;
3572}
3573
3574/*
Chris Masond352ac62008-09-29 15:18:18 -04003575 * read an inode from the btree into the in-memory inode
3576 */
Filipe Manana67710892016-06-06 11:51:25 +01003577static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003578{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003580 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003581 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003582 struct btrfs_inode_item *inode_item;
3583 struct btrfs_root *root = BTRFS_I(inode)->root;
3584 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003585 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003586 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003587 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003588 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003589 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003590 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003591
3592 ret = btrfs_fill_inode(inode, &rdev);
3593 if (!ret)
3594 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003595
3596 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003597 if (!path) {
3598 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003599 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003600 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003601
Chris Mason39279cc2007-06-12 06:35:45 -04003602 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003603
Chris Mason39279cc2007-06-12 06:35:45 -04003604 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003605 if (ret) {
3606 if (ret > 0)
3607 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003608 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003609 }
Chris Mason39279cc2007-06-12 06:35:45 -04003610
Chris Mason5f39d392007-10-15 16:14:19 -04003611 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003612
3613 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003614 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003615
Chris Mason5f39d392007-10-15 16:14:19 -04003616 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3617 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003618 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003619 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003620 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3621 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003622 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3625 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
David Sterbaa937b972014-12-12 17:39:12 +01003627 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3628 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003629
David Sterbaa937b972014-12-12 17:39:12 +01003630 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3631 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003632
chandan r9cc97d62012-07-04 12:48:07 +05303633 BTRFS_I(inode)->i_otime.tv_sec =
3634 btrfs_timespec_sec(leaf, &inode_item->otime);
3635 BTRFS_I(inode)->i_otime.tv_nsec =
3636 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003637
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003638 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003639 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003640 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3641
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003642 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003643 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003644 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 rdev = btrfs_inode_rdev(leaf, inode_item);
3646
Josef Bacikaec74772008-07-24 12:12:38 -04003647 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003649
3650cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003651 /*
3652 * If we were modified in the current generation and evicted from memory
3653 * and then re-read we need to do a full sync since we don't have any
3654 * idea about which extents were modified before we were evicted from
3655 * cache.
3656 *
3657 * This is required for both inode re-read from disk and delayed inode
3658 * in delayed_nodes_tree.
3659 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003660 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003661 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3662 &BTRFS_I(inode)->runtime_flags);
3663
Filipe Mananabde6c242015-07-24 00:00:19 +01003664 /*
3665 * We don't persist the id of the transaction where an unlink operation
3666 * against the inode was last made. So here we assume the inode might
3667 * have been evicted, and therefore the exact value of last_unlink_trans
3668 * lost, and set it to last_trans to avoid metadata inconsistencies
3669 * between the inode and its parent if the inode is fsync'ed and the log
3670 * replayed. For example, in the scenario:
3671 *
3672 * touch mydir/foo
3673 * ln mydir/foo mydir/bar
3674 * sync
3675 * unlink mydir/bar
3676 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3677 * xfs_io -c fsync mydir/foo
3678 * <power failure>
3679 * mount fs, triggers fsync log replay
3680 *
3681 * We must make sure that when we fsync our inode foo we also log its
3682 * parent inode, otherwise after log replay the parent still has the
3683 * dentry with the "bar" name but our inode foo has a link count of 1
3684 * and doesn't have an inode ref with the name "bar" anymore.
3685 *
3686 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003687 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003688 * transaction commits on fsync if our inode is a directory, or if our
3689 * inode is not a directory, logging its parent unnecessarily.
3690 */
3691 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3692
Miao Xie67de1172013-12-26 13:07:06 +08003693 path->slots[0]++;
3694 if (inode->i_nlink != 1 ||
3695 path->slots[0] >= btrfs_header_nritems(leaf))
3696 goto cache_acl;
3697
3698 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003699 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003700 goto cache_acl;
3701
3702 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3703 if (location.type == BTRFS_INODE_REF_KEY) {
3704 struct btrfs_inode_ref *ref;
3705
3706 ref = (struct btrfs_inode_ref *)ptr;
3707 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3708 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3709 struct btrfs_inode_extref *extref;
3710
3711 extref = (struct btrfs_inode_extref *)ptr;
3712 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3713 extref);
3714 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003715cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003716 /*
3717 * try to precache a NULL acl entry for files that don't have
3718 * any xattrs or acls
3719 */
Li Zefan33345d012011-04-20 10:31:50 +08003720 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003721 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003722 if (first_xattr_slot != -1) {
3723 path->slots[0] = first_xattr_slot;
3724 ret = btrfs_load_inode_props(inode, path);
3725 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003726 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003727 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003728 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003729 root->root_key.objectid, ret);
3730 }
3731 btrfs_free_path(path);
3732
Al Viro72c04902009-06-24 16:58:48 -04003733 if (!maybe_acls)
3734 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003735
Chris Mason39279cc2007-06-12 06:35:45 -04003736 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003737 case S_IFREG:
3738 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003739 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003740 inode->i_fop = &btrfs_file_operations;
3741 inode->i_op = &btrfs_file_inode_operations;
3742 break;
3743 case S_IFDIR:
3744 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003745 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003746 break;
3747 case S_IFLNK:
3748 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003749 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003750 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3751 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003752 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003753 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003754 init_special_inode(inode, inode->i_mode, rdev);
3755 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003757
3758 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003759 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003760
3761make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003762 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003763 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003764 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003765}
3766
Chris Masond352ac62008-09-29 15:18:18 -04003767/*
3768 * given a leaf and an inode, copy the inode fields into the leaf
3769 */
Chris Masone02119d2008-09-05 16:13:11 -04003770static void fill_inode_item(struct btrfs_trans_handle *trans,
3771 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003772 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003773 struct inode *inode)
3774{
Liu Bo51fab692012-12-27 09:01:21 +00003775 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003776
Liu Bo51fab692012-12-27 09:01:21 +00003777 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003778
Liu Bo51fab692012-12-27 09:01:21 +00003779 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3780 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3781 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3782 &token);
3783 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3784 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003785
David Sterbaa937b972014-12-12 17:39:12 +01003786 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003787 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003788 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003789 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003790
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003793 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003794 inode->i_mtime.tv_nsec, &token);
3795
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003798 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003799 inode->i_ctime.tv_nsec, &token);
3800
chandan r9cc97d62012-07-04 12:48:07 +05303801 btrfs_set_token_timespec_sec(leaf, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_sec, &token);
3803 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3804 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3805
Liu Bo51fab692012-12-27 09:01:21 +00003806 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3807 &token);
3808 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3809 &token);
3810 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3811 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3812 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3813 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3814 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003815}
3816
Chris Masond352ac62008-09-29 15:18:18 -04003817/*
3818 * copy everything in the in-memory inode into the btree.
3819 */
Chris Mason21151332011-11-10 20:39:08 -05003820static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003821 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003822{
3823 struct btrfs_inode_item *inode_item;
3824 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003825 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003826 int ret;
3827
3828 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003829 if (!path)
3830 return -ENOMEM;
3831
Chris Masonb9473432009-03-13 11:00:37 -04003832 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003833 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3834 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003835 if (ret) {
3836 if (ret > 0)
3837 ret = -ENOENT;
3838 goto failed;
3839 }
3840
Chris Mason5f39d392007-10-15 16:14:19 -04003841 leaf = path->nodes[0];
3842 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003843 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003844
Chris Masone02119d2008-09-05 16:13:11 -04003845 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003846 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003847 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003848 ret = 0;
3849failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003850 btrfs_free_path(path);
3851 return ret;
3852}
3853
Chris Masond352ac62008-09-29 15:18:18 -04003854/*
Chris Mason21151332011-11-10 20:39:08 -05003855 * copy everything in the in-memory inode into the btree.
3856 */
3857noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root, struct inode *inode)
3859{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003860 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003861 int ret;
3862
3863 /*
3864 * If the inode is a free space inode, we can deadlock during commit
3865 * if we put it into the delayed code.
3866 *
3867 * The data relocation inode should also be directly updated
3868 * without delay
3869 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003870 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003871 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003872 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003873 btrfs_update_root_times(trans, root);
3874
Chris Mason21151332011-11-10 20:39:08 -05003875 ret = btrfs_delayed_update_inode(trans, root, inode);
3876 if (!ret)
3877 btrfs_set_inode_last_trans(trans, inode);
3878 return ret;
3879 }
3880
3881 return btrfs_update_inode_item(trans, root, inode);
3882}
3883
Josef Bacikbe6aef62012-10-22 15:43:12 -04003884noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root,
3886 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003887{
3888 int ret;
3889
3890 ret = btrfs_update_inode(trans, root, inode);
3891 if (ret == -ENOSPC)
3892 return btrfs_update_inode_item(trans, root, inode);
3893 return ret;
3894}
3895
3896/*
Chris Masond352ac62008-09-29 15:18:18 -04003897 * unlink helper that gets used here in inode.c and in the tree logging
3898 * recovery code. It remove a link in a directory with a given name, and
3899 * also drops the back refs in the inode to the directory
3900 */
Al Viro92986792011-03-04 17:14:37 +00003901static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3902 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003903 struct btrfs_inode *dir,
3904 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003905 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003906{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003912 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003913 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003914 u64 ino = btrfs_ino(inode);
3915 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003916
3917 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003918 if (!path) {
3919 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003920 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003921 }
3922
Chris Masonb9473432009-03-13 11:00:37 -04003923 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003924 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003925 name, name_len, -1);
3926 if (IS_ERR(di)) {
3927 ret = PTR_ERR(di);
3928 goto err;
3929 }
3930 if (!di) {
3931 ret = -ENOENT;
3932 goto err;
3933 }
Chris Mason5f39d392007-10-15 16:14:19 -04003934 leaf = path->nodes[0];
3935 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003936 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003937 if (ret)
3938 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003939 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003940
Miao Xie67de1172013-12-26 13:07:06 +08003941 /*
3942 * If we don't have dir index, we have to get it by looking up
3943 * the inode ref, since we get the inode ref, remove it directly,
3944 * it is unnecessary to do delayed deletion.
3945 *
3946 * But if we have dir index, needn't search inode ref to get it.
3947 * Since the inode ref is close to the inode item, it is better
3948 * that we delay to delete it, and just do this deletion when
3949 * we update the inode item.
3950 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003951 if (inode->dir_index) {
3952 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003953 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003954 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003955 goto skip_backref;
3956 }
3957 }
3958
Li Zefan33345d012011-04-20 10:31:50 +08003959 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3960 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003961 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003962 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003963 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003964 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003965 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003966 goto err;
3967 }
Miao Xie67de1172013-12-26 13:07:06 +08003968skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003969 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003971 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003972 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 }
Chris Mason39279cc2007-06-12 06:35:45 -04003974
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003975 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3976 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003978 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 goto err;
3980 }
Chris Masone02119d2008-09-05 16:13:11 -04003981
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003982 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3983 index);
Chris Mason6418c962010-10-30 07:34:24 -04003984 if (ret == -ENOENT)
3985 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003986 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003987 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003988err:
3989 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003990 if (ret)
3991 goto out;
3992
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003993 btrfs_i_size_write(&dir->vfs_inode,
3994 dir->vfs_inode.i_size - name_len * 2);
3995 inode_inc_iversion(&inode->vfs_inode);
3996 inode_inc_iversion(&dir->vfs_inode);
3997 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3998 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3999 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004000out:
Chris Mason39279cc2007-06-12 06:35:45 -04004001 return ret;
4002}
4003
Al Viro92986792011-03-04 17:14:37 +00004004int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004006 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004007 const char *name, int name_len)
4008{
4009 int ret;
4010 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4011 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004012 drop_nlink(&inode->vfs_inode);
4013 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004014 }
4015 return ret;
4016}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004017
4018/*
4019 * helper to start transaction for unlink and rmdir.
4020 *
Josef Bacikd52be812013-05-29 14:54:47 -04004021 * unlink and rmdir are special in btrfs, they do not always free space, so
4022 * if we cannot make our reservations the normal way try and see if there is
4023 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4024 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 */
Josef Bacikd52be812013-05-29 14:54:47 -04004026static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029
Josef Bacike70bea52011-10-11 14:18:24 -04004030 /*
4031 * 1 for the possible orphan item
4032 * 1 for the dir item
4033 * 1 for the dir index
4034 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004035 * 1 for the inode
4036 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004037 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038}
4039
Chris Mason39279cc2007-06-12 06:35:45 -04004040static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4041{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004044 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004045 int ret;
4046
Josef Bacikd52be812013-05-29 14:54:47 -04004047 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004048 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004049 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004050
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4052 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004053
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004054 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4055 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4056 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004057 if (ret)
4058 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004059
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004061 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004062 if (ret)
4063 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004064 }
Josef Bacik7b128762008-07-24 12:17:14 -04004065
Tsutomu Itohb5324022011-07-19 07:27:20 +00004066out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004067 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004068 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004069 return ret;
4070}
4071
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4073 struct btrfs_root *root,
4074 struct inode *dir, u64 objectid,
4075 const char *name, int name_len)
4076{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004077 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004078 struct btrfs_path *path;
4079 struct extent_buffer *leaf;
4080 struct btrfs_dir_item *di;
4081 struct btrfs_key key;
4082 u64 index;
4083 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004084 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085
4086 path = btrfs_alloc_path();
4087 if (!path)
4088 return -ENOMEM;
4089
Li Zefan33345d012011-04-20 10:31:50 +08004090 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004091 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 if (IS_ERR_OR_NULL(di)) {
4093 if (!di)
4094 ret = -ENOENT;
4095 else
4096 ret = PTR_ERR(di);
4097 goto out;
4098 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099
4100 leaf = path->nodes[0];
4101 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4102 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4103 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 goto out;
4107 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004108 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004110 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4111 root->root_key.objectid, dir_ino,
4112 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004113 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004115 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 goto out;
4117 }
Li Zefan33345d012011-04-20 10:31:50 +08004118 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004120 if (IS_ERR_OR_NULL(di)) {
4121 if (!di)
4122 ret = -ENOENT;
4123 else
4124 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004125 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 goto out;
4127 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128
4129 leaf = path->nodes[0];
4130 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132 index = key.offset;
4133 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004134 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004136 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004138 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 goto out;
4140 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004141
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004142 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004143 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004144 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004145 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004147 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004149 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151}
4152
Chris Mason39279cc2007-06-12 06:35:45 -04004153static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4154{
David Howells2b0143b2015-03-17 22:25:59 +00004155 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004156 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004157 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004158 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004159 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004160
David Sterbab3ae2442012-09-13 16:04:34 -06004161 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004162 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004163 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004164 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004165
Josef Bacikd52be812013-05-29 14:54:47 -04004166 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004168 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004170 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004171 err = btrfs_unlink_subvol(trans, root, dir,
4172 BTRFS_I(inode)->location.objectid,
4173 dentry->d_name.name,
4174 dentry->d_name.len);
4175 goto out;
4176 }
4177
Josef Bacik7b128762008-07-24 12:17:14 -04004178 err = btrfs_orphan_add(trans, inode);
4179 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004180 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004181
Filipe Manana44f714d2016-06-06 16:11:13 +01004182 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4183
Chris Mason39279cc2007-06-12 06:35:45 -04004184 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004185 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4186 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4187 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004188 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004189 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004190 /*
4191 * Propagate the last_unlink_trans value of the deleted dir to
4192 * its parent directory. This is to prevent an unrecoverable
4193 * log tree in the case we do something like this:
4194 * 1) create dir foo
4195 * 2) create snapshot under dir foo
4196 * 3) delete the snapshot
4197 * 4) rmdir foo
4198 * 5) mkdir foo
4199 * 6) fsync foo or some file inside foo
4200 */
4201 if (last_unlink_trans >= trans->transid)
4202 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4203 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004205 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004206 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004207
Chris Mason39279cc2007-06-12 06:35:45 -04004208 return err;
4209}
4210
Chris Mason28f75a02015-02-04 06:59:29 -08004211static int truncate_space_check(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
4213 u64 bytes_deleted)
4214{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004216 int ret;
4217
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004218 /*
4219 * This is only used to apply pressure to the enospc system, we don't
4220 * intend to use this reservation at all.
4221 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004222 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 bytes_deleted *= fs_info->nodesize;
4224 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004225 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004226 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004227 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004228 trans->transid,
4229 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004230 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004231 }
Chris Mason28f75a02015-02-04 06:59:29 -08004232 return ret;
4233
4234}
4235
Filipe Manana0305cd52015-10-16 12:34:25 +01004236static int truncate_inline_extent(struct inode *inode,
4237 struct btrfs_path *path,
4238 struct btrfs_key *found_key,
4239 const u64 item_end,
4240 const u64 new_size)
4241{
4242 struct extent_buffer *leaf = path->nodes[0];
4243 int slot = path->slots[0];
4244 struct btrfs_file_extent_item *fi;
4245 u32 size = (u32)(new_size - found_key->offset);
4246 struct btrfs_root *root = BTRFS_I(inode)->root;
4247
4248 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4249
4250 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4251 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004252 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004253
4254 /*
4255 * Zero out the remaining of the last page of our inline extent,
4256 * instead of directly truncating our inline extent here - that
4257 * would be much more complex (decompressing all the data, then
4258 * compressing the truncated data, which might be bigger than
4259 * the size of the inline extent, resize the extent, etc).
4260 * We release the path because to get the page we might need to
4261 * read the extent item from disk (data not in the page cache).
4262 */
4263 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304264 return btrfs_truncate_block(inode, offset, page_end - offset,
4265 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004266 }
4267
4268 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4269 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004270 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004271
4272 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4273 inode_sub_bytes(inode, item_end + 1 - new_size);
4274
4275 return 0;
4276}
4277
Chris Mason323ac952008-10-01 19:05:46 -04004278/*
Chris Mason39279cc2007-06-12 06:35:45 -04004279 * this can truncate away extent items, csum items and directory items.
4280 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004281 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004282 *
4283 * csum items that cross the new i_size are truncated to the new size
4284 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004285 *
4286 * min_type is the minimum key type to truncate down to. If set to 0, this
4287 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004288 */
Yan, Zheng80825102009-11-12 09:35:36 +00004289int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4290 struct btrfs_root *root,
4291 struct inode *inode,
4292 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004294 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004296 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004298 struct btrfs_key key;
4299 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004300 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004301 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004302 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004303 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004304 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004305 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004306 int found_extent;
4307 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004308 int pending_del_nr = 0;
4309 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004310 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004311 int ret;
4312 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004313 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004314 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004315 bool be_nice = 0;
4316 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004317 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004318
4319 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004320
Chris Mason28ed1342014-12-17 09:41:04 -08004321 /*
4322 * for non-free space inodes and ref cows, we want to back off from
4323 * time to time
4324 */
4325 if (!btrfs_is_free_space_inode(inode) &&
4326 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4327 be_nice = 1;
4328
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004329 path = btrfs_alloc_path();
4330 if (!path)
4331 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004332 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004333
Josef Bacik5dc562c2012-08-17 13:14:17 -04004334 /*
4335 * We want to drop from the next block forward in case this new size is
4336 * not block aligned since we will be keeping the last block of the
4337 * extent just the way it is.
4338 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004339 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004340 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004341 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004342 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004343 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004344
Miao Xie16cdcec2011-04-22 18:12:22 +08004345 /*
4346 * This function is also used to drop the items in the log tree before
4347 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4348 * it is used to drop the loged items. So we shouldn't kill the delayed
4349 * items.
4350 */
4351 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004352 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004353
Li Zefan33345d012011-04-20 10:31:50 +08004354 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004355 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004356 key.type = (u8)-1;
4357
Chris Mason85e21ba2008-01-29 15:11:36 -05004358search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004359 /*
4360 * with a 16K leaf size and 128MB extents, you can actually queue
4361 * up a huge file in a single leaf. Most of the time that
4362 * bytes_deleted is > 0, it will be huge by the time we get here
4363 */
Byongho Leeee221842015-12-15 01:42:10 +09004364 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004365 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004366 err = -EAGAIN;
4367 goto error;
4368 }
4369 }
4370
4371
Chris Masonb9473432009-03-13 11:00:37 -04004372 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004373 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004374 if (ret < 0) {
4375 err = ret;
4376 goto out;
4377 }
Chris Masond3977122009-01-05 21:25:51 -05004378
Chris Mason85e21ba2008-01-29 15:11:36 -05004379 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004380 /* there are no items in the tree for us to truncate, we're
4381 * done
4382 */
Yan, Zheng80825102009-11-12 09:35:36 +00004383 if (path->slots[0] == 0)
4384 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004385 path->slots[0]--;
4386 }
4387
Chris Masond3977122009-01-05 21:25:51 -05004388 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004389 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004390 leaf = path->nodes[0];
4391 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004392 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004393
Li Zefan33345d012011-04-20 10:31:50 +08004394 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004395 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004396
Chris Mason85e21ba2008-01-29 15:11:36 -05004397 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004398 break;
4399
Chris Mason5f39d392007-10-15 16:14:19 -04004400 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004401 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004402 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004403 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004404 extent_type = btrfs_file_extent_type(leaf, fi);
4405 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004406 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004407 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004408 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004409 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004410 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004411 }
Yan008630c2007-11-07 13:31:09 -05004412 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004413 }
Yan, Zheng80825102009-11-12 09:35:36 +00004414 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004415 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004416 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004417 if (item_end < new_size) {
4418 /*
4419 * With NO_HOLES mode, for the following mapping
4420 *
4421 * [0-4k][hole][8k-12k]
4422 *
4423 * if truncating isize down to 6k, it ends up
4424 * isize being 8k.
4425 */
4426 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4427 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004428 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004429 }
Yan, Zheng80825102009-11-12 09:35:36 +00004430 if (found_key.offset >= new_size)
4431 del_item = 1;
4432 else
4433 del_item = 0;
4434 }
Chris Mason39279cc2007-06-12 06:35:45 -04004435 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004436 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004437 if (found_type != BTRFS_EXTENT_DATA_KEY)
4438 goto delete;
4439
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004440 if (del_item)
4441 last_size = found_key.offset;
4442 else
4443 last_size = new_size;
4444
Chris Mason179e29e2007-11-01 11:28:41 -04004445 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004446 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004447 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004448 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004449 u64 orig_num_bytes =
4450 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004451 extent_num_bytes = ALIGN(new_size -
4452 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004453 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004454 btrfs_set_file_extent_num_bytes(leaf, fi,
4455 extent_num_bytes);
4456 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004457 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004458 if (test_bit(BTRFS_ROOT_REF_COWS,
4459 &root->state) &&
4460 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004461 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004462 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004463 } else {
Chris Masondb945352007-10-15 16:15:53 -04004464 extent_num_bytes =
4465 btrfs_file_extent_disk_num_bytes(leaf,
4466 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004467 extent_offset = found_key.offset -
4468 btrfs_file_extent_offset(leaf, fi);
4469
Chris Mason39279cc2007-06-12 06:35:45 -04004470 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004471 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004472 if (extent_start != 0) {
4473 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004474 if (test_bit(BTRFS_ROOT_REF_COWS,
4475 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004476 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004477 }
4478 }
Chris Mason90692182008-02-08 13:49:28 -05004479 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004480 /*
4481 * we can't truncate inline items that have had
4482 * special encodings
4483 */
4484 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004485 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4486 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004487
4488 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004489 * Need to release path in order to truncate a
4490 * compressed extent. So delete any accumulated
4491 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004492 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004493 if (btrfs_file_extent_compression(leaf, fi) !=
4494 BTRFS_COMPRESS_NONE && pending_del_nr) {
4495 err = btrfs_del_items(trans, root, path,
4496 pending_del_slot,
4497 pending_del_nr);
4498 if (err) {
4499 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004500 err);
4501 goto error;
4502 }
4503 pending_del_nr = 0;
4504 }
4505
4506 err = truncate_inline_extent(inode, path,
4507 &found_key,
4508 item_end,
4509 new_size);
4510 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004511 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004512 goto error;
4513 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004514 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4515 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004516 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004517 }
Chris Mason39279cc2007-06-12 06:35:45 -04004518 }
Chris Mason179e29e2007-11-01 11:28:41 -04004519delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004520 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004521 if (!pending_del_nr) {
4522 /* no pending yet, add ourselves */
4523 pending_del_slot = path->slots[0];
4524 pending_del_nr = 1;
4525 } else if (pending_del_nr &&
4526 path->slots[0] + 1 == pending_del_slot) {
4527 /* hop on the pending chunk */
4528 pending_del_nr++;
4529 pending_del_slot = path->slots[0];
4530 } else {
Chris Masond3977122009-01-05 21:25:51 -05004531 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004532 }
Chris Mason39279cc2007-06-12 06:35:45 -04004533 } else {
4534 break;
4535 }
Chris Mason28f75a02015-02-04 06:59:29 -08004536 should_throttle = 0;
4537
Miao Xie27cdeb72014-04-02 19:51:05 +08004538 if (found_extent &&
4539 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004540 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004541 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004542 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004543 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004544 extent_num_bytes, 0,
4545 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004546 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004547 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004548 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4549 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004550 trans->delayed_ref_updates * 2,
4551 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004552 if (be_nice) {
4553 if (truncate_space_check(trans, root,
4554 extent_num_bytes)) {
4555 should_end = 1;
4556 }
4557 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004558 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004559 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004560 }
Chris Mason39279cc2007-06-12 06:35:45 -04004561 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004562
Yan, Zheng80825102009-11-12 09:35:36 +00004563 if (found_type == BTRFS_INODE_ITEM_KEY)
4564 break;
4565
4566 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004567 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004568 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004569 if (pending_del_nr) {
4570 ret = btrfs_del_items(trans, root, path,
4571 pending_del_slot,
4572 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004573 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004574 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004575 goto error;
4576 }
Yan, Zheng80825102009-11-12 09:35:36 +00004577 pending_del_nr = 0;
4578 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004579 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004580 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004581 unsigned long updates = trans->delayed_ref_updates;
4582 if (updates) {
4583 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004584 ret = btrfs_run_delayed_refs(trans,
4585 fs_info,
4586 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004587 if (ret && !err)
4588 err = ret;
4589 }
4590 }
Chris Mason28f75a02015-02-04 06:59:29 -08004591 /*
4592 * if we failed to refill our space rsv, bail out
4593 * and let the transaction restart
4594 */
4595 if (should_end) {
4596 err = -EAGAIN;
4597 goto error;
4598 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004599 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004600 } else {
4601 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004602 }
Chris Mason39279cc2007-06-12 06:35:45 -04004603 }
Yan, Zheng80825102009-11-12 09:35:36 +00004604out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004605 if (pending_del_nr) {
4606 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4607 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004608 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004609 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004610 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004611error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004612 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004613 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004614
Chris Mason39279cc2007-06-12 06:35:45 -04004615 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004616
Liu Bo19fd2df2016-12-01 13:01:02 -08004617 if (err == 0) {
4618 /* only inline file may have last_size != new_size */
4619 if (new_size >= fs_info->sectorsize ||
4620 new_size > fs_info->max_inline)
4621 ASSERT(last_size == new_size);
4622 }
4623
Byongho Leeee221842015-12-15 01:42:10 +09004624 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004625 unsigned long updates = trans->delayed_ref_updates;
4626 if (updates) {
4627 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004628 ret = btrfs_run_delayed_refs(trans, fs_info,
4629 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004630 if (ret && !err)
4631 err = ret;
4632 }
4633 }
Yan, Zheng80825102009-11-12 09:35:36 +00004634 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004635}
4636
Chris Masona52d9a82007-08-27 16:49:44 -04004637/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304638 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004639 * @inode - inode that we're zeroing
4640 * @from - the offset to start zeroing
4641 * @len - the length to zero, 0 to zero the entire range respective to the
4642 * offset
4643 * @front - zero up to the offset instead of from the offset on
4644 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304645 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004646 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004647 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304648int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004649 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004652 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4654 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004655 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004656 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004657 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004658 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304659 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004660 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004661 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004662 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304663 u64 block_start;
4664 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004665
Josef Bacik2aaa6652012-08-29 14:27:18 -04004666 if ((offset & (blocksize - 1)) == 0 &&
4667 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004668 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304669
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004670 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304671 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004672 if (ret)
4673 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004674
Chris Mason211c17f2008-05-15 09:13:45 -04004675again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004676 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004677 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004678 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304679 round_down(from, blocksize),
4680 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004681 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004682 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004683 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004684
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304685 block_start = round_down(from, blocksize);
4686 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004687
Chris Masona52d9a82007-08-27 16:49:44 -04004688 if (!PageUptodate(page)) {
4689 ret = btrfs_readpage(NULL, page);
4690 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004691 if (page->mapping != mapping) {
4692 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004693 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004694 goto again;
4695 }
Chris Masona52d9a82007-08-27 16:49:44 -04004696 if (!PageUptodate(page)) {
4697 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004698 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004699 }
4700 }
Chris Mason211c17f2008-05-15 09:13:45 -04004701 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004702
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304703 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004704 set_page_extent_mapped(page);
4705
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004707 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304708 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004709 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004711 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004712 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713 btrfs_put_ordered_extent(ordered);
4714 goto again;
4715 }
4716
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304717 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004718 EXTENT_DIRTY | EXTENT_DELALLOC |
4719 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004720 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004721
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004723 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004724 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004726 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004727 goto out_unlock;
4728 }
4729
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304730 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004731 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 memset(kaddr + (block_start - page_offset(page)),
4736 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004737 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 memset(kaddr + (block_start - page_offset(page)) + offset,
4739 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740 flush_dcache_page(page);
4741 kunmap(page);
4742 }
Chris Mason247e7432008-07-17 12:53:51 -04004743 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004744 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304745 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004746 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004747
Chris Mason89642222008-07-24 09:41:53 -04004748out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004749 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750 btrfs_delalloc_release_space(inode, block_start,
4751 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004752 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004753 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004754out:
4755 return ret;
4756}
4757
Josef Bacik16e75492013-10-22 12:18:51 -04004758static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4759 u64 offset, u64 len)
4760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004762 struct btrfs_trans_handle *trans;
4763 int ret;
4764
4765 /*
4766 * Still need to make sure the inode looks like it's been updated so
4767 * that any holes get logged if we fsync.
4768 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004769 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4770 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004771 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4772 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4773 return 0;
4774 }
4775
4776 /*
4777 * 1 - for the one we're dropping
4778 * 1 - for the one we're adding
4779 * 1 - for updating the inode.
4780 */
4781 trans = btrfs_start_transaction(root, 3);
4782 if (IS_ERR(trans))
4783 return PTR_ERR(trans);
4784
4785 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4786 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004787 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004788 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004789 return ret;
4790 }
4791
David Sterbaf85b7372017-01-20 14:54:07 +01004792 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4793 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004794 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004795 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004796 else
4797 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004798 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004799 return ret;
4800}
4801
Josef Bacik695a0d02011-03-04 15:46:53 -05004802/*
4803 * This function puts in dummy file extents for the area we're creating a hole
4804 * for. So if we are truncating this file to a larger size we need to insert
4805 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4806 * the range between oldsize and size
4807 */
Josef Bacika41ad392011-01-31 15:30:16 -05004808int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004809{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004810 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004811 struct btrfs_root *root = BTRFS_I(inode)->root;
4812 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004813 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004814 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004815 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004816 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4817 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004818 u64 last_byte;
4819 u64 cur_offset;
4820 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004821 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004822
Josef Bacika71754f2013-06-17 17:14:39 -04004823 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 * If our size started in the middle of a block we need to zero out the
4825 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004826 * expose stale data.
4827 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004829 if (err)
4830 return err;
4831
Yan Zheng9036c102008-10-30 14:19:41 -04004832 if (size <= hole_start)
4833 return 0;
4834
Yan Zheng9036c102008-10-30 14:19:41 -04004835 while (1) {
4836 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004837
David Sterbaff13db42015-12-03 14:30:40 +01004838 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004839 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004840 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4841 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004842 if (!ordered)
4843 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004844 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4845 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004846 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004847 btrfs_put_ordered_extent(ordered);
4848 }
4849
Yan Zheng9036c102008-10-30 14:19:41 -04004850 cur_offset = hole_start;
4851 while (1) {
4852 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4853 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004854 if (IS_ERR(em)) {
4855 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004856 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857 break;
4858 }
Yan Zheng9036c102008-10-30 14:19:41 -04004859 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004860 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004861 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004862 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004863 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004864
Josef Bacik16e75492013-10-22 12:18:51 -04004865 err = maybe_insert_hole(root, inode, cur_offset,
4866 hole_size);
4867 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004868 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004869 btrfs_drop_extent_cache(inode, cur_offset,
4870 cur_offset + hole_size - 1, 0);
4871 hole_em = alloc_extent_map();
4872 if (!hole_em) {
4873 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4874 &BTRFS_I(inode)->runtime_flags);
4875 goto next;
4876 }
4877 hole_em->start = cur_offset;
4878 hole_em->len = hole_size;
4879 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004880
Josef Bacik5dc562c2012-08-17 13:14:17 -04004881 hole_em->block_start = EXTENT_MAP_HOLE;
4882 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004883 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004884 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004886 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004888
4889 while (1) {
4890 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004891 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004892 write_unlock(&em_tree->lock);
4893 if (err != -EEXIST)
4894 break;
4895 btrfs_drop_extent_cache(inode, cur_offset,
4896 cur_offset +
4897 hole_size - 1, 0);
4898 }
4899 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004900 }
Josef Bacik16e75492013-10-22 12:18:51 -04004901next:
Yan Zheng9036c102008-10-30 14:19:41 -04004902 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004903 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004904 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004905 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004906 break;
4907 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004908 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004909 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4910 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004911 return err;
4912}
4913
Eric Sandeen3972f262013-01-12 02:57:22 +00004914static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004915{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004916 struct btrfs_root *root = BTRFS_I(inode)->root;
4917 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004918 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004919 loff_t newsize = attr->ia_size;
4920 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004921 int ret;
4922
Eric Sandeen3972f262013-01-12 02:57:22 +00004923 /*
4924 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4925 * special case where we need to update the times despite not having
4926 * these flags set. For all other operations the VFS set these flags
4927 * explicitly if it wants a timestamp update.
4928 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004929 if (newsize != oldsize) {
4930 inode_inc_iversion(inode);
4931 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4932 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004933 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004934 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004935
Josef Bacika41ad392011-01-31 15:30:16 -05004936 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004937 /*
4938 * Don't do an expanding truncate while snapshoting is ongoing.
4939 * This is to ensure the snapshot captures a fully consistent
4940 * state of this file - if the snapshot captures this expanding
4941 * truncation, it must capture all writes that happened before
4942 * this truncation.
4943 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004944 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004945 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 if (ret) {
4947 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004948 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 }
Yan, Zheng80825102009-11-12 09:35:36 +00004950
Miao Xief4a2f4c2011-12-14 20:12:01 -05004951 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 if (IS_ERR(trans)) {
4953 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004954 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004955 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004956
4957 i_size_write(inode, newsize);
4958 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304959 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004960 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004961 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004962 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004963 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004964
Josef Bacika41ad392011-01-31 15:30:16 -05004965 /*
4966 * We're truncating a file that used to have good data down to
4967 * zero. Make sure it gets into the ordered flush list so that
4968 * any new writes get down to disk quickly.
4969 */
4970 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004971 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4972 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004973
Josef Bacikf3fe8202013-01-07 17:03:21 -05004974 /*
4975 * 1 for the orphan item we're going to add
4976 * 1 for the orphan item deletion.
4977 */
4978 trans = btrfs_start_transaction(root, 2);
4979 if (IS_ERR(trans))
4980 return PTR_ERR(trans);
4981
4982 /*
4983 * We need to do this in case we fail at _any_ point during the
4984 * actual truncate. Once we do the truncate_setsize we could
4985 * invalidate pages which forces any outstanding ordered io to
4986 * be instantly completed which will give us extents that need
4987 * to be truncated. If we fail to get an orphan inode down we
4988 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004989 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004990 * will be consistent.
4991 */
4992 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004993 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004994 if (ret)
4995 return ret;
4996
Josef Bacika41ad392011-01-31 15:30:16 -05004997 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4998 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004999
5000 /* Disable nonlocked read DIO to avoid the end less truncate */
5001 btrfs_inode_block_unlocked_dio(inode);
5002 inode_dio_wait(inode);
5003 btrfs_inode_resume_unlocked_dio(inode);
5004
Josef Bacika41ad392011-01-31 15:30:16 -05005005 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005006 if (ret && inode->i_nlink) {
5007 int err;
5008
Liu Bo19fd2df2016-12-01 13:01:02 -08005009 /* To get a stable disk_i_size */
5010 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5011 if (err) {
5012 btrfs_orphan_del(NULL, inode);
5013 return err;
5014 }
5015
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005016 /*
5017 * failed to truncate, disk_i_size is only adjusted down
5018 * as we remove extents, so it should represent the true
5019 * size of the inode, so reset the in memory size and
5020 * delete our orphan entry.
5021 */
5022 trans = btrfs_join_transaction(root);
5023 if (IS_ERR(trans)) {
5024 btrfs_orphan_del(NULL, inode);
5025 return ret;
5026 }
5027 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5028 err = btrfs_orphan_del(trans, inode);
5029 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005030 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005031 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005032 }
Yan, Zheng80825102009-11-12 09:35:36 +00005033 }
5034
Josef Bacika41ad392011-01-31 15:30:16 -05005035 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005036}
5037
Chris Mason39279cc2007-06-12 06:35:45 -04005038static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5039{
David Howells2b0143b2015-03-17 22:25:59 +00005040 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005041 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005042 int err;
5043
Li Zefanb83cc962010-12-20 16:04:08 +08005044 if (btrfs_root_readonly(root))
5045 return -EROFS;
5046
Jan Kara31051c82016-05-26 16:55:18 +02005047 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005048 if (err)
5049 return err;
5050
Chris Mason5a3f23d2009-03-31 13:27:11 -04005051 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005052 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005053 if (err)
5054 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005055 }
Yan Zheng9036c102008-10-30 14:19:41 -04005056
Christoph Hellwig10257742010-06-04 11:30:02 +02005057 if (attr->ia_valid) {
5058 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005059 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005060 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005061
Josef Bacik22c44fe2011-11-30 10:45:38 -05005062 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005063 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005064 }
5065
Chris Mason39279cc2007-06-12 06:35:45 -04005066 return err;
5067}
Chris Mason61295eb2008-01-14 16:24:38 -05005068
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005069/*
5070 * While truncating the inode pages during eviction, we get the VFS calling
5071 * btrfs_invalidatepage() against each page of the inode. This is slow because
5072 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5073 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5074 * extent_state structures over and over, wasting lots of time.
5075 *
5076 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5077 * those expensive operations on a per page basis and do only the ordered io
5078 * finishing, while we release here the extent_map and extent_state structures,
5079 * without the excessive merging and splitting.
5080 */
5081static void evict_inode_truncate_pages(struct inode *inode)
5082{
5083 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5084 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5085 struct rb_node *node;
5086
5087 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005088 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005089
5090 write_lock(&map_tree->lock);
5091 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5092 struct extent_map *em;
5093
5094 node = rb_first(&map_tree->map);
5095 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005096 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5097 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005098 remove_extent_mapping(map_tree, em);
5099 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005100 if (need_resched()) {
5101 write_unlock(&map_tree->lock);
5102 cond_resched();
5103 write_lock(&map_tree->lock);
5104 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005105 }
5106 write_unlock(&map_tree->lock);
5107
Filipe Manana6ca07092015-05-26 00:55:42 +01005108 /*
5109 * Keep looping until we have no more ranges in the io tree.
5110 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005111 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5112 * still in progress (unlocked the pages in the bio but did not yet
5113 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005114 * ranges can still be locked and eviction started because before
5115 * submitting those bios, which are executed by a separate task (work
5116 * queue kthread), inode references (inode->i_count) were not taken
5117 * (which would be dropped in the end io callback of each bio).
5118 * Therefore here we effectively end up waiting for those bios and
5119 * anyone else holding locked ranges without having bumped the inode's
5120 * reference count - if we don't do it, when they access the inode's
5121 * io_tree to unlock a range it may be too late, leading to an
5122 * use-after-free issue.
5123 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005124 spin_lock(&io_tree->lock);
5125 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5126 struct extent_state *state;
5127 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005128 u64 start;
5129 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005130
5131 node = rb_first(&io_tree->state);
5132 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005133 start = state->start;
5134 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005135 spin_unlock(&io_tree->lock);
5136
David Sterbaff13db42015-12-03 14:30:40 +01005137 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005138
5139 /*
5140 * If still has DELALLOC flag, the extent didn't reach disk,
5141 * and its reserved space won't be freed by delayed_ref.
5142 * So we need to free its reserved space here.
5143 * (Refer to comment in btrfs_invalidatepage, case 2)
5144 *
5145 * Note, end is the bytenr of last byte, so we need + 1 here.
5146 */
5147 if (state->state & EXTENT_DELALLOC)
5148 btrfs_qgroup_free_data(inode, start, end - start + 1);
5149
Filipe Manana6ca07092015-05-26 00:55:42 +01005150 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005151 EXTENT_LOCKED | EXTENT_DIRTY |
5152 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5153 EXTENT_DEFRAG, 1, 1,
5154 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005155
Filipe Manana7064dd52014-08-08 02:47:05 +01005156 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005157 spin_lock(&io_tree->lock);
5158 }
5159 spin_unlock(&io_tree->lock);
5160}
5161
Al Virobd555972010-06-07 11:35:40 -04005162void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005163{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005164 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005165 struct btrfs_trans_handle *trans;
5166 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005167 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005168 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005169 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005170 int ret;
5171
liubo1abe9b82011-03-24 11:18:59 +00005172 trace_btrfs_inode_evict(inode);
5173
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005174 if (!root) {
5175 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5176 return;
5177 }
5178
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005179 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005180
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005181 evict_inode_truncate_pages(inode);
5182
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005183 if (inode->i_nlink &&
5184 ((btrfs_root_refs(&root->root_item) != 0 &&
5185 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5186 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005187 goto no_delete;
5188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005190 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005191 goto no_delete;
5192 }
Al Virobd555972010-06-07 11:35:40 -04005193 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005194 if (!special_file(inode->i_mode))
5195 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005196
Miao Xief6124962014-09-12 18:44:04 +08005197 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005199 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005200 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005201 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005202 goto no_delete;
5203 }
5204
Yan, Zheng76dda932009-09-21 16:00:26 -04005205 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005206 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5207 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005208 goto no_delete;
5209 }
5210
Nikolay Borisovaa790212017-01-10 20:35:40 +02005211 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005212 if (ret) {
5213 btrfs_orphan_del(NULL, inode);
5214 goto no_delete;
5215 }
5216
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005217 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005218 if (!rsv) {
5219 btrfs_orphan_del(NULL, inode);
5220 goto no_delete;
5221 }
Josef Bacik4a338542011-08-29 11:01:31 -04005222 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005223 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005224 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005225
Chris Masondbe674a2008-07-17 12:54:05 -04005226 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005227
Josef Bacik4289a662011-08-05 13:22:24 -04005228 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005229 * This is a bit simpler than btrfs_truncate since we've already
5230 * reserved our space for our orphan item in the unlink, so we just
5231 * need to reserve some slack space in case we add bytes and update
5232 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005233 */
Yan, Zheng80825102009-11-12 09:35:36 +00005234 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005235 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5236 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005237
Josef Bacik726c35f2011-09-26 15:46:06 -04005238 /*
5239 * Try and steal from the global reserve since we will
5240 * likely not use this space anyway, we want to try as
5241 * hard as possible to get this to work.
5242 */
5243 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005244 steal_from_global++;
5245 else
5246 steal_from_global = 0;
5247 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005248
Josef Bacik3bce8762015-02-24 12:35:51 -08005249 /*
5250 * steal_from_global == 0: we reserved stuff, hooray!
5251 * steal_from_global == 1: we didn't reserve stuff, boo!
5252 * steal_from_global == 2: we've committed, still not a lot of
5253 * room but maybe we'll have room in the global reserve this
5254 * time.
5255 * steal_from_global == 3: abandon all hope!
5256 */
5257 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005258 btrfs_warn(fs_info,
5259 "Could not get space for a delete, will truncate on mount %d",
5260 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005261 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005262 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005263 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005264 }
5265
Miao Xie0e8c36a2012-12-19 06:59:51 +00005266 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005267 if (IS_ERR(trans)) {
5268 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005269 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005270 goto no_delete;
5271 }
5272
Josef Bacik3bce8762015-02-24 12:35:51 -08005273 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005274 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005275 * sure there is room to do it, if not we need to commit and try
5276 * again.
5277 */
5278 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005279 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005280 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005281 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005282 else
5283 ret = -ENOSPC;
5284 }
5285
5286 /*
5287 * Couldn't steal from the global reserve, we have too much
5288 * pending stuff built up, commit the transaction and try it
5289 * again.
5290 */
5291 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005292 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 if (ret) {
5294 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005295 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005296 goto no_delete;
5297 }
5298 continue;
5299 } else {
5300 steal_from_global = 0;
5301 }
5302
Josef Bacik4289a662011-08-05 13:22:24 -04005303 trans->block_rsv = rsv;
5304
Yan, Zhengd68fc572010-05-16 10:49:58 -04005305 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005306 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005307 break;
5308
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005309 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005310 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005311 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005312 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005313 }
5314
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005315 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005316
Josef Bacik4ef31a42013-08-13 14:10:08 -04005317 /*
5318 * Errors here aren't a big deal, it just means we leave orphan items
5319 * in the tree. They will be cleaned up on the next mount.
5320 */
Yan, Zheng80825102009-11-12 09:35:36 +00005321 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005322 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005323 btrfs_orphan_del(trans, inode);
5324 } else {
5325 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005326 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005327
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 trans->block_rsv = &fs_info->trans_block_rsv;
5329 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005330 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005331 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005332
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005333 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005334 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005335no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005336 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005337 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005338}
5339
5340/*
5341 * this returns the key found in the dir entry in the location pointer.
5342 * If no dir entries were found, location->objectid is 0.
5343 */
5344static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5345 struct btrfs_key *location)
5346{
5347 const char *name = dentry->d_name.name;
5348 int namelen = dentry->d_name.len;
5349 struct btrfs_dir_item *di;
5350 struct btrfs_path *path;
5351 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005352 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005353
5354 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005355 if (!path)
5356 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005357
David Sterbaf85b7372017-01-20 14:54:07 +01005358 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5359 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005360 if (IS_ERR(di))
5361 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005362
David Sterbac7040052011-04-19 18:00:01 +02005363 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005364 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005365
Chris Mason5f39d392007-10-15 16:14:19 -04005366 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005367out:
Chris Mason39279cc2007-06-12 06:35:45 -04005368 btrfs_free_path(path);
5369 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005370out_err:
5371 location->objectid = 0;
5372 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005373}
5374
5375/*
5376 * when we hit a tree root in a directory, the btrfs part of the inode
5377 * needs to be changed to reflect the root directory of the tree root. This
5378 * is kind of like crossing a mount point.
5379 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005380static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005381 struct inode *dir,
5382 struct dentry *dentry,
5383 struct btrfs_key *location,
5384 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005385{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 struct btrfs_path *path;
5387 struct btrfs_root *new_root;
5388 struct btrfs_root_ref *ref;
5389 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005390 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391 int ret;
5392 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005393
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 path = btrfs_alloc_path();
5395 if (!path) {
5396 err = -ENOMEM;
5397 goto out;
5398 }
Chris Mason39279cc2007-06-12 06:35:45 -04005399
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005400 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005401 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5402 key.type = BTRFS_ROOT_REF_KEY;
5403 key.offset = location->objectid;
5404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005405 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005406 if (ret) {
5407 if (ret < 0)
5408 err = ret;
5409 goto out;
5410 }
Chris Mason39279cc2007-06-12 06:35:45 -04005411
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005412 leaf = path->nodes[0];
5413 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005414 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005415 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5416 goto out;
5417
5418 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5419 (unsigned long)(ref + 1),
5420 dentry->d_name.len);
5421 if (ret)
5422 goto out;
5423
David Sterbab3b4aa72011-04-21 01:20:15 +02005424 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005426 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427 if (IS_ERR(new_root)) {
5428 err = PTR_ERR(new_root);
5429 goto out;
5430 }
5431
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005432 *sub_root = new_root;
5433 location->objectid = btrfs_root_dirid(&new_root->root_item);
5434 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005435 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005436 err = 0;
5437out:
5438 btrfs_free_path(path);
5439 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005440}
5441
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442static void inode_tree_add(struct inode *inode)
5443{
5444 struct btrfs_root *root = BTRFS_I(inode)->root;
5445 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005446 struct rb_node **p;
5447 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005448 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005449 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450
Al Viro1d3382cb2010-10-23 15:19:20 -04005451 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005452 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005453 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005455 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005456 while (*p) {
5457 parent = *p;
5458 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5459
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005460 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005461 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005462 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005463 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005464 else {
5465 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005466 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005467 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005468 RB_CLEAR_NODE(parent);
5469 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005470 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471 }
5472 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005473 rb_link_node(new, parent, p);
5474 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 spin_unlock(&root->inode_lock);
5476}
5477
5478static void inode_tree_del(struct inode *inode)
5479{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005484 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005488 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005490 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005492 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005493 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005494 spin_lock(&root->inode_lock);
5495 empty = RB_EMPTY_ROOT(&root->inode_tree);
5496 spin_unlock(&root->inode_lock);
5497 if (empty)
5498 btrfs_add_dead_root(root);
5499 }
5500}
5501
Jeff Mahoney143bede2012-03-01 14:56:26 +01005502void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005503{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005504 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005505 struct rb_node *node;
5506 struct rb_node *prev;
5507 struct btrfs_inode *entry;
5508 struct inode *inode;
5509 u64 objectid = 0;
5510
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005511 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005512 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005513
5514 spin_lock(&root->inode_lock);
5515again:
5516 node = root->inode_tree.rb_node;
5517 prev = NULL;
5518 while (node) {
5519 prev = node;
5520 entry = rb_entry(node, struct btrfs_inode, rb_node);
5521
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005522 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005523 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005524 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005525 node = node->rb_right;
5526 else
5527 break;
5528 }
5529 if (!node) {
5530 while (prev) {
5531 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005532 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005533 node = prev;
5534 break;
5535 }
5536 prev = rb_next(prev);
5537 }
5538 }
5539 while (node) {
5540 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005541 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005542 inode = igrab(&entry->vfs_inode);
5543 if (inode) {
5544 spin_unlock(&root->inode_lock);
5545 if (atomic_read(&inode->i_count) > 1)
5546 d_prune_aliases(inode);
5547 /*
Al Viro45321ac2010-06-07 13:43:19 -04005548 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005549 * the inode cache when its usage count
5550 * hits zero.
5551 */
5552 iput(inode);
5553 cond_resched();
5554 spin_lock(&root->inode_lock);
5555 goto again;
5556 }
5557
5558 if (cond_resched_lock(&root->inode_lock))
5559 goto again;
5560
5561 node = rb_next(node);
5562 }
5563 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564}
5565
Chris Masone02119d2008-09-05 16:13:11 -04005566static int btrfs_init_locked_inode(struct inode *inode, void *p)
5567{
5568 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005569 inode->i_ino = args->location->objectid;
5570 memcpy(&BTRFS_I(inode)->location, args->location,
5571 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005572 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005573 return 0;
5574}
5575
5576static int btrfs_find_actor(struct inode *inode, void *opaque)
5577{
5578 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005579 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005580 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005581}
5582
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005584 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005586{
5587 struct inode *inode;
5588 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005589 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005590
Chris Mason90d3e592014-01-09 17:28:00 -08005591 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005592 args.root = root;
5593
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005594 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005595 btrfs_init_locked_inode,
5596 (void *)&args);
5597 return inode;
5598}
5599
Balaji Rao1a54ef82008-07-21 02:01:04 +05305600/* Get an inode object given its location and corresponding root.
5601 * Returns in *is_new if the inode was read from disk
5602 */
5603struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005604 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305605{
5606 struct inode *inode;
5607
Chris Mason90d3e592014-01-09 17:28:00 -08005608 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305609 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305611
5612 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005613 int ret;
5614
5615 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005616 if (!is_bad_inode(inode)) {
5617 inode_tree_add(inode);
5618 unlock_new_inode(inode);
5619 if (new)
5620 *new = 1;
5621 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005622 unlock_new_inode(inode);
5623 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005624 ASSERT(ret < 0);
5625 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005626 }
5627 }
5628
Balaji Rao1a54ef82008-07-21 02:01:04 +05305629 return inode;
5630}
5631
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632static struct inode *new_simple_dir(struct super_block *s,
5633 struct btrfs_key *key,
5634 struct btrfs_root *root)
5635{
5636 struct inode *inode = new_inode(s);
5637
5638 if (!inode)
5639 return ERR_PTR(-ENOMEM);
5640
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 BTRFS_I(inode)->root = root;
5642 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005643 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644
5645 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005646 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005647 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 inode->i_fop = &simple_dir_operations;
5649 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005650 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305651 inode->i_atime = inode->i_mtime;
5652 inode->i_ctime = inode->i_mtime;
5653 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654
5655 return inode;
5656}
5657
Chris Mason3de45862008-11-17 21:02:50 -05005658struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005659{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005660 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005661 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005663 struct btrfs_root *sub_root = root;
5664 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005666 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005667
5668 if (dentry->d_name.len > BTRFS_NAME_LEN)
5669 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005670
Jeff Layton39e3c952012-11-28 11:30:53 -05005671 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005672 if (ret < 0)
5673 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005674
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005675 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005676 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677
5678 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005679 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005680 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005681 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005682
5683 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5684
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005685 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005686 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005687 &location, &sub_root);
5688 if (ret < 0) {
5689 if (ret != -ENOENT)
5690 inode = ERR_PTR(ret);
5691 else
5692 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5693 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005694 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005695 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005696 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005697
Julia Lawall34d19ba2011-01-24 19:55:19 +00005698 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005699 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005700 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005701 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005702 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005703 if (ret) {
5704 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005705 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005706 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005707 }
5708
Chris Mason3de45862008-11-17 21:02:50 -05005709 return inode;
5710}
5711
Nick Pigginfe15ce42011-01-07 17:49:23 +11005712static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005713{
5714 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005715 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005716
Li Zefan848cce02012-02-21 17:04:28 +08005717 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005718 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005719
Li Zefan848cce02012-02-21 17:04:28 +08005720 if (inode) {
5721 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005722 if (btrfs_root_refs(&root->root_item) == 0)
5723 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005724
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005725 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005726 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005727 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005728 return 0;
5729}
5730
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005731static void btrfs_dentry_release(struct dentry *dentry)
5732{
Daeseok Youn944a4512014-04-14 15:37:02 +09005733 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005734}
5735
Chris Mason3de45862008-11-17 21:02:50 -05005736static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005737 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005738{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005739 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005740
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005741 inode = btrfs_lookup_dentry(dir, dentry);
5742 if (IS_ERR(inode)) {
5743 if (PTR_ERR(inode) == -ENOENT)
5744 inode = NULL;
5745 else
5746 return ERR_CAST(inode);
5747 }
5748
Al Viro41d28bc2014-10-12 22:24:21 -04005749 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005750}
5751
Miao Xie16cdcec2011-04-22 18:12:22 +08005752unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005753 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5754};
5755
Al Viro9cdda8d2013-05-22 16:48:09 -04005756static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005757{
Al Viro9cdda8d2013-05-22 16:48:09 -04005758 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005760 struct btrfs_root *root = BTRFS_I(inode)->root;
5761 struct btrfs_item *item;
5762 struct btrfs_dir_item *di;
5763 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005764 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005766 struct list_head ins_list;
5767 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005769 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005770 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005771 unsigned char d_type;
5772 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005773 char tmp_name[32];
5774 char *name_ptr;
5775 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005776 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005777 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005778
Al Viro9cdda8d2013-05-22 16:48:09 -04005779 if (!dir_emit_dots(file, ctx))
5780 return 0;
5781
David Woodhouse49593bf2008-08-17 17:08:36 +01005782 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005783 if (!path)
5784 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005785
David Sterbae4058b52015-11-27 16:31:35 +01005786 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005787
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005788 INIT_LIST_HEAD(&ins_list);
5789 INIT_LIST_HEAD(&del_list);
5790 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005791
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005792 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005793 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005794 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005795
Chris Mason39279cc2007-06-12 06:35:45 -04005796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5797 if (ret < 0)
5798 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005799
5800 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005801 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005802 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005803 if (slot >= btrfs_header_nritems(leaf)) {
5804 ret = btrfs_next_leaf(root, path);
5805 if (ret < 0)
5806 goto err;
5807 else if (ret > 0)
5808 break;
5809 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005810 }
Chris Mason3de45862008-11-17 21:02:50 -05005811
Ross Kirkdd3cc162013-09-16 15:58:09 +01005812 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005813 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5814
5815 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005816 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005817 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005818 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005819 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005820 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005821 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005822 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005823
Al Viro9cdda8d2013-05-22 16:48:09 -04005824 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005825
Chris Mason39279cc2007-06-12 06:35:45 -04005826 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005827 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005828 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005829
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005830 name_len = btrfs_dir_name_len(leaf, di);
5831 if (name_len <= sizeof(tmp_name)) {
5832 name_ptr = tmp_name;
5833 } else {
5834 name_ptr = kmalloc(name_len, GFP_KERNEL);
5835 if (!name_ptr) {
5836 ret = -ENOMEM;
5837 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005838 }
Chris Mason39279cc2007-06-12 06:35:45 -04005839 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005840 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5841 name_len);
5842
5843 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5844 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5845
5846 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5847 d_type);
5848
5849 if (name_ptr != tmp_name)
5850 kfree(name_ptr);
5851
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005852 if (over)
5853 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005854 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005855next:
5856 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005858
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005859 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005860 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005861 goto nopos;
5862
Zach Browndb62efb2013-07-11 16:19:42 -07005863 /*
5864 * Stop new entries from being returned after we return the last
5865 * entry.
5866 *
5867 * New directory entries are assigned a strictly increasing
5868 * offset. This means that new entries created during readdir
5869 * are *guaranteed* to be seen in the future by that readdir.
5870 * This has broken buggy programs which operate on names as
5871 * they're returned by readdir. Until we re-use freed offsets
5872 * we have this hack to stop new entries from being returned
5873 * under the assumption that they'll never reach this huge
5874 * offset.
5875 *
5876 * This is being careful not to overflow 32bit loff_t unless the
5877 * last entry requires it because doing so has broken 32bit apps
5878 * in the past.
5879 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005880 if (ctx->pos >= INT_MAX)
5881 ctx->pos = LLONG_MAX;
5882 else
5883 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005884nopos:
5885 ret = 0;
5886err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005887 if (put)
5888 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005889 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005890 return ret;
5891}
5892
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005893int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005894{
5895 struct btrfs_root *root = BTRFS_I(inode)->root;
5896 struct btrfs_trans_handle *trans;
5897 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005898 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005899
Josef Bacik72ac3c02012-05-23 14:13:11 -04005900 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005901 return 0;
5902
Liu Bo83eea1f2012-07-10 05:28:39 -06005903 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005904 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005905
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005906 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005907 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005908 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005909 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005910 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005911 if (IS_ERR(trans))
5912 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005913 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005914 }
5915 return ret;
5916}
5917
5918/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005919 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005920 * inode changes. But, it is most likely to find the inode in cache.
5921 * FIXME, needs more benchmarking...there are no reasons other than performance
5922 * to keep or drop this code.
5923 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005924static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005925{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005927 struct btrfs_root *root = BTRFS_I(inode)->root;
5928 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005929 int ret;
5930
Josef Bacik72ac3c02012-05-23 14:13:11 -04005931 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005932 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005934 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005935 if (IS_ERR(trans))
5936 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005937
5938 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005939 if (ret && ret == -ENOSPC) {
5940 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005941 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005942 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005943 if (IS_ERR(trans))
5944 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005945
Chris Mason94b60442010-05-26 11:02:00 -04005946 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005947 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005948 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005949 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005950 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005951
5952 return ret;
5953}
5954
5955/*
5956 * This is a copy of file_update_time. We need this so we can return error on
5957 * ENOSPC for updating the inode in the case of file write and mmap writes.
5958 */
Josef Bacike41f9412012-03-26 09:46:47 -04005959static int btrfs_update_time(struct inode *inode, struct timespec *now,
5960 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005961{
Alexander Block2bc5565282012-06-15 09:49:33 +02005962 struct btrfs_root *root = BTRFS_I(inode)->root;
5963
5964 if (btrfs_root_readonly(root))
5965 return -EROFS;
5966
Josef Bacike41f9412012-03-26 09:46:47 -04005967 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005968 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005969 if (flags & S_CTIME)
5970 inode->i_ctime = *now;
5971 if (flags & S_MTIME)
5972 inode->i_mtime = *now;
5973 if (flags & S_ATIME)
5974 inode->i_atime = *now;
5975 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005976}
5977
Chris Masond352ac62008-09-29 15:18:18 -04005978/*
5979 * find the highest existing sequence number in a directory
5980 * and then set the in-memory index_cnt variable to reflect
5981 * free sequence numbers
5982 */
Josef Bacikaec74772008-07-24 12:12:38 -04005983static int btrfs_set_inode_index_count(struct inode *inode)
5984{
5985 struct btrfs_root *root = BTRFS_I(inode)->root;
5986 struct btrfs_key key, found_key;
5987 struct btrfs_path *path;
5988 struct extent_buffer *leaf;
5989 int ret;
5990
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005991 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +02005992 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005993 key.offset = (u64)-1;
5994
5995 path = btrfs_alloc_path();
5996 if (!path)
5997 return -ENOMEM;
5998
5999 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6000 if (ret < 0)
6001 goto out;
6002 /* FIXME: we should be able to handle this */
6003 if (ret == 0)
6004 goto out;
6005 ret = 0;
6006
6007 /*
6008 * MAGIC NUMBER EXPLANATION:
6009 * since we search a directory based on f_pos we have to start at 2
6010 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6011 * else has to start at 2
6012 */
6013 if (path->slots[0] == 0) {
6014 BTRFS_I(inode)->index_cnt = 2;
6015 goto out;
6016 }
6017
6018 path->slots[0]--;
6019
6020 leaf = path->nodes[0];
6021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6022
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006023 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
David Sterba962a2982014-06-04 18:41:45 +02006024 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006025 BTRFS_I(inode)->index_cnt = 2;
6026 goto out;
6027 }
6028
6029 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6030out:
6031 btrfs_free_path(path);
6032 return ret;
6033}
6034
Chris Masond352ac62008-09-29 15:18:18 -04006035/*
6036 * helper to find a free sequence number in a given directory. This current
6037 * code is very simple, later versions will do smarter things in the btree
6038 */
Chris Mason3de45862008-11-17 21:02:50 -05006039int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006040{
6041 int ret = 0;
6042
6043 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02006044 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
Miao Xie16cdcec2011-04-22 18:12:22 +08006045 if (ret) {
6046 ret = btrfs_set_inode_index_count(dir);
6047 if (ret)
6048 return ret;
6049 }
Josef Bacikaec74772008-07-24 12:12:38 -04006050 }
6051
Chris Mason00e4e6b2008-08-05 11:18:09 -04006052 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006053 BTRFS_I(dir)->index_cnt++;
6054
6055 return ret;
6056}
6057
Chris Masonb0d5d102014-09-08 13:08:51 -07006058static int btrfs_insert_inode_locked(struct inode *inode)
6059{
6060 struct btrfs_iget_args args;
6061 args.location = &BTRFS_I(inode)->location;
6062 args.root = BTRFS_I(inode)->root;
6063
6064 return insert_inode_locked4(inode,
6065 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6066 btrfs_find_actor, &args);
6067}
6068
Chris Mason39279cc2007-06-12 06:35:45 -04006069static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6070 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006071 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006072 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006073 u64 ref_objectid, u64 objectid,
6074 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006075{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006076 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006077 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006078 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006079 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006080 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006081 struct btrfs_inode_ref *ref;
6082 struct btrfs_key key[2];
6083 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006084 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006085 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006086 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006087
Chris Mason5f39d392007-10-15 16:14:19 -04006088 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006089 if (!path)
6090 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006092 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006093 if (!inode) {
6094 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006095 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006096 }
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Li Zefan581bb052011-04-20 10:06:11 +08006098 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006099 * O_TMPFILE, set link count to 0, so that after this point,
6100 * we fill in an inode item with the correct link count.
6101 */
6102 if (!name)
6103 set_nlink(inode, 0);
6104
6105 /*
Li Zefan581bb052011-04-20 10:06:11 +08006106 * we have to initialize this early, so we can reclaim the inode
6107 * number if we fail afterwards in this function.
6108 */
6109 inode->i_ino = objectid;
6110
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006111 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006112 trace_btrfs_inode_request(dir);
6113
Chris Mason3de45862008-11-17 21:02:50 -05006114 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006115 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006116 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006117 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006118 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006119 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006120 } else if (dir) {
6121 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006122 }
6123 /*
6124 * index_cnt is ignored for everything but a dir,
6125 * btrfs_get_inode_index_count has an explanation for the magic
6126 * number
6127 */
6128 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006129 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006130 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006131 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006132 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006133
Josef Bacik5dc562c2012-08-17 13:14:17 -04006134 /*
6135 * We could have gotten an inode number from somebody who was fsynced
6136 * and then removed in this same transaction, so let's just set full
6137 * sync since it will be a full sync anyway and this will blow away the
6138 * old info in the log.
6139 */
6140 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6141
Chris Mason9c583092008-01-29 15:15:18 -05006142 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006143 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006144 key[0].offset = 0;
6145
Chris Mason9c583092008-01-29 15:15:18 -05006146 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006147
6148 if (name) {
6149 /*
6150 * Start new inodes with an inode_ref. This is slightly more
6151 * efficient for small numbers of hard links since they will
6152 * be packed into one item. Extended refs will kick in if we
6153 * add more hard links than can fit in the ref item.
6154 */
6155 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006156 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006157 key[1].offset = ref_objectid;
6158
6159 sizes[1] = name_len + sizeof(*ref);
6160 }
Chris Mason9c583092008-01-29 15:15:18 -05006161
Chris Masonb0d5d102014-09-08 13:08:51 -07006162 location = &BTRFS_I(inode)->location;
6163 location->objectid = objectid;
6164 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006165 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006166
6167 ret = btrfs_insert_inode_locked(inode);
6168 if (ret < 0)
6169 goto fail;
6170
Chris Masonb9473432009-03-13 11:00:37 -04006171 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006172 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006173 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006174 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006175
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006176 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006177 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306178
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006179 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306180 inode->i_atime = inode->i_mtime;
6181 inode->i_ctime = inode->i_mtime;
6182 BTRFS_I(inode)->i_otime = inode->i_mtime;
6183
Chris Mason5f39d392007-10-15 16:14:19 -04006184 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6185 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006186 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006187 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006188 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006189
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006190 if (name) {
6191 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6192 struct btrfs_inode_ref);
6193 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6194 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6195 ptr = (unsigned long)(ref + 1);
6196 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6197 }
Chris Mason9c583092008-01-29 15:15:18 -05006198
Chris Mason5f39d392007-10-15 16:14:19 -04006199 btrfs_mark_buffer_dirty(path->nodes[0]);
6200 btrfs_free_path(path);
6201
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006202 btrfs_inherit_iflags(inode, dir);
6203
Al Viro569254b2011-07-24 17:08:40 -04006204 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006205 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006206 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006207 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006208 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6209 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006210 }
6211
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006212 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006213
6214 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006215 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006216
Alexander Block8ea05e32012-07-25 17:35:53 +02006217 btrfs_update_root_times(trans, root);
6218
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006219 ret = btrfs_inode_inherit_props(trans, inode, dir);
6220 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006221 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006222 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006223 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006224
Chris Mason39279cc2007-06-12 06:35:45 -04006225 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006226
6227fail_unlock:
6228 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006229fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006230 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006231 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006232 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006233 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006234 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006235}
6236
6237static inline u8 btrfs_inode_type(struct inode *inode)
6238{
6239 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6240}
6241
Chris Masond352ac62008-09-29 15:18:18 -04006242/*
6243 * utility function to add 'inode' into 'parent_inode' with
6244 * a give name and a given sequence number.
6245 * if 'add_backref' is true, also insert a backref from the
6246 * inode to the parent directory.
6247 */
Chris Masone02119d2008-09-05 16:13:11 -04006248int btrfs_add_link(struct btrfs_trans_handle *trans,
6249 struct inode *parent_inode, struct inode *inode,
6250 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006253 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006254 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006255 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006256 u64 ino = btrfs_ino(BTRFS_I(inode));
6257 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006258
Li Zefan33345d012011-04-20 10:31:50 +08006259 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006260 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6261 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006262 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006263 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006264 key.offset = 0;
6265 }
Chris Mason39279cc2007-06-12 06:35:45 -04006266
Li Zefan33345d012011-04-20 10:31:50 +08006267 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006268 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6269 root->root_key.objectid, parent_ino,
6270 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006271 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006272 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6273 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006274 }
6275
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006276 /* Nothing to clean up yet */
6277 if (ret)
6278 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006279
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6281 parent_inode, &key,
6282 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006283 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284 goto fail_dir_item;
6285 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289
6290 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6291 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006292 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006293 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006294 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006295 ret = btrfs_update_inode(trans, root, parent_inode);
6296 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006297 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006298 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006299
6300fail_dir_item:
6301 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6302 u64 local_index;
6303 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006304 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6305 root->root_key.objectid, parent_ino,
6306 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006307
6308 } else if (add_backref) {
6309 u64 local_index;
6310 int err;
6311
6312 err = btrfs_del_inode_ref(trans, root, name, name_len,
6313 ino, parent_ino, &local_index);
6314 }
6315 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006316}
6317
6318static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006319 struct inode *dir, struct dentry *dentry,
6320 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006321{
Josef Bacika1b075d2010-11-19 20:36:11 +00006322 int err = btrfs_add_link(trans, dir, inode,
6323 dentry->d_name.name, dentry->d_name.len,
6324 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006325 if (err > 0)
6326 err = -EEXIST;
6327 return err;
6328}
6329
Josef Bacik618e21d2007-07-11 10:18:17 -04006330static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006331 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006332{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006333 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006334 struct btrfs_trans_handle *trans;
6335 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006336 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006337 int err;
6338 int drop_inode = 0;
6339 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006340 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006341
Josef Bacik9ed74f22009-09-11 16:12:44 -04006342 /*
6343 * 2 for inode item and ref
6344 * 2 for dir items
6345 * 1 for xattr if selinux is on
6346 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006347 trans = btrfs_start_transaction(root, 5);
6348 if (IS_ERR(trans))
6349 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006350
Li Zefan581bb052011-04-20 10:06:11 +08006351 err = btrfs_find_free_ino(root, &objectid);
6352 if (err)
6353 goto out_unlock;
6354
Josef Bacikaec74772008-07-24 12:12:38 -04006355 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006356 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6357 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006358 if (IS_ERR(inode)) {
6359 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006360 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006361 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006362
Casey Schauflerad19db72011-12-15 10:09:07 -05006363 /*
6364 * If the active LSM wants to access the inode during
6365 * d_instantiate it needs these. Smack checks to see
6366 * if the filesystem supports xattrs by looking at the
6367 * ops vector.
6368 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006369 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006370 init_special_inode(inode, inode->i_mode, rdev);
6371
6372 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006373 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006374 goto out_unlock_inode;
6375
6376 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6377 if (err) {
6378 goto out_unlock_inode;
6379 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006380 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006381 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006382 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006383 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006384
Josef Bacik618e21d2007-07-11 10:18:17 -04006385out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006386 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006387 btrfs_balance_delayed_items(fs_info);
6388 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006389 if (drop_inode) {
6390 inode_dec_link_count(inode);
6391 iput(inode);
6392 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006393 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006394
6395out_unlock_inode:
6396 drop_inode = 1;
6397 unlock_new_inode(inode);
6398 goto out_unlock;
6399
Josef Bacik618e21d2007-07-11 10:18:17 -04006400}
6401
Chris Mason39279cc2007-06-12 06:35:45 -04006402static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006403 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006404{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006405 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006406 struct btrfs_trans_handle *trans;
6407 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006408 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006409 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006410 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006411 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006412 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006413
Josef Bacik9ed74f22009-09-11 16:12:44 -04006414 /*
6415 * 2 for inode item and ref
6416 * 2 for dir items
6417 * 1 for xattr if selinux is on
6418 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006419 trans = btrfs_start_transaction(root, 5);
6420 if (IS_ERR(trans))
6421 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006422
Li Zefan581bb052011-04-20 10:06:11 +08006423 err = btrfs_find_free_ino(root, &objectid);
6424 if (err)
6425 goto out_unlock;
6426
Josef Bacikaec74772008-07-24 12:12:38 -04006427 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006428 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6429 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006430 if (IS_ERR(inode)) {
6431 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006432 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006433 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006434 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006435 /*
6436 * If the active LSM wants to access the inode during
6437 * d_instantiate it needs these. Smack checks to see
6438 * if the filesystem supports xattrs by looking at the
6439 * ops vector.
6440 */
6441 inode->i_fop = &btrfs_file_operations;
6442 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006444
6445 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6446 if (err)
6447 goto out_unlock_inode;
6448
6449 err = btrfs_update_inode(trans, root, inode);
6450 if (err)
6451 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006452
Josef Bacika1b075d2010-11-19 20:36:11 +00006453 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006454 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006458 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006459 d_instantiate(dentry, inode);
6460
Chris Mason39279cc2007-06-12 06:35:45 -04006461out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006462 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006464 inode_dec_link_count(inode);
6465 iput(inode);
6466 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 btrfs_balance_delayed_items(fs_info);
6468 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006469 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006470
6471out_unlock_inode:
6472 unlock_new_inode(inode);
6473 goto out_unlock;
6474
Chris Mason39279cc2007-06-12 06:35:45 -04006475}
6476
6477static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6478 struct dentry *dentry)
6479{
Filipe Manana271dba42016-01-05 16:24:05 +00006480 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006482 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006484 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006485 int err;
6486 int drop_inode = 0;
6487
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006488 /* do not allow sys_link's with other subvols of the same device */
6489 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006490 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006491
Mark Fashehf1863732012-08-08 11:32:27 -07006492 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006493 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494
Chris Mason3de45862008-11-17 21:02:50 -05006495 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006496 if (err)
6497 goto fail;
6498
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006500 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006501 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006502 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006503 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006504 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 if (IS_ERR(trans)) {
6506 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006507 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006508 goto fail;
6509 }
Chris Mason5f39d392007-10-15 16:14:19 -04006510
Miao Xie67de1172013-12-26 13:07:06 +08006511 /* There are several dir indexes for this inode, clear the cache. */
6512 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006513 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006514 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006515 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006516 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006517 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006518
Josef Bacika1b075d2010-11-19 20:36:11 +00006519 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006520
Yan, Zhenga5719522009-09-24 09:17:31 -04006521 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006522 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006523 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006524 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006525 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006526 if (err)
6527 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006528 if (inode->i_nlink == 1) {
6529 /*
6530 * If new hard link count is 1, it's a file created
6531 * with open(2) O_TMPFILE flag.
6532 */
6533 err = btrfs_orphan_del(trans, inode);
6534 if (err)
6535 goto fail;
6536 }
Al Viro08c422c2011-12-23 07:58:13 -05006537 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006538 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006539 }
Chris Mason39279cc2007-06-12 06:35:45 -04006540
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006541 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006542fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006543 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006544 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006545 if (drop_inode) {
6546 inode_dec_link_count(inode);
6547 iput(inode);
6548 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006549 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006550 return err;
6551}
6552
Al Viro18bb1db2011-07-26 01:41:39 -04006553static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006554{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006555 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006556 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006557 struct btrfs_trans_handle *trans;
6558 struct btrfs_root *root = BTRFS_I(dir)->root;
6559 int err = 0;
6560 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006561 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006562 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006563
Josef Bacik9ed74f22009-09-11 16:12:44 -04006564 /*
6565 * 2 items for inode and ref
6566 * 2 items for dir items
6567 * 1 for xattr if selinux is on
6568 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006569 trans = btrfs_start_transaction(root, 5);
6570 if (IS_ERR(trans))
6571 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006572
Li Zefan581bb052011-04-20 10:06:11 +08006573 err = btrfs_find_free_ino(root, &objectid);
6574 if (err)
6575 goto out_fail;
6576
Josef Bacikaec74772008-07-24 12:12:38 -04006577 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006578 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6579 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006580 if (IS_ERR(inode)) {
6581 err = PTR_ERR(inode);
6582 goto out_fail;
6583 }
Chris Mason5f39d392007-10-15 16:14:19 -04006584
Chris Mason39279cc2007-06-12 06:35:45 -04006585 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006586 /* these must be set before we unlock the inode */
6587 inode->i_op = &btrfs_dir_inode_operations;
6588 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006589
Eric Paris2a7dba32011-02-01 11:05:39 -05006590 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006591 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006593
Chris Masondbe674a2008-07-17 12:54:05 -04006594 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006595 err = btrfs_update_inode(trans, root, inode);
6596 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006597 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006598
Josef Bacika1b075d2010-11-19 20:36:11 +00006599 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6600 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006602 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006603
Chris Mason39279cc2007-06-12 06:35:45 -04006604 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006605 /*
6606 * mkdir is special. We're unlocking after we call d_instantiate
6607 * to avoid a race with nfsd calling d_instantiate.
6608 */
6609 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006611
6612out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006613 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006614 if (drop_on_err) {
6615 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006617 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006618 btrfs_balance_delayed_items(fs_info);
6619 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006621
6622out_fail_inode:
6623 unlock_new_inode(inode);
6624 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006625}
6626
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006627/* Find next extent map of a given extent map, caller needs to ensure locks */
6628static struct extent_map *next_extent_map(struct extent_map *em)
6629{
6630 struct rb_node *next;
6631
6632 next = rb_next(&em->rb_node);
6633 if (!next)
6634 return NULL;
6635 return container_of(next, struct extent_map, rb_node);
6636}
6637
6638static struct extent_map *prev_extent_map(struct extent_map *em)
6639{
6640 struct rb_node *prev;
6641
6642 prev = rb_prev(&em->rb_node);
6643 if (!prev)
6644 return NULL;
6645 return container_of(prev, struct extent_map, rb_node);
6646}
6647
Chris Masond352ac62008-09-29 15:18:18 -04006648/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006649 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006650 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006651 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006652 */
Chris Mason3b951512008-04-17 11:29:12 -04006653static int merge_extent_mapping(struct extent_map_tree *em_tree,
6654 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006655 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006656 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006657{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006658 struct extent_map *prev;
6659 struct extent_map *next;
6660 u64 start;
6661 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006662 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006663
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006665
6666 if (existing->start > map_start) {
6667 next = existing;
6668 prev = prev_extent_map(next);
6669 } else {
6670 prev = existing;
6671 next = next_extent_map(prev);
6672 }
6673
6674 start = prev ? extent_map_end(prev) : em->start;
6675 start = max_t(u64, start, em->start);
6676 end = next ? next->start : extent_map_end(em);
6677 end = min_t(u64, end, extent_map_end(em));
6678 start_diff = start - em->start;
6679 em->start = start;
6680 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006681 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6682 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006683 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006684 em->block_len -= start_diff;
6685 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006686 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006687}
6688
Chris Masonc8b97812008-10-29 14:49:59 -04006689static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006690 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006691 size_t pg_offset, u64 extent_offset,
6692 struct btrfs_file_extent_item *item)
6693{
6694 int ret;
6695 struct extent_buffer *leaf = path->nodes[0];
6696 char *tmp;
6697 size_t max_size;
6698 unsigned long inline_size;
6699 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006700 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006701
6702 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006703 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006704 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6705 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006706 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006707 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006708 if (!tmp)
6709 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006710 ptr = btrfs_file_extent_inline_start(item);
6711
6712 read_extent_buffer(leaf, tmp, ptr, inline_size);
6713
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006714 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006715 ret = btrfs_decompress(compress_type, tmp, page,
6716 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006717 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006718 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006719}
6720
Chris Masond352ac62008-09-29 15:18:18 -04006721/*
6722 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006723 * the ugly parts come from merging extents from the disk with the in-ram
6724 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006725 * where the in-ram extents might be locked pending data=ordered completion.
6726 *
6727 * This also copies inline extents directly into the page.
6728 */
Chris Masond3977122009-01-05 21:25:51 -05006729
Chris Masona52d9a82007-08-27 16:49:44 -04006730struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006731 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006732 int create)
6733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006735 int ret;
6736 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006737 u64 extent_start = 0;
6738 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006739 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006740 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006741 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006742 struct btrfs_root *root = BTRFS_I(inode)->root;
6743 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006744 struct extent_buffer *leaf;
6745 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct extent_map *em = NULL;
6747 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006748 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006749 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006750 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006751
Chris Masona52d9a82007-08-27 16:49:44 -04006752again:
Chris Mason890871b2009-09-02 16:24:52 -04006753 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006754 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006755 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006756 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006757 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006758
Chris Masona52d9a82007-08-27 16:49:44 -04006759 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006760 if (em->start > start || em->start + em->len <= start)
6761 free_extent_map(em);
6762 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006763 free_extent_map(em);
6764 else
6765 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006766 }
David Sterba172ddd62011-04-21 00:48:27 +02006767 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006768 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006769 err = -ENOMEM;
6770 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006771 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006772 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006773 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006774 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006775 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006776 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006777
6778 if (!path) {
6779 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006780 if (!path) {
6781 err = -ENOMEM;
6782 goto out;
6783 }
6784 /*
6785 * Chances are we'll be called again, so go ahead and do
6786 * readahead
6787 */
David Sterbae4058b52015-11-27 16:31:35 +01006788 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006789 }
6790
Chris Mason179e29e2007-11-01 11:28:41 -04006791 ret = btrfs_lookup_file_extent(trans, root, path,
6792 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006793 if (ret < 0) {
6794 err = ret;
6795 goto out;
6796 }
6797
6798 if (ret != 0) {
6799 if (path->slots[0] == 0)
6800 goto not_found;
6801 path->slots[0]--;
6802 }
6803
Chris Mason5f39d392007-10-15 16:14:19 -04006804 leaf = path->nodes[0];
6805 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006806 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006807 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006808 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006809 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006810 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006811 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006812 /*
6813 * If we backup past the first extent we want to move forward
6814 * and see if there is an extent in front of us, otherwise we'll
6815 * say there is a hole for our whole search range which can
6816 * cause problems.
6817 */
6818 extent_end = start;
6819 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006820 }
6821
Chris Mason5f39d392007-10-15 16:14:19 -04006822 found_type = btrfs_file_extent_type(leaf, item);
6823 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006824 if (found_type == BTRFS_FILE_EXTENT_REG ||
6825 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006826 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006827 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006828 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6829 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006830 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006831 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006832 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006833 }
Josef Bacik25a50342013-10-14 12:08:38 -04006834next:
Yan Zheng9036c102008-10-30 14:19:41 -04006835 if (start >= extent_end) {
6836 path->slots[0]++;
6837 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6838 ret = btrfs_next_leaf(root, path);
6839 if (ret < 0) {
6840 err = ret;
6841 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006842 }
Yan Zheng9036c102008-10-30 14:19:41 -04006843 if (ret > 0)
6844 goto not_found;
6845 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006846 }
Yan Zheng9036c102008-10-30 14:19:41 -04006847 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6848 if (found_key.objectid != objectid ||
6849 found_key.type != BTRFS_EXTENT_DATA_KEY)
6850 goto not_found;
6851 if (start + len <= found_key.offset)
6852 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006853 if (start > found_key.offset)
6854 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006855 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006856 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006857 em->len = found_key.offset - start;
6858 goto not_found_em;
6859 }
6860
Filipe Manana7ffbb592014-06-09 03:48:05 +01006861 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6862
Yan Zhengd899e052008-10-30 14:25:28 -04006863 if (found_type == BTRFS_FILE_EXTENT_REG ||
6864 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006865 goto insert;
6866 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006867 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006868 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006869 size_t size;
6870 size_t extent_offset;
6871 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006872
Filipe Manana7ffbb592014-06-09 03:48:05 +01006873 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006874 goto out;
Yan689f9342007-10-29 11:41:07 -04006875
Chris Mason514ac8a2014-01-03 21:07:00 -08006876 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006877 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006878 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6879 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006880 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006881 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006882 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006883 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006884 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006885 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006886 if (btrfs_file_extent_compression(leaf, item) !=
6887 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006888 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006889 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006890 if (ret) {
6891 err = ret;
6892 goto out;
6893 }
Chris Masonc8b97812008-10-29 14:49:59 -04006894 } else {
6895 map = kmap(page);
6896 read_extent_buffer(leaf, map + pg_offset, ptr,
6897 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006898 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006899 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006900 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006901 copy_size);
6902 }
Chris Masonc8b97812008-10-29 14:49:59 -04006903 kunmap(page);
6904 }
Chris Mason179e29e2007-11-01 11:28:41 -04006905 flush_dcache_page(page);
6906 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006907 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006908 if (!trans) {
6909 kunmap(page);
6910 free_extent_map(em);
6911 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006912
David Sterbab3b4aa72011-04-21 01:20:15 +02006913 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006914 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006915
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006916 if (IS_ERR(trans))
6917 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006918 goto again;
6919 }
Chris Masonc8b97812008-10-29 14:49:59 -04006920 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006921 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006922 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006923 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006924 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006925 }
Chris Masond1310b22008-01-24 16:13:08 -05006926 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006927 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006928 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006929 }
6930not_found:
6931 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006932 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006933 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006934not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006935 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006936 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006937insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006938 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006939 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006940 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006941 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6942 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006943 err = -EIO;
6944 goto out;
6945 }
Chris Masond1310b22008-01-24 16:13:08 -05006946
6947 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006948 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006949 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006950 /* it is possible that someone inserted the extent into the tree
6951 * while we had the lock dropped. It is also possible that
6952 * an overlapping map exists in the tree
6953 */
Chris Masona52d9a82007-08-27 16:49:44 -04006954 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006955 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006956
6957 ret = 0;
6958
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006959 existing = search_extent_mapping(em_tree, start, len);
6960 /*
6961 * existing will always be non-NULL, since there must be
6962 * extent causing the -EEXIST.
6963 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006964 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006965 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006966 em->block_start == existing->block_start) {
6967 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006968 * The existing extent map already encompasses the
6969 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006970 */
6971 free_extent_map(em);
6972 em = existing;
6973 err = 0;
6974
6975 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006976 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006977 /*
6978 * The existing extent map is the one nearest to
6979 * the [start, start + len) range which overlaps
6980 */
6981 err = merge_extent_mapping(em_tree, existing,
6982 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006983 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006984 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006985 free_extent_map(em);
6986 em = NULL;
6987 }
6988 } else {
6989 free_extent_map(em);
6990 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006991 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006992 }
Chris Masona52d9a82007-08-27 16:49:44 -04006993 }
Chris Mason890871b2009-09-02 16:24:52 -04006994 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006995out:
liubo1abe9b82011-03-24 11:18:59 +00006996
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006997 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00006998
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006999 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007000 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007001 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007002 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007003 err = ret;
7004 }
Chris Masona52d9a82007-08-27 16:49:44 -04007005 if (err) {
7006 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007007 return ERR_PTR(err);
7008 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007009 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007010 return em;
7011}
7012
Chris Masonec29ed52011-02-23 16:23:20 -05007013struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7014 size_t pg_offset, u64 start, u64 len,
7015 int create)
7016{
7017 struct extent_map *em;
7018 struct extent_map *hole_em = NULL;
7019 u64 range_start = start;
7020 u64 end;
7021 u64 found;
7022 u64 found_end;
7023 int err = 0;
7024
7025 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7026 if (IS_ERR(em))
7027 return em;
7028 if (em) {
7029 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007030 * if our em maps to
7031 * - a hole or
7032 * - a pre-alloc extent,
7033 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007034 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007035 if (em->block_start != EXTENT_MAP_HOLE &&
7036 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007037 return em;
7038 else
7039 hole_em = em;
7040 }
7041
7042 /* check to see if we've wrapped (len == -1 or similar) */
7043 end = start + len;
7044 if (end < start)
7045 end = (u64)-1;
7046 else
7047 end -= 1;
7048
7049 em = NULL;
7050
7051 /* ok, we didn't find anything, lets look for delalloc */
7052 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7053 end, len, EXTENT_DELALLOC, 1);
7054 found_end = range_start + found;
7055 if (found_end < range_start)
7056 found_end = (u64)-1;
7057
7058 /*
7059 * we didn't find anything useful, return
7060 * the original results from get_extent()
7061 */
7062 if (range_start > end || found_end <= start) {
7063 em = hole_em;
7064 hole_em = NULL;
7065 goto out;
7066 }
7067
7068 /* adjust the range_start to make sure it doesn't
7069 * go backwards from the start they passed in
7070 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307071 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007072 found = found_end - range_start;
7073
7074 if (found > 0) {
7075 u64 hole_start = start;
7076 u64 hole_len = len;
7077
David Sterba172ddd62011-04-21 00:48:27 +02007078 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007079 if (!em) {
7080 err = -ENOMEM;
7081 goto out;
7082 }
7083 /*
7084 * when btrfs_get_extent can't find anything it
7085 * returns one huge hole
7086 *
7087 * make sure what it found really fits our range, and
7088 * adjust to make sure it is based on the start from
7089 * the caller
7090 */
7091 if (hole_em) {
7092 u64 calc_end = extent_map_end(hole_em);
7093
7094 if (calc_end <= start || (hole_em->start > end)) {
7095 free_extent_map(hole_em);
7096 hole_em = NULL;
7097 } else {
7098 hole_start = max(hole_em->start, start);
7099 hole_len = calc_end - hole_start;
7100 }
7101 }
7102 em->bdev = NULL;
7103 if (hole_em && range_start > hole_start) {
7104 /* our hole starts before our delalloc, so we
7105 * have to return just the parts of the hole
7106 * that go until the delalloc starts
7107 */
7108 em->len = min(hole_len,
7109 range_start - hole_start);
7110 em->start = hole_start;
7111 em->orig_start = hole_start;
7112 /*
7113 * don't adjust block start at all,
7114 * it is fixed at EXTENT_MAP_HOLE
7115 */
7116 em->block_start = hole_em->block_start;
7117 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007118 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7119 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007120 } else {
7121 em->start = range_start;
7122 em->len = found;
7123 em->orig_start = range_start;
7124 em->block_start = EXTENT_MAP_DELALLOC;
7125 em->block_len = found;
7126 }
7127 } else if (hole_em) {
7128 return hole_em;
7129 }
7130out:
7131
7132 free_extent_map(hole_em);
7133 if (err) {
7134 free_extent_map(em);
7135 return ERR_PTR(err);
7136 }
7137 return em;
7138}
7139
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007140static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7141 const u64 start,
7142 const u64 len,
7143 const u64 orig_start,
7144 const u64 block_start,
7145 const u64 block_len,
7146 const u64 orig_block_len,
7147 const u64 ram_bytes,
7148 const int type)
7149{
7150 struct extent_map *em = NULL;
7151 int ret;
7152
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007153 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007154 em = create_io_em(inode, start, len, orig_start,
7155 block_start, block_len, orig_block_len,
7156 ram_bytes,
7157 BTRFS_COMPRESS_NONE, /* compress_type */
7158 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007159 if (IS_ERR(em))
7160 goto out;
7161 }
7162 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7163 len, block_len, type);
7164 if (ret) {
7165 if (em) {
7166 free_extent_map(em);
7167 btrfs_drop_extent_cache(inode, start,
7168 start + len - 1, 0);
7169 }
7170 em = ERR_PTR(ret);
7171 }
7172 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007173
7174 return em;
7175}
7176
Josef Bacik4b46fce2010-05-23 11:00:55 -04007177static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7178 u64 start, u64 len)
7179{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007182 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007183 struct btrfs_key ins;
7184 u64 alloc_hint;
7185 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007188 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007189 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007190 if (ret)
7191 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007192
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007193 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7194 ins.objectid, ins.offset, ins.offset,
7195 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007196 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007197 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007198 btrfs_free_reserved_extent(fs_info, ins.objectid,
7199 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007200
Josef Bacik4b46fce2010-05-23 11:00:55 -04007201 return em;
7202}
7203
Chris Mason46bfbb52010-05-26 11:04:10 -04007204/*
7205 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7206 * block must be cow'd
7207 */
Josef Bacik00361582013-08-14 14:02:47 -04007208noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007209 u64 *orig_start, u64 *orig_block_len,
7210 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007211{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007213 struct btrfs_path *path;
7214 int ret;
7215 struct extent_buffer *leaf;
7216 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007218 struct btrfs_file_extent_item *fi;
7219 struct btrfs_key key;
7220 u64 disk_bytenr;
7221 u64 backref_offset;
7222 u64 extent_end;
7223 u64 num_bytes;
7224 int slot;
7225 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007226 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007227
Chris Mason46bfbb52010-05-26 11:04:10 -04007228 path = btrfs_alloc_path();
7229 if (!path)
7230 return -ENOMEM;
7231
David Sterbaf85b7372017-01-20 14:54:07 +01007232 ret = btrfs_lookup_file_extent(NULL, root, path,
7233 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007234 if (ret < 0)
7235 goto out;
7236
7237 slot = path->slots[0];
7238 if (ret == 1) {
7239 if (slot == 0) {
7240 /* can't find the item, must cow */
7241 ret = 0;
7242 goto out;
7243 }
7244 slot--;
7245 }
7246 ret = 0;
7247 leaf = path->nodes[0];
7248 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007249 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007250 key.type != BTRFS_EXTENT_DATA_KEY) {
7251 /* not our file or wrong item type, must cow */
7252 goto out;
7253 }
7254
7255 if (key.offset > offset) {
7256 /* Wrong offset, must cow */
7257 goto out;
7258 }
7259
7260 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7261 found_type = btrfs_file_extent_type(leaf, fi);
7262 if (found_type != BTRFS_FILE_EXTENT_REG &&
7263 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7264 /* not a regular extent, must cow */
7265 goto out;
7266 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007267
7268 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7269 goto out;
7270
Miao Xiee77751a2013-12-27 21:11:50 +08007271 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7272 if (extent_end <= offset)
7273 goto out;
7274
Chris Mason46bfbb52010-05-26 11:04:10 -04007275 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007276 if (disk_bytenr == 0)
7277 goto out;
7278
7279 if (btrfs_file_extent_compression(leaf, fi) ||
7280 btrfs_file_extent_encryption(leaf, fi) ||
7281 btrfs_file_extent_other_encoding(leaf, fi))
7282 goto out;
7283
Chris Mason46bfbb52010-05-26 11:04:10 -04007284 backref_offset = btrfs_file_extent_offset(leaf, fi);
7285
Josef Bacik7ee9e442013-06-21 16:37:03 -04007286 if (orig_start) {
7287 *orig_start = key.offset - backref_offset;
7288 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7289 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7290 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007291
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007292 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007293 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007294
7295 num_bytes = min(offset + *len, extent_end) - offset;
7296 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7297 u64 range_end;
7298
Jeff Mahoneyda170662016-06-15 09:22:56 -04007299 range_end = round_up(offset + num_bytes,
7300 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007301 ret = test_range_bit(io_tree, offset, range_end,
7302 EXTENT_DELALLOC, 0, NULL);
7303 if (ret) {
7304 ret = -EAGAIN;
7305 goto out;
7306 }
7307 }
7308
Josef Bacik1bda19e2013-10-18 12:10:36 -04007309 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007310
7311 /*
7312 * look for other files referencing this extent, if we
7313 * find any we must cow
7314 */
Josef Bacik00361582013-08-14 14:02:47 -04007315
Liu Boe4c3b2d2017-01-30 12:25:28 -08007316 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007317 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007318 if (ret) {
7319 ret = 0;
7320 goto out;
7321 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007322
7323 /*
7324 * adjust disk_bytenr and num_bytes to cover just the bytes
7325 * in this extent we are about to write. If there
7326 * are any csums in that range we have to cow in order
7327 * to keep the csums correct
7328 */
7329 disk_bytenr += backref_offset;
7330 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007331 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7332 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007333 /*
7334 * all of the above have passed, it is safe to overwrite this extent
7335 * without cow
7336 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007337 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007338 ret = 1;
7339out:
7340 btrfs_free_path(path);
7341 return ret;
7342}
7343
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007344bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7345{
7346 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7347 int found = false;
7348 void **pagep = NULL;
7349 struct page *page = NULL;
7350 int start_idx;
7351 int end_idx;
7352
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007353 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007354
7355 /*
7356 * end is the last byte in the last page. end == start is legal
7357 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007358 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007359
7360 rcu_read_lock();
7361
7362 /* Most of the code in this while loop is lifted from
7363 * find_get_page. It's been modified to begin searching from a
7364 * page and return just the first page found in that range. If the
7365 * found idx is less than or equal to the end idx then we know that
7366 * a page exists. If no pages are found or if those pages are
7367 * outside of the range then we're fine (yay!) */
7368 while (page == NULL &&
7369 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7370 page = radix_tree_deref_slot(pagep);
7371 if (unlikely(!page))
7372 break;
7373
7374 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007375 if (radix_tree_deref_retry(page)) {
7376 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007377 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007378 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007379 /*
7380 * Otherwise, shmem/tmpfs must be storing a swap entry
7381 * here as an exceptional entry: so return it without
7382 * attempting to raise page count.
7383 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007384 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007385 break; /* TODO: Is this relevant for this use case? */
7386 }
7387
Filipe Manana91405152014-06-05 13:22:24 +01007388 if (!page_cache_get_speculative(page)) {
7389 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007390 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007391 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007392
7393 /*
7394 * Has the page moved?
7395 * This is part of the lockless pagecache protocol. See
7396 * include/linux/pagemap.h for details.
7397 */
7398 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007399 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007400 page = NULL;
7401 }
7402 }
7403
7404 if (page) {
7405 if (page->index <= end_idx)
7406 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007407 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007408 }
7409
7410 rcu_read_unlock();
7411 return found;
7412}
7413
Josef Bacikeb838e72012-07-31 16:28:48 -04007414static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7415 struct extent_state **cached_state, int writing)
7416{
7417 struct btrfs_ordered_extent *ordered;
7418 int ret = 0;
7419
7420 while (1) {
7421 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007422 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007423 /*
7424 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007425 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007426 * extents in this range.
7427 */
7428 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7429 lockend - lockstart + 1);
7430
7431 /*
7432 * We need to make sure there are no buffered pages in this
7433 * range either, we could have raced between the invalidate in
7434 * generic_file_direct_write and locking the extent. The
7435 * invalidate needs to happen so that reads after a write do not
7436 * get stale data.
7437 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007438 if (!ordered &&
7439 (!writing ||
7440 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007441 break;
7442
7443 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7444 cached_state, GFP_NOFS);
7445
7446 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007447 /*
7448 * If we are doing a DIO read and the ordered extent we
7449 * found is for a buffered write, we can not wait for it
7450 * to complete and retry, because if we do so we can
7451 * deadlock with concurrent buffered writes on page
7452 * locks. This happens only if our DIO read covers more
7453 * than one extent map, if at this point has already
7454 * created an ordered extent for a previous extent map
7455 * and locked its range in the inode's io tree, and a
7456 * concurrent write against that previous extent map's
7457 * range and this range started (we unlock the ranges
7458 * in the io tree only when the bios complete and
7459 * buffered writes always lock pages before attempting
7460 * to lock range in the io tree).
7461 */
7462 if (writing ||
7463 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7464 btrfs_start_ordered_extent(inode, ordered, 1);
7465 else
7466 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007467 btrfs_put_ordered_extent(ordered);
7468 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007469 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007470 * We could trigger writeback for this range (and wait
7471 * for it to complete) and then invalidate the pages for
7472 * this range (through invalidate_inode_pages2_range()),
7473 * but that can lead us to a deadlock with a concurrent
7474 * call to readpages() (a buffered read or a defrag call
7475 * triggered a readahead) on a page lock due to an
7476 * ordered dio extent we created before but did not have
7477 * yet a corresponding bio submitted (whence it can not
7478 * complete), which makes readpages() wait for that
7479 * ordered extent to complete while holding a lock on
7480 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007482 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007483 }
7484
Filipe Mananaade77022016-02-18 14:28:55 +00007485 if (ret)
7486 break;
7487
Josef Bacikeb838e72012-07-31 16:28:48 -04007488 cond_resched();
7489 }
7490
7491 return ret;
7492}
7493
Liu Bo6f9994d2017-01-31 07:50:22 -08007494/* The callers of this must take lock_extent() */
7495static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7496 u64 orig_start, u64 block_start,
7497 u64 block_len, u64 orig_block_len,
7498 u64 ram_bytes, int compress_type,
7499 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007500{
7501 struct extent_map_tree *em_tree;
7502 struct extent_map *em;
7503 struct btrfs_root *root = BTRFS_I(inode)->root;
7504 int ret;
7505
Liu Bo6f9994d2017-01-31 07:50:22 -08007506 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7507 type == BTRFS_ORDERED_COMPRESSED ||
7508 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007509 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007510
Josef Bacik69ffb542012-09-11 15:40:07 -04007511 em_tree = &BTRFS_I(inode)->extent_tree;
7512 em = alloc_extent_map();
7513 if (!em)
7514 return ERR_PTR(-ENOMEM);
7515
7516 em->start = start;
7517 em->orig_start = orig_start;
7518 em->len = len;
7519 em->block_len = block_len;
7520 em->block_start = block_start;
7521 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007522 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007523 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007524 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007525 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007526 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007527 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007528 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007529 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7530 em->compress_type = compress_type;
7531 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007532
7533 do {
7534 btrfs_drop_extent_cache(inode, em->start,
7535 em->start + em->len - 1, 0);
7536 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007537 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007538 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007539 /*
7540 * The caller has taken lock_extent(), who could race with us
7541 * to add em?
7542 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007543 } while (ret == -EEXIST);
7544
7545 if (ret) {
7546 free_extent_map(em);
7547 return ERR_PTR(ret);
7548 }
7549
Liu Bo6f9994d2017-01-31 07:50:22 -08007550 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007551 return em;
7552}
7553
Filipe Manana9c9464c2015-11-04 09:52:04 +00007554static void adjust_dio_outstanding_extents(struct inode *inode,
7555 struct btrfs_dio_data *dio_data,
7556 const u64 len)
7557{
David Sterba823bb202017-01-04 11:09:51 +01007558 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007559
Filipe Manana9c9464c2015-11-04 09:52:04 +00007560 /*
7561 * If we have an outstanding_extents count still set then we're
7562 * within our reservation, otherwise we need to adjust our inode
7563 * counter appropriately.
7564 */
Liu Boc2931662016-12-22 17:13:54 -08007565 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007566 dio_data->outstanding_extents -= num_extents;
7567 } else {
Liu Boc2931662016-12-22 17:13:54 -08007568 /*
7569 * If dio write length has been split due to no large enough
7570 * contiguous space, we need to compensate our inode counter
7571 * appropriately.
7572 */
7573 u64 num_needed = num_extents - dio_data->outstanding_extents;
7574
Filipe Manana9c9464c2015-11-04 09:52:04 +00007575 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007576 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007577 spin_unlock(&BTRFS_I(inode)->lock);
7578 }
7579}
7580
Josef Bacik4b46fce2010-05-23 11:00:55 -04007581static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7582 struct buffer_head *bh_result, int create)
7583{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007584 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007586 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307587 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007588 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007589 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007590 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007591 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007592 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007593
Miao Xie172a5042013-02-21 02:48:22 -07007594 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007595 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007596 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007597 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007598
Josef Bacikc3298612012-08-03 16:49:19 -04007599 lockstart = start;
7600 lockend = start + len - 1;
7601
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007602 if (current->journal_info) {
7603 /*
7604 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007605 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007606 * confused.
7607 */
chandan50745b02015-08-28 21:10:13 +05307608 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007609 current->journal_info = NULL;
7610 }
7611
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 /*
7613 * If this errors out it's because we couldn't invalidate pagecache for
7614 * this range and we need to fallback to buffered.
7615 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007616 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7617 create)) {
7618 ret = -ENOTBLK;
7619 goto err;
7620 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007621
Josef Bacik4b46fce2010-05-23 11:00:55 -04007622 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 if (IS_ERR(em)) {
7624 ret = PTR_ERR(em);
7625 goto unlock_err;
7626 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007627
7628 /*
7629 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7630 * io. INLINE is special, and we could probably kludge it in here, but
7631 * it's still buffered so for safety lets just fall back to the generic
7632 * buffered path.
7633 *
7634 * For COMPRESSED we _have_ to read the entire extent in so we can
7635 * decompress it, so there will be buffering required no matter what we
7636 * do, so go ahead and fallback to buffered.
7637 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007638 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007639 * to buffered IO. Don't blame me, this is the price we pay for using
7640 * the generic code.
7641 */
7642 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7643 em->block_start == EXTENT_MAP_INLINE) {
7644 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 ret = -ENOTBLK;
7646 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647 }
7648
7649 /* Just a good old fashioned hole, return */
7650 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7651 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7652 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654 }
7655
7656 /*
7657 * We don't allocate a new extent in the following cases
7658 *
7659 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7660 * existing extent.
7661 * 2) The extent is marked as PREALLOC. We're good to go here and can
7662 * just use the extent.
7663 *
7664 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007665 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 len = min(len, em->len - (start - em->start));
7667 lockstart = start + len;
7668 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007669 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007670
7671 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7672 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7673 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007675 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676
7677 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7678 type = BTRFS_ORDERED_PREALLOC;
7679 else
7680 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007681 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007682 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007683
Josef Bacik00361582013-08-14 14:02:47 -04007684 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007685 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007686 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007687 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007688
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007689 em2 = btrfs_create_dio_extent(inode, start, len,
7690 orig_start, block_start,
7691 len, orig_block_len,
7692 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007693 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007694 if (type == BTRFS_ORDERED_PREALLOC) {
7695 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007696 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007697 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007698 if (em2 && IS_ERR(em2)) {
7699 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007700 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007701 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007702 /*
7703 * For inode marked NODATACOW or extent marked PREALLOC,
7704 * use the existing or preallocated extent, so does not
7705 * need to adjust btrfs_space_info's bytes_may_use.
7706 */
7707 btrfs_free_reserved_data_space_noquota(inode,
7708 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007709 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007711 }
Josef Bacik00361582013-08-14 14:02:47 -04007712
Chris Mason46bfbb52010-05-26 11:04:10 -04007713 /*
7714 * this will cow the extent, reset the len in case we changed
7715 * it above
7716 */
7717 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007718 free_extent_map(em);
7719 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 if (IS_ERR(em)) {
7721 ret = PTR_ERR(em);
7722 goto unlock_err;
7723 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007724 len = min(len, em->len - (start - em->start));
7725unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7727 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007728 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729 bh_result->b_bdev = em->bdev;
7730 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007731 if (create) {
7732 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7733 set_buffer_new(bh_result);
7734
7735 /*
7736 * Need to update the i_size under the extent lock so buffered
7737 * readers will get the updated i_size when we unlock.
7738 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007739 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007740 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007741
Filipe Manana9c9464c2015-11-04 09:52:04 +00007742 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307743 WARN_ON(dio_data->reserve < len);
7744 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007745 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307746 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007747 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007748
Josef Bacikeb838e72012-07-31 16:28:48 -04007749 /*
7750 * In the case of write we need to clear and unlock the entire range,
7751 * in the case of read we need to unlock only the end area that we
7752 * aren't using if there is any left over space.
7753 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007754 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007755 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7756 lockend, unlock_bits, 1, 0,
7757 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007758 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007759 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007760 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007761
Josef Bacik4b46fce2010-05-23 11:00:55 -04007762 free_extent_map(em);
7763
7764 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007765
7766unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7768 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007769err:
chandan50745b02015-08-28 21:10:13 +05307770 if (dio_data)
7771 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007772 /*
7773 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7774 * write less data then expected, so that we don't underflow our inode's
7775 * outstanding extents counter.
7776 */
7777 if (create && dio_data)
7778 adjust_dio_outstanding_extents(inode, dio_data, len);
7779
Josef Bacikeb838e72012-07-31 16:28:48 -04007780 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781}
7782
Miao Xie8b110e32014-09-12 18:44:03 +08007783static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007784 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007785{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007786 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007787 int ret;
7788
Mike Christie37226b22016-06-05 14:31:52 -05007789 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007790
7791 bio_get(bio);
7792
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007793 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007794 if (ret)
7795 goto err;
7796
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007797 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007798err:
7799 bio_put(bio);
7800 return ret;
7801}
7802
7803static int btrfs_check_dio_repairable(struct inode *inode,
7804 struct bio *failed_bio,
7805 struct io_failure_record *failrec,
7806 int failed_mirror)
7807{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007809 int num_copies;
7810
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007811 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007812 if (num_copies == 1) {
7813 /*
7814 * we only have a single copy of the data, so don't bother with
7815 * all the retry and error correction code that follows. no
7816 * matter what the error is, it is very likely to persist.
7817 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007818 btrfs_debug(fs_info,
7819 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7820 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007821 return 0;
7822 }
7823
7824 failrec->failed_mirror = failed_mirror;
7825 failrec->this_mirror++;
7826 if (failrec->this_mirror == failed_mirror)
7827 failrec->this_mirror++;
7828
7829 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007830 btrfs_debug(fs_info,
7831 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7832 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007833 return 0;
7834 }
7835
7836 return 1;
7837}
7838
7839static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307840 struct page *page, unsigned int pgoff,
7841 u64 start, u64 end, int failed_mirror,
7842 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007843{
7844 struct io_failure_record *failrec;
7845 struct bio *bio;
7846 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007847 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007848 int ret;
7849
Mike Christie37226b22016-06-05 14:31:52 -05007850 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007851
7852 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7853 if (ret)
7854 return ret;
7855
7856 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7857 failed_mirror);
7858 if (!ret) {
7859 free_io_failure(inode, failrec);
7860 return -EIO;
7861 }
7862
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307863 if ((failed_bio->bi_vcnt > 1)
7864 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007865 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007866 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007867
7868 isector = start - btrfs_io_bio(failed_bio)->logical;
7869 isector >>= inode->i_sb->s_blocksize_bits;
7870 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307871 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 if (!bio) {
7873 free_io_failure(inode, failrec);
7874 return -EIO;
7875 }
Mike Christie37226b22016-06-05 14:31:52 -05007876 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007877
7878 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7879 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7880 read_mode, failrec->this_mirror, failrec->in_validation);
7881
Mike Christie81a75f672016-06-05 14:31:54 -05007882 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007883 if (ret) {
7884 free_io_failure(inode, failrec);
7885 bio_put(bio);
7886 }
7887
7888 return ret;
7889}
7890
7891struct btrfs_retry_complete {
7892 struct completion done;
7893 struct inode *inode;
7894 u64 start;
7895 int uptodate;
7896};
7897
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007898static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007899{
7900 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307901 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007902 struct bio_vec *bvec;
7903 int i;
7904
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007905 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007906 goto end;
7907
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908 ASSERT(bio->bi_vcnt == 1);
7909 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007910 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307911
Miao Xie8b110e32014-09-12 18:44:03 +08007912 done->uptodate = 1;
7913 bio_for_each_segment_all(bvec, bio, i)
7914 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7915end:
7916 complete(&done->done);
7917 bio_put(bio);
7918}
7919
7920static int __btrfs_correct_data_nocsum(struct inode *inode,
7921 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007924 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007925 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 unsigned int pgoff;
7928 u32 sectorsize;
7929 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007930 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007931 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007932
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307933 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007934 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935
Miao Xiec1dc0892014-09-12 18:43:56 +08007936 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007937 done.inode = inode;
7938
7939 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307940 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7941 pgoff = bvec->bv_offset;
7942
7943next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007944 done.uptodate = 0;
7945 done.start = start;
7946 init_completion(&done.done);
7947
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7949 pgoff, start, start + sectorsize - 1,
7950 io_bio->mirror_num,
7951 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007952 if (ret)
7953 return ret;
7954
7955 wait_for_completion(&done.done);
7956
7957 if (!done.uptodate) {
7958 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307959 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007960 }
7961
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962 start += sectorsize;
7963
7964 if (nr_sectors--) {
7965 pgoff += sectorsize;
7966 goto next_block_or_try_again;
7967 }
Miao Xie8b110e32014-09-12 18:44:03 +08007968 }
7969
7970 return 0;
7971}
7972
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007973static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007974{
7975 struct btrfs_retry_complete *done = bio->bi_private;
7976 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007978 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007980 int uptodate;
7981 int ret;
7982 int i;
7983
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007984 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007985 goto end;
7986
7987 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988
7989 start = done->start;
7990
7991 ASSERT(bio->bi_vcnt == 1);
7992 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007993 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994
Miao Xie8b110e32014-09-12 18:44:03 +08007995 bio_for_each_segment_all(bvec, bio, i) {
7996 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307997 bvec->bv_page, bvec->bv_offset,
7998 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007999 if (!ret)
8000 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008002 else
8003 uptodate = 0;
8004 }
8005
8006 done->uptodate = uptodate;
8007end:
8008 complete(&done->done);
8009 bio_put(bio);
8010}
8011
8012static int __btrfs_subio_endio_read(struct inode *inode,
8013 struct btrfs_io_bio *io_bio, int err)
8014{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008016 struct bio_vec *bvec;
8017 struct btrfs_retry_complete done;
8018 u64 start;
8019 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 u32 sectorsize;
8021 int nr_sectors;
8022 unsigned int pgoff;
8023 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008024 int i;
8025 int ret;
8026
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008028 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029
Miao Xie8b110e32014-09-12 18:44:03 +08008030 err = 0;
8031 start = io_bio->logical;
8032 done.inode = inode;
8033
Miao Xiec1dc0892014-09-12 18:43:56 +08008034 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8036
8037 pgoff = bvec->bv_offset;
8038next_block:
8039 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8040 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8041 bvec->bv_page, pgoff, start,
8042 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008043 if (likely(!ret))
8044 goto next;
8045try_again:
8046 done.uptodate = 0;
8047 done.start = start;
8048 init_completion(&done.done);
8049
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308050 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8051 pgoff, start, start + sectorsize - 1,
8052 io_bio->mirror_num,
8053 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008054 if (ret) {
8055 err = ret;
8056 goto next;
8057 }
8058
8059 wait_for_completion(&done.done);
8060
8061 if (!done.uptodate) {
8062 /* We might have another mirror, so try again */
8063 goto try_again;
8064 }
8065next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066 offset += sectorsize;
8067 start += sectorsize;
8068
8069 ASSERT(nr_sectors);
8070
8071 if (--nr_sectors) {
8072 pgoff += sectorsize;
8073 goto next_block;
8074 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008075 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008076
8077 return err;
8078}
8079
Miao Xie8b110e32014-09-12 18:44:03 +08008080static int btrfs_subio_endio_read(struct inode *inode,
8081 struct btrfs_io_bio *io_bio, int err)
8082{
8083 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8084
8085 if (skip_csum) {
8086 if (unlikely(err))
8087 return __btrfs_correct_data_nocsum(inode, io_bio);
8088 else
8089 return 0;
8090 } else {
8091 return __btrfs_subio_endio_read(inode, io_bio, err);
8092 }
8093}
8094
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008095static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008096{
8097 struct btrfs_dio_private *dip = bio->bi_private;
8098 struct inode *inode = dip->inode;
8099 struct bio *dio_bio;
8100 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008101 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008102
Miao Xie8b110e32014-09-12 18:44:03 +08008103 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8104 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008105
Josef Bacik4b46fce2010-05-23 11:00:55 -04008106 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008107 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008108 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008109
Josef Bacik4b46fce2010-05-23 11:00:55 -04008110 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008111
Filipe Manana1636d1d2016-02-15 16:20:26 +00008112 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008113 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008114
8115 if (io_bio->end_io)
8116 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008117 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008118}
8119
Filipe Manana14543772015-11-24 16:23:54 +00008120static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8121 const u64 offset,
8122 const u64 bytes,
8123 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008126 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008127 u64 ordered_offset = offset;
8128 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008129 int ret;
8130
Chris Mason163cf092010-11-28 19:56:33 -05008131again:
8132 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8133 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008134 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008135 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008136 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008137 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008138
Liu Bo9e0af232014-08-15 23:36:53 +08008139 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8140 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008141 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008142out_test:
8143 /*
8144 * our bio might span multiple ordered extents. If we haven't
8145 * completed the accounting for the whole dio, go back and try again
8146 */
Filipe Manana14543772015-11-24 16:23:54 +00008147 if (ordered_offset < offset + bytes) {
8148 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008149 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008150 goto again;
8151 }
Filipe Manana14543772015-11-24 16:23:54 +00008152}
8153
8154static void btrfs_endio_direct_write(struct bio *bio)
8155{
8156 struct btrfs_dio_private *dip = bio->bi_private;
8157 struct bio *dio_bio = dip->dio_bio;
8158
8159 btrfs_endio_direct_write_update_ordered(dip->inode,
8160 dip->logical_offset,
8161 dip->bytes,
8162 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163
Josef Bacik4b46fce2010-05-23 11:00:55 -04008164 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008165
Filipe Manana1636d1d2016-02-15 16:20:26 +00008166 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008167 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008168 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008169}
8170
Mike Christie81a75f672016-06-05 14:31:54 -05008171static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008172 struct bio *bio, int mirror_num,
8173 unsigned long bio_flags, u64 offset)
8174{
8175 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008176 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008177 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008178 return 0;
8179}
8180
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008181static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008182{
8183 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008184 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008185
Miao Xie8b110e32014-09-12 18:44:03 +08008186 if (err)
8187 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008188 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008189 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8190 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008191 (unsigned long long)bio->bi_iter.bi_sector,
8192 bio->bi_iter.bi_size, err);
8193
8194 if (dip->subio_endio)
8195 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008196
8197 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008198 dip->errors = 1;
8199
8200 /*
8201 * before atomic variable goto zero, we must make sure
8202 * dip->errors is perceived to be set.
8203 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008204 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008205 }
8206
8207 /* if there are more bios still pending for this dio, just exit */
8208 if (!atomic_dec_and_test(&dip->pending_bios))
8209 goto out;
8210
Chris Mason9be33952013-05-17 18:30:14 -04008211 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008212 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008213 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008214 dip->dio_bio->bi_error = 0;
8215 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008216 }
8217out:
8218 bio_put(bio);
8219}
8220
8221static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8222 u64 first_sector, gfp_t gfp_flags)
8223{
Chris Masonda2f0f72015-07-02 13:57:22 -07008224 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008225 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008226 if (bio)
8227 bio_associate_current(bio);
8228 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008229}
8230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008231static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008232 struct btrfs_dio_private *dip,
8233 struct bio *bio,
8234 u64 file_offset)
8235{
8236 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8237 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8238 int ret;
8239
8240 /*
8241 * We load all the csum data we need when we submit
8242 * the first bio to reduce the csum tree search and
8243 * contention.
8244 */
8245 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008246 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008247 file_offset);
8248 if (ret)
8249 return ret;
8250 }
8251
8252 if (bio == dip->orig_bio)
8253 return 0;
8254
8255 file_offset -= dip->logical_offset;
8256 file_offset >>= inode->i_sb->s_blocksize_bits;
8257 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8258
8259 return 0;
8260}
8261
Miao Xiee65e1532010-11-22 03:04:43 +00008262static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008263 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008264 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008265{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008266 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008267 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008268 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008269 int ret;
8270
Josef Bacikb812ce22012-11-16 13:56:32 -05008271 if (async_submit)
8272 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8273
Miao Xiee65e1532010-11-22 03:04:43 +00008274 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008275
8276 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008277 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008278 if (ret)
8279 goto err;
8280 }
Miao Xiee65e1532010-11-22 03:04:43 +00008281
Josef Bacik1ae39932011-04-06 14:41:34 -04008282 if (skip_sum)
8283 goto map;
8284
8285 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008286 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8287 file_offset,
8288 __btrfs_submit_bio_start_direct_io,
8289 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008290 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008291 } else if (write) {
8292 /*
8293 * If we aren't doing async submit, calculate the csum of the
8294 * bio now.
8295 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008296 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008297 if (ret)
8298 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008299 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008300 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008301 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008302 if (ret)
8303 goto err;
8304 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008305map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008306 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008307err:
8308 bio_put(bio);
8309 return ret;
8310}
8311
Mike Christie81a75f672016-06-05 14:31:54 -05008312static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008313 int skip_sum)
8314{
8315 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008317 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008318 struct bio *bio;
8319 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008320 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008321 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008322 u64 file_offset = dip->logical_offset;
8323 u64 submit_len = 0;
8324 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008325 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008326 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308327 int nr_sectors;
8328 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008329 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008330
Kent Overstreet4f024f32013-10-11 15:44:27 -07008331 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008332 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8333 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008334 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008335 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008336
Kent Overstreet4f024f32013-10-11 15:44:27 -07008337 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008338 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008339 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008340 goto submit;
8341 }
8342
David Woodhouse53b381b2013-01-29 18:40:14 -05008343 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008344 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008345 async_submit = 0;
8346 else
8347 async_submit = 1;
8348
Josef Bacik02f57c72011-04-06 14:25:44 -04008349 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8350 if (!bio)
8351 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008352
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008353 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008354 bio->bi_private = dip;
8355 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008356 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008357 atomic_inc(&dip->pending_bios);
8358
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008359 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008360 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308361 i = 0;
8362next_block:
8363 if (unlikely(map_length < submit_len + blocksize ||
8364 bio_add_page(bio, bvec->bv_page, blocksize,
8365 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008366 /*
8367 * inc the count before we submit the bio so
8368 * we know the end IO handler won't happen before
8369 * we inc the count. Otherwise, the dip might get freed
8370 * before we're done setting it up
8371 */
8372 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008373 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008374 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008375 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008376 if (ret) {
8377 bio_put(bio);
8378 atomic_dec(&dip->pending_bios);
8379 goto out_err;
8380 }
8381
Miao Xiee65e1532010-11-22 03:04:43 +00008382 start_sector += submit_len >> 9;
8383 file_offset += submit_len;
8384
8385 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008386
8387 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8388 start_sector, GFP_NOFS);
8389 if (!bio)
8390 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008391 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008392 bio->bi_private = dip;
8393 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008394 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008395
Kent Overstreet4f024f32013-10-11 15:44:27 -07008396 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008397 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008398 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008399 &map_length, NULL, 0);
8400 if (ret) {
8401 bio_put(bio);
8402 goto out_err;
8403 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308404
8405 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008406 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308407 submit_len += blocksize;
8408 if (--nr_sectors) {
8409 i++;
8410 goto next_block;
8411 }
Miao Xiee65e1532010-11-22 03:04:43 +00008412 }
8413 }
8414
Josef Bacik02f57c72011-04-06 14:25:44 -04008415submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008416 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008417 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008418 if (!ret)
8419 return 0;
8420
8421 bio_put(bio);
8422out_err:
8423 dip->errors = 1;
8424 /*
8425 * before atomic variable goto zero, we must
8426 * make sure dip->errors is perceived to be set.
8427 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008428 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008429 if (atomic_dec_and_test(&dip->pending_bios))
8430 bio_io_error(dip->orig_bio);
8431
8432 /* bio_end_io() will handle error, so we needn't return it */
8433 return 0;
8434}
8435
Mike Christie8a4c1e42016-06-05 14:31:50 -05008436static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8437 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008438{
Filipe Manana61de7182015-07-01 12:13:10 +01008439 struct btrfs_dio_private *dip = NULL;
8440 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008441 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008442 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008443 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008444 int ret = 0;
8445
8446 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8447
Chris Mason9be33952013-05-17 18:30:14 -04008448 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008449 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008450 ret = -ENOMEM;
8451 goto free_ordered;
8452 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008453
Miao Xiec1dc0892014-09-12 18:43:56 +08008454 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008455 if (!dip) {
8456 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008457 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008458 }
8459
8460 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008461 dip->inode = inode;
8462 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008463 dip->bytes = dio_bio->bi_iter.bi_size;
8464 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008465 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008466 dip->orig_bio = io_bio;
8467 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008468 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008469 btrfs_bio = btrfs_io_bio(io_bio);
8470 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008471
Miao Xiec1dc0892014-09-12 18:43:56 +08008472 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008473 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008474 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008475 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008476 dip->subio_endio = btrfs_subio_endio_read;
8477 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008478
Filipe Mananaf28a4922015-12-08 19:23:20 +00008479 /*
8480 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8481 * even if we fail to submit a bio, because in such case we do the
8482 * corresponding error handling below and it must not be done a second
8483 * time by btrfs_direct_IO().
8484 */
8485 if (write) {
8486 struct btrfs_dio_data *dio_data = current->journal_info;
8487
8488 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8489 dip->bytes;
8490 dio_data->unsubmitted_oe_range_start =
8491 dio_data->unsubmitted_oe_range_end;
8492 }
8493
Mike Christie81a75f672016-06-05 14:31:54 -05008494 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008495 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008496 return;
Chris Mason9be33952013-05-17 18:30:14 -04008497
Miao Xie23ea8e52014-09-12 18:43:54 +08008498 if (btrfs_bio->end_io)
8499 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008500
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501free_ordered:
8502 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008503 * If we arrived here it means either we failed to submit the dip
8504 * or we either failed to clone the dio_bio or failed to allocate the
8505 * dip. If we cloned the dio_bio and allocated the dip, we can just
8506 * call bio_endio against our io_bio so that we get proper resource
8507 * cleanup if we fail to submit the dip, otherwise, we must do the
8508 * same as btrfs_endio_direct_[write|read] because we can't call these
8509 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008510 */
Filipe Manana61de7182015-07-01 12:13:10 +01008511 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008512 io_bio->bi_error = -EIO;
8513 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008514 /*
8515 * The end io callbacks free our dip, do the final put on io_bio
8516 * and all the cleanup and final put for dio_bio (through
8517 * dio_end_io()).
8518 */
8519 dip = NULL;
8520 io_bio = NULL;
8521 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008522 if (write)
8523 btrfs_endio_direct_write_update_ordered(inode,
8524 file_offset,
8525 dio_bio->bi_iter.bi_size,
8526 0);
8527 else
Filipe Manana61de7182015-07-01 12:13:10 +01008528 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8529 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008530
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008531 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008532 /*
8533 * Releases and cleans up our dio_bio, no need to bio_put()
8534 * nor bio_endio()/bio_io_error() against dio_bio.
8535 */
8536 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008537 }
Filipe Manana61de7182015-07-01 12:13:10 +01008538 if (io_bio)
8539 bio_put(io_bio);
8540 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541}
8542
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008543static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8544 struct kiocb *iocb,
8545 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008546{
8547 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008548 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008549 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008550 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008551
8552 if (offset & blocksize_mask)
8553 goto out;
8554
Al Viro28060d52014-03-22 05:15:17 -04008555 if (iov_iter_alignment(iter) & blocksize_mask)
8556 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008557
Al Viro28060d52014-03-22 05:15:17 -04008558 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008559 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008560 return 0;
8561 /*
8562 * Check to make sure we don't have duplicate iov_base's in this
8563 * iovec, if so return EINVAL, otherwise we'll get csum errors
8564 * when reading back.
8565 */
8566 for (seg = 0; seg < iter->nr_segs; seg++) {
8567 for (i = seg + 1; i < iter->nr_segs; i++) {
8568 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008569 goto out;
8570 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008571 }
8572 retval = 0;
8573out:
8574 return retval;
8575}
Josef Bacikeb838e72012-07-31 16:28:48 -04008576
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008577static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008578{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008579 struct file *file = iocb->ki_filp;
8580 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008581 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308582 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008583 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008584 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008585 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008586 bool wakeup = true;
8587 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008588 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008589
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008590 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008591 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008592
Jens Axboefe0f07d2015-04-15 17:05:48 -06008593 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008594 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008595
Josef Bacik0e267c42013-07-02 10:38:02 -04008596 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008597 * The generic stuff only does filemap_write_and_wait_range, which
8598 * isn't enough if we've written compressed pages to this area, so
8599 * we need to flush the dirty pages again to make absolutely sure
8600 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008601 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008602 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008603 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8604 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008605 filemap_fdatawrite_range(inode->i_mapping, offset,
8606 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008607
Omar Sandoval6f673762015-03-16 04:33:52 -07008608 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008609 /*
8610 * If the write DIO is beyond the EOF, we need update
8611 * the isize, but it is protected by i_mutex. So we can
8612 * not unlock the i_mutex at this case.
8613 */
8614 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008615 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008616 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008617 relock = true;
8618 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008619 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008620 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008621 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008622 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008623
8624 /*
8625 * We need to know how many extents we reserved so that we can
8626 * do the accounting properly if we go over the number we
8627 * originally calculated. Abuse current->journal_info for this.
8628 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008629 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008630 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008631 dio_data.unsubmitted_oe_range_start = (u64)offset;
8632 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308633 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308634 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008635 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8636 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008637 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008638 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8639 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008640 }
8641
Omar Sandoval17f8c842015-03-16 04:33:50 -07008642 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008643 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008644 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008645 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008646 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308647 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008648 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008649 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308650 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008651 btrfs_delalloc_release_space(inode, offset,
8652 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008653 /*
8654 * On error we might have left some ordered extents
8655 * without submitting corresponding bios for them, so
8656 * cleanup them up to avoid other tasks getting them
8657 * and waiting for them to complete forever.
8658 */
8659 if (dio_data.unsubmitted_oe_range_start <
8660 dio_data.unsubmitted_oe_range_end)
8661 btrfs_endio_direct_write_update_ordered(inode,
8662 dio_data.unsubmitted_oe_range_start,
8663 dio_data.unsubmitted_oe_range_end -
8664 dio_data.unsubmitted_oe_range_start,
8665 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008666 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008667 btrfs_delalloc_release_space(inode, offset,
8668 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008669 }
Miao Xie38851cc2013-02-08 07:04:11 +00008670out:
Miao Xie2e60a512013-02-08 07:01:08 +00008671 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008672 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008673 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008674 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008675
8676 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008677}
8678
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008679#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8680
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008681static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8682 __u64 start, __u64 len)
8683{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008684 int ret;
8685
8686 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8687 if (ret)
8688 return ret;
8689
Chris Masonec29ed52011-02-23 16:23:20 -05008690 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008691}
8692
Chris Mason9ebefb182007-06-15 13:50:00 -04008693int btrfs_readpage(struct file *file, struct page *page)
8694{
Chris Masond1310b22008-01-24 16:13:08 -05008695 struct extent_io_tree *tree;
8696 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008697 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008698}
Chris Mason1832a6d2007-12-21 16:27:21 -05008699
Chris Mason39279cc2007-06-12 06:35:45 -04008700static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8701{
Chris Masond1310b22008-01-24 16:13:08 -05008702 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008703 struct inode *inode = page->mapping->host;
8704 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008705
8706 if (current->flags & PF_MEMALLOC) {
8707 redirty_page_for_writepage(wbc, page);
8708 unlock_page(page);
8709 return 0;
8710 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008711
8712 /*
8713 * If we are under memory pressure we will call this directly from the
8714 * VM, we need to make sure we have the inode referenced for the ordered
8715 * extent. If not just return like we didn't do anything.
8716 */
8717 if (!igrab(inode)) {
8718 redirty_page_for_writepage(wbc, page);
8719 return AOP_WRITEPAGE_ACTIVATE;
8720 }
Chris Masond1310b22008-01-24 16:13:08 -05008721 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008722 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8723 btrfs_add_delayed_iput(inode);
8724 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008725}
Chris Mason39279cc2007-06-12 06:35:45 -04008726
Eric Sandeen48a3b632013-04-25 20:41:01 +00008727static int btrfs_writepages(struct address_space *mapping,
8728 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008729{
Chris Masond1310b22008-01-24 16:13:08 -05008730 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008731
Chris Masond1310b22008-01-24 16:13:08 -05008732 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008733 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8734}
8735
Chris Mason3ab2fb52007-11-08 10:59:22 -05008736static int
8737btrfs_readpages(struct file *file, struct address_space *mapping,
8738 struct list_head *pages, unsigned nr_pages)
8739{
Chris Masond1310b22008-01-24 16:13:08 -05008740 struct extent_io_tree *tree;
8741 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008742 return extent_readpages(tree, mapping, pages, nr_pages,
8743 btrfs_get_extent);
8744}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008745static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008746{
Chris Masond1310b22008-01-24 16:13:08 -05008747 struct extent_io_tree *tree;
8748 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008749 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008750
Chris Masond1310b22008-01-24 16:13:08 -05008751 tree = &BTRFS_I(page->mapping->host)->io_tree;
8752 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008753 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008754 if (ret == 1) {
8755 ClearPagePrivate(page);
8756 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008757 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008758 }
8759 return ret;
8760}
Chris Mason39279cc2007-06-12 06:35:45 -04008761
Chris Masone6dcd2d2008-07-17 12:53:50 -04008762static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8763{
Chris Mason98509cf2008-09-11 15:51:43 -04008764 if (PageWriteback(page) || PageDirty(page))
8765 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008766 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008767}
8768
Lukas Czernerd47992f2013-05-21 23:17:23 -04008769static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8770 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008771{
Josef Bacik5fd02042012-05-02 14:00:54 -04008772 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008773 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008774 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008775 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008776 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008777 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308778 u64 start;
8779 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008780 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008781
Chris Mason8b62b722009-09-02 16:53:46 -04008782 /*
8783 * we have the page locked, so new writeback can't start,
8784 * and the dirty bit won't be cleared while we are here.
8785 *
8786 * Wait for IO on this page so that we can safely clear
8787 * the PagePrivate2 bit and do ordered accounting
8788 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008790
Josef Bacik5fd02042012-05-02 14:00:54 -04008791 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008792 if (offset) {
8793 btrfs_releasepage(page, GFP_NOFS);
8794 return;
8795 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008796
8797 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008798 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308799again:
8800 start = page_start;
8801 ordered = btrfs_lookup_ordered_range(inode, start,
8802 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008803 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308804 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008805 /*
8806 * IO on this page will never be started, so we need
8807 * to account for any ordered extents now
8808 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008809 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308810 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008811 EXTENT_DIRTY | EXTENT_DELALLOC |
8812 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8813 EXTENT_DEFRAG, 1, 0, &cached_state,
8814 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008815 /*
8816 * whoever cleared the private bit is responsible
8817 * for the finish_ordered_io
8818 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008819 if (TestClearPagePrivate2(page)) {
8820 struct btrfs_ordered_inode_tree *tree;
8821 u64 new_len;
8822
8823 tree = &BTRFS_I(inode)->ordered_tree;
8824
8825 spin_lock_irq(&tree->lock);
8826 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308827 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008828 if (new_len < ordered->truncated_len)
8829 ordered->truncated_len = new_len;
8830 spin_unlock_irq(&tree->lock);
8831
8832 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308833 start,
8834 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008835 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008836 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838 if (!inode_evicting) {
8839 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308840 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008841 &cached_state);
8842 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308843
8844 start = end + 1;
8845 if (start < page_end)
8846 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008847 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008848
Qu Wenruob9d0b382015-09-29 10:35:16 +08008849 /*
8850 * Qgroup reserved space handler
8851 * Page here will be either
8852 * 1) Already written to disk
8853 * In this case, its reserved space is released from data rsv map
8854 * and will be freed by delayed_ref handler finally.
8855 * So even we call qgroup_free_data(), it won't decrease reserved
8856 * space.
8857 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008858 * This means the reserved space should be freed here. However,
8859 * if a truncate invalidates the page (by clearing PageDirty)
8860 * and the page is accounted for while allocating extent
8861 * in btrfs_check_data_free_space() we let delayed_ref to
8862 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008863 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008864 if (PageDirty(page))
8865 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008866 if (!inode_evicting) {
8867 clear_extent_bit(tree, page_start, page_end,
8868 EXTENT_LOCKED | EXTENT_DIRTY |
8869 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8870 EXTENT_DEFRAG, 1, 1,
8871 &cached_state, GFP_NOFS);
8872
8873 __btrfs_releasepage(page, GFP_NOFS);
8874 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008875
Chris Mason4a096752008-07-21 10:29:44 -04008876 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008877 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008878 ClearPagePrivate(page);
8879 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008880 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008881 }
Chris Mason39279cc2007-06-12 06:35:45 -04008882}
8883
Chris Mason9ebefb182007-06-15 13:50:00 -04008884/*
8885 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8886 * called from a page fault handler when a page is first dirtied. Hence we must
8887 * be careful to check for EOF conditions here. We set the page up correctly
8888 * for a written page which means we get ENOSPC checking when writing into
8889 * holes and correct delalloc and unwritten extent mapping on filesystems that
8890 * support these features.
8891 *
8892 * We are not allowed to take the i_mutex here so we have to play games to
8893 * protect against truncate races as the page could now be beyond EOF. Because
8894 * vmtruncate() writes the inode size before removing pages, once we have the
8895 * page lock we can determine safely if the page is beyond EOF. If it is not
8896 * beyond EOF, then the page is guaranteed safe against truncation until we
8897 * unlock the page.
8898 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008899int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008900{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008901 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008902 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008903 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8905 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008906 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907 char *kaddr;
8908 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008909 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008910 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008911 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308912 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008913 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008914 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308915 u64 end;
8916
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008917 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008918
Jan Karab2b5ef52012-06-12 16:20:45 +02008919 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008920 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008921 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308922 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008923
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308924 /*
8925 * Reserving delalloc space after obtaining the page lock can lead to
8926 * deadlock. For example, if a dirty page is locked by this function
8927 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8928 * dirty page write out, then the btrfs_writepage() function could
8929 * end up waiting indefinitely to get a lock on the page currently
8930 * being processed by btrfs_page_mkwrite() function.
8931 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008932 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308933 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008934 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008935 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008936 reserved = 1;
8937 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008938 if (ret) {
8939 if (ret == -ENOMEM)
8940 ret = VM_FAULT_OOM;
8941 else /* -ENOSPC, -EIO, etc */
8942 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008943 if (reserved)
8944 goto out;
8945 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008946 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008947
Nick Piggin56a76f82009-03-31 15:23:23 -07008948 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008952
Chris Mason9ebefb182007-06-15 13:50:00 -04008953 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008955 /* page got truncated out from underneath us */
8956 goto out_unlock;
8957 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 wait_on_page_writeback(page);
8959
David Sterbaff13db42015-12-03 14:30:40 +01008960 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961 set_page_extent_mapped(page);
8962
Chris Masoneb84ae02008-07-17 13:53:27 -04008963 /*
8964 * we can't set the delalloc bits if there are pending ordered
8965 * extents. Drop our locks and wait for them to finish
8966 */
Liu Bo9a9239a2017-01-24 15:58:51 -08008967 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008969 unlock_extent_cached(io_tree, page_start, page_end,
8970 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008972 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 btrfs_put_ordered_extent(ordered);
8974 goto again;
8975 }
8976
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008977 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008978 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008979 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008980 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308981 end = page_start + reserved_space - 1;
8982 spin_lock(&BTRFS_I(inode)->lock);
8983 BTRFS_I(inode)->outstanding_extents++;
8984 spin_unlock(&BTRFS_I(inode)->lock);
8985 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008986 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308987 }
8988 }
8989
Josef Bacikfbf19082009-10-01 17:10:23 -04008990 /*
Liu Bo54160342016-12-13 12:15:19 -08008991 * page_mkwrite gets called when the page is firstly dirtied after it's
8992 * faulted in, but write(2) could also dirty a page and set delalloc
8993 * bits, thus in this case for space account reason, we still need to
8994 * clear any delalloc bits within this page range since we have to
8995 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008996 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308997 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008998 EXTENT_DIRTY | EXTENT_DELALLOC |
8999 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009000 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009001
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309002 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009003 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009004 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009005 unlock_extent_cached(io_tree, page_start, page_end,
9006 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009007 ret = VM_FAULT_SIGBUS;
9008 goto out_unlock;
9009 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009010 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009011
9012 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009013 if (page_start + PAGE_SIZE > size)
9014 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009016 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009017
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009020 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009021 flush_dcache_page(page);
9022 kunmap(page);
9023 }
Chris Mason247e7432008-07-17 12:53:51 -04009024 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009026 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009027
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009028 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009029 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009030 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009031
Josef Bacik2ac55d42010-02-03 19:33:23 +00009032 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009033
9034out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009035 if (!ret) {
9036 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009037 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009038 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009039 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009040out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309041 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009042out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009043 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009044 return ret;
9045}
9046
Josef Bacika41ad392011-01-31 15:30:16 -05009047static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009048{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009050 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009051 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009052 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009053 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009054 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009055 u64 mask = fs_info->sectorsize - 1;
9056 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009057
Josef Bacik0ef8b722013-10-25 16:13:35 -04009058 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9059 (u64)-1);
9060 if (ret)
9061 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009062
Josef Bacikfcb80c22011-05-03 10:40:22 -04009063 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009064 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009065 * 3 things going on here
9066 *
9067 * 1) We need to reserve space for our orphan item and the space to
9068 * delete our orphan item. Lord knows we don't want to have a dangling
9069 * orphan item because we didn't reserve space to remove it.
9070 *
9071 * 2) We need to reserve space to update our inode.
9072 *
9073 * 3) We need to have something to cache all the space that is going to
9074 * be free'd up by the truncate operation, but also have some slack
9075 * space reserved in case it uses space during the truncate (thank you
9076 * very much snapshotting).
9077 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009078 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009079 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009080 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009081 * doesn't end up using space reserved for updating the inode or
9082 * removing the orphan item. We also need to be able to stop the
9083 * transaction and start a new one, which means we need to be able to
9084 * update the inode several times, and we have no idea of knowing how
9085 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009086 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009087 * Then there is the orphan item, which does indeed need to be held on
9088 * to for the whole operation, and we need nobody to touch this reserved
9089 * space except the orphan code.
9090 *
9091 * So that leaves us with
9092 *
9093 * 1) root->orphan_block_rsv - for the orphan deletion.
9094 * 2) rsv - for the truncate reservation, which we will steal from the
9095 * transaction reservation.
9096 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9097 * updating the inode.
9098 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009099 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 if (!rsv)
9101 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009102 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009103 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009104
Josef Bacik907cbce2011-08-08 13:46:15 -04009105 /*
Josef Bacik07127182011-08-19 10:29:59 -04009106 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009107 * 1 for updating the inode.
9108 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009109 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009110 if (IS_ERR(trans)) {
9111 err = PTR_ERR(trans);
9112 goto out;
9113 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009114
Josef Bacik907cbce2011-08-08 13:46:15 -04009115 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009116 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009117 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009118 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009119
Chris Mason5a3f23d2009-03-31 13:27:11 -04009120 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009121 * So if we truncate and then write and fsync we normally would just
9122 * write the extents that changed, which is a problem if we need to
9123 * first truncate that entire inode. So set this flag so we write out
9124 * all of the extents in the inode to the sync log so we're completely
9125 * safe.
9126 */
9127 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009128 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009129
Yan, Zheng80825102009-11-12 09:35:36 +00009130 while (1) {
9131 ret = btrfs_truncate_inode_items(trans, root, inode,
9132 inode->i_size,
9133 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009134 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009135 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009136 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009137 }
Chris Mason39279cc2007-06-12 06:35:45 -04009138
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009139 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009140 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009141 if (ret) {
9142 err = ret;
9143 break;
9144 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009145
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009146 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009147 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009148
9149 trans = btrfs_start_transaction(root, 2);
9150 if (IS_ERR(trans)) {
9151 ret = err = PTR_ERR(trans);
9152 trans = NULL;
9153 break;
9154 }
9155
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009156 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009157 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009158 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009159 BUG_ON(ret); /* shouldn't happen */
9160 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009161 }
9162
9163 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009164 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009165 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009166 if (ret)
9167 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009168 }
9169
Chris Mason917c16b2011-11-08 14:49:59 -05009170 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009171 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009172 ret = btrfs_update_inode(trans, root, inode);
9173 if (ret && !err)
9174 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009175
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009176 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009177 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009178 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009179out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009180 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009181
Josef Bacik3893e332011-01-31 16:03:11 -05009182 if (ret && !err)
9183 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009184
Josef Bacik3893e332011-01-31 16:03:11 -05009185 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009186}
9187
Sven Wegener3b963622008-06-09 21:57:42 -04009188/*
Chris Masond352ac62008-09-29 15:18:18 -04009189 * create a new subvolume directory/inode (helper for the ioctl).
9190 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009191int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009192 struct btrfs_root *new_root,
9193 struct btrfs_root *parent_root,
9194 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009195{
Chris Mason39279cc2007-06-12 06:35:45 -04009196 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009197 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009198 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009199
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009200 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9201 new_dirid, new_dirid,
9202 S_IFDIR | (~current_umask() & S_IRWXUGO),
9203 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009204 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009205 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009206 inode->i_op = &btrfs_dir_inode_operations;
9207 inode->i_fop = &btrfs_dir_file_operations;
9208
Miklos Szeredibfe86842011-10-28 14:13:29 +02009209 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009210 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009211 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009212
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009213 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9214 if (err)
9215 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009216 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009217 new_root->root_key.objectid, err);
9218
Yan, Zheng76dda932009-09-21 16:00:26 -04009219 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009220
Yan, Zheng76dda932009-09-21 16:00:26 -04009221 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009222 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009223}
9224
Chris Mason39279cc2007-06-12 06:35:45 -04009225struct inode *btrfs_alloc_inode(struct super_block *sb)
9226{
9227 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009229
9230 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9231 if (!ei)
9232 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009233
9234 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009235 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009236 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009237 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009238 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009239 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009240 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 ei->disk_i_size = 0;
9242 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009243 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009245 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009247 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009248 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249
Josef Bacik9e0baf62011-07-15 15:16:44 +00009250 spin_lock_init(&ei->lock);
9251 ei->outstanding_extents = 0;
9252 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009253
Josef Bacik72ac3c02012-05-23 14:13:11 -04009254 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009255 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009256
Miao Xie16cdcec2011-04-22 18:12:22 +08009257 ei->delayed_node = NULL;
9258
chandan r9cc97d62012-07-04 12:48:07 +05309259 ei->i_otime.tv_sec = 0;
9260 ei->i_otime.tv_nsec = 0;
9261
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009262 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009263 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009264 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9265 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009266 ei->io_tree.track_uptodate = 1;
9267 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009268 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009270 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009271 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009273 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009275 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009276
9277 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009278}
9279
Josef Bacikaaedb552013-10-11 14:44:09 -04009280#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9281void btrfs_test_destroy_inode(struct inode *inode)
9282{
9283 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9284 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9285}
9286#endif
9287
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009288static void btrfs_i_callback(struct rcu_head *head)
9289{
9290 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009291 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9292}
9293
Chris Mason39279cc2007-06-12 06:35:45 -04009294void btrfs_destroy_inode(struct inode *inode)
9295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009296 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009297 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009298 struct btrfs_root *root = BTRFS_I(inode)->root;
9299
Al Virob3d9b7a2012-06-09 13:51:19 -04009300 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009301 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009302 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9303 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009304 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9305 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009306 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009307
Chris Mason5a3f23d2009-03-31 13:27:11 -04009308 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009309 * This can happen where we create an inode, but somebody else also
9310 * created the same inode and we need to destroy the one we already
9311 * created.
9312 */
9313 if (!root)
9314 goto free;
9315
Josef Bacik8a35d952012-05-23 14:26:42 -04009316 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9317 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009318 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009319 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009320 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009321 }
Josef Bacik7b128762008-07-24 12:17:14 -04009322
Chris Masond3977122009-01-05 21:25:51 -05009323 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009324 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9325 if (!ordered)
9326 break;
9327 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009328 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009329 "found ordered extent %llu %llu on inode cleanup",
9330 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009331 btrfs_remove_ordered_extent(inode, ordered);
9332 btrfs_put_ordered_extent(ordered);
9333 btrfs_put_ordered_extent(ordered);
9334 }
9335 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009336 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009337 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009338 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009339free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009340 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009341}
9342
Al Viro45321ac2010-06-07 13:43:19 -04009343int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009344{
9345 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009346
Naohiro Aota6379ef92013-06-06 09:56:34 +00009347 if (root == NULL)
9348 return 1;
9349
Liu Bofa6ac872013-02-20 14:10:23 +00009350 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009351 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009352 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009353 else
Al Viro45321ac2010-06-07 13:43:19 -04009354 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009355}
9356
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009357static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009358{
9359 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9360
9361 inode_init_once(&ei->vfs_inode);
9362}
9363
9364void btrfs_destroy_cachep(void)
9365{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009366 /*
9367 * Make sure all delayed rcu free inodes are flushed before we
9368 * destroy cache.
9369 */
9370 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009371 kmem_cache_destroy(btrfs_inode_cachep);
9372 kmem_cache_destroy(btrfs_trans_handle_cachep);
9373 kmem_cache_destroy(btrfs_transaction_cachep);
9374 kmem_cache_destroy(btrfs_path_cachep);
9375 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009376}
9377
9378int btrfs_init_cachep(void)
9379{
David Sterba837e1972012-09-07 03:00:48 -06009380 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009381 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009382 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9383 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009384 if (!btrfs_inode_cachep)
9385 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009386
David Sterba837e1972012-09-07 03:00:48 -06009387 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009388 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009389 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009390 if (!btrfs_trans_handle_cachep)
9391 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392
David Sterba837e1972012-09-07 03:00:48 -06009393 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009394 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009395 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009396 if (!btrfs_transaction_cachep)
9397 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009398
David Sterba837e1972012-09-07 03:00:48 -06009399 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009400 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009401 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009402 if (!btrfs_path_cachep)
9403 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009404
David Sterba837e1972012-09-07 03:00:48 -06009405 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009406 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009407 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009408 if (!btrfs_free_space_cachep)
9409 goto fail;
9410
Chris Mason39279cc2007-06-12 06:35:45 -04009411 return 0;
9412fail:
9413 btrfs_destroy_cachep();
9414 return -ENOMEM;
9415}
9416
9417static int btrfs_getattr(struct vfsmount *mnt,
9418 struct dentry *dentry, struct kstat *stat)
9419{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009420 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009421 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009422 u32 blocksize = inode->i_sb->s_blocksize;
9423
Chris Mason39279cc2007-06-12 06:35:45 -04009424 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009425 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009426
9427 spin_lock(&BTRFS_I(inode)->lock);
9428 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9429 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009430 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009431 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009432 return 0;
9433}
9434
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009435static int btrfs_rename_exchange(struct inode *old_dir,
9436 struct dentry *old_dentry,
9437 struct inode *new_dir,
9438 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009439{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009440 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009441 struct btrfs_trans_handle *trans;
9442 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009443 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009444 struct inode *new_inode = new_dentry->d_inode;
9445 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009446 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009448 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9449 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 u64 old_idx = 0;
9451 u64 new_idx = 0;
9452 u64 root_objectid;
9453 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009454 bool root_log_pinned = false;
9455 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009456
9457 /* we only allow rename subvolume link between subvolumes */
9458 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9459 return -EXDEV;
9460
9461 /* close the race window with snapshot create/destroy ioctl */
9462 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009463 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009464 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009465 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466
9467 /*
9468 * We want to reserve the absolute worst case amount of items. So if
9469 * both inodes are subvols and we need to unlink them then that would
9470 * require 4 item modifications, but if they are both normal inodes it
9471 * would require 5 item modifications, so we'll assume their normal
9472 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9473 * should cover the worst case number of items we'll modify.
9474 */
9475 trans = btrfs_start_transaction(root, 12);
9476 if (IS_ERR(trans)) {
9477 ret = PTR_ERR(trans);
9478 goto out_notrans;
9479 }
9480
9481 /*
9482 * We need to find a free sequence number both in the source and
9483 * in the destination directory for the exchange.
9484 */
9485 ret = btrfs_set_inode_index(new_dir, &old_idx);
9486 if (ret)
9487 goto out_fail;
9488 ret = btrfs_set_inode_index(old_dir, &new_idx);
9489 if (ret)
9490 goto out_fail;
9491
9492 BTRFS_I(old_inode)->dir_index = 0ULL;
9493 BTRFS_I(new_inode)->dir_index = 0ULL;
9494
9495 /* Reference for the source. */
9496 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9497 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009498 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009500 btrfs_pin_log_trans(root);
9501 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 ret = btrfs_insert_inode_ref(trans, dest,
9503 new_dentry->d_name.name,
9504 new_dentry->d_name.len,
9505 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009506 btrfs_ino(BTRFS_I(new_dir)),
9507 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 if (ret)
9509 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510 }
9511
9512 /* And now for the dest. */
9513 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9514 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009515 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009517 btrfs_pin_log_trans(dest);
9518 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 ret = btrfs_insert_inode_ref(trans, root,
9520 old_dentry->d_name.name,
9521 old_dentry->d_name.len,
9522 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009523 btrfs_ino(BTRFS_I(old_dir)),
9524 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 if (ret)
9526 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 }
9528
9529 /* Update inode version and ctime/mtime. */
9530 inode_inc_iversion(old_dir);
9531 inode_inc_iversion(new_dir);
9532 inode_inc_iversion(old_inode);
9533 inode_inc_iversion(new_inode);
9534 old_dir->i_ctime = old_dir->i_mtime = ctime;
9535 new_dir->i_ctime = new_dir->i_mtime = ctime;
9536 old_inode->i_ctime = ctime;
9537 new_inode->i_ctime = ctime;
9538
9539 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009540 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9541 BTRFS_I(old_inode), 1);
9542 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9543 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009544 }
9545
9546 /* src is a subvolume */
9547 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9548 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9549 ret = btrfs_unlink_subvol(trans, root, old_dir,
9550 root_objectid,
9551 old_dentry->d_name.name,
9552 old_dentry->d_name.len);
9553 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009554 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9555 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 old_dentry->d_name.name,
9557 old_dentry->d_name.len);
9558 if (!ret)
9559 ret = btrfs_update_inode(trans, root, old_inode);
9560 }
9561 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009562 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 goto out_fail;
9564 }
9565
9566 /* dest is a subvolume */
9567 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9568 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9569 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9570 root_objectid,
9571 new_dentry->d_name.name,
9572 new_dentry->d_name.len);
9573 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009574 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9575 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 new_dentry->d_name.name,
9577 new_dentry->d_name.len);
9578 if (!ret)
9579 ret = btrfs_update_inode(trans, dest, new_inode);
9580 }
9581 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009582 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 goto out_fail;
9584 }
9585
9586 ret = btrfs_add_link(trans, new_dir, old_inode,
9587 new_dentry->d_name.name,
9588 new_dentry->d_name.len, 0, old_idx);
9589 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009590 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 goto out_fail;
9592 }
9593
9594 ret = btrfs_add_link(trans, old_dir, new_inode,
9595 old_dentry->d_name.name,
9596 old_dentry->d_name.len, 0, new_idx);
9597 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009598 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 goto out_fail;
9600 }
9601
9602 if (old_inode->i_nlink == 1)
9603 BTRFS_I(old_inode)->dir_index = old_idx;
9604 if (new_inode->i_nlink == 1)
9605 BTRFS_I(new_inode)->dir_index = new_idx;
9606
Filipe Manana86e8aa02016-05-05 02:02:27 +01009607 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009609 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9610 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009614 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009616 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9617 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 }
9621out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009622 /*
9623 * If we have pinned a log and an error happened, we unpin tasks
9624 * trying to sync the log and force them to fallback to a transaction
9625 * commit if the log currently contains any of the inodes involved in
9626 * this rename operation (to ensure we do not persist a log with an
9627 * inconsistent state for any of these inodes or leading to any
9628 * inconsistencies when replayed). If the transaction was aborted, the
9629 * abortion reason is propagated to userspace when attempting to commit
9630 * the transaction. If the log does not contain any of these inodes, we
9631 * allow the tasks to sync it.
9632 */
9633 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009634 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9635 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9636 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009637 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009638 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009639 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009640
9641 if (root_log_pinned) {
9642 btrfs_end_log_trans(root);
9643 root_log_pinned = false;
9644 }
9645 if (dest_log_pinned) {
9646 btrfs_end_log_trans(dest);
9647 dest_log_pinned = false;
9648 }
9649 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009650 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651out_notrans:
9652 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009653 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009655 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656
9657 return ret;
9658}
9659
9660static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9661 struct btrfs_root *root,
9662 struct inode *dir,
9663 struct dentry *dentry)
9664{
9665 int ret;
9666 struct inode *inode;
9667 u64 objectid;
9668 u64 index;
9669
9670 ret = btrfs_find_free_ino(root, &objectid);
9671 if (ret)
9672 return ret;
9673
9674 inode = btrfs_new_inode(trans, root, dir,
9675 dentry->d_name.name,
9676 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009677 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 objectid,
9679 S_IFCHR | WHITEOUT_MODE,
9680 &index);
9681
9682 if (IS_ERR(inode)) {
9683 ret = PTR_ERR(inode);
9684 return ret;
9685 }
9686
9687 inode->i_op = &btrfs_special_inode_operations;
9688 init_special_inode(inode, inode->i_mode,
9689 WHITEOUT_DEV);
9690
9691 ret = btrfs_init_inode_security(trans, inode, dir,
9692 &dentry->d_name);
9693 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009694 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695
9696 ret = btrfs_add_nondir(trans, dir, dentry,
9697 inode, 0, index);
9698 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009699 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700
9701 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009702out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009704 if (ret)
9705 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 iput(inode);
9707
Filipe Mananac9901612016-05-05 01:41:57 +01009708 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709}
9710
Chris Mason39279cc2007-06-12 06:35:45 -04009711static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 struct inode *new_dir, struct dentry *new_dentry,
9713 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009714{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009716 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009717 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009718 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9719 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009720 struct inode *new_inode = d_inode(new_dentry);
9721 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009722 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009723 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009724 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009725 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009726 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009727
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009728 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009729 return -EPERM;
9730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009731 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009732 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009733 return -EXDEV;
9734
Li Zefan33345d012011-04-20 10:31:50 +08009735 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009736 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009737 return -ENOTEMPTY;
9738
Chris Mason39279cc2007-06-12 06:35:45 -04009739 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009741 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009742
9743
9744 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009745 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009746 new_dentry->d_name.name,
9747 new_dentry->d_name.len);
9748
9749 if (ret) {
9750 if (ret == -EEXIST) {
9751 /* we shouldn't get
9752 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309753 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009754 return ret;
9755 }
9756 } else {
9757 /* maybe -EOVERFLOW */
9758 return ret;
9759 }
9760 }
9761 ret = 0;
9762
Chris Mason5a3f23d2009-03-31 13:27:11 -04009763 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009764 * we're using rename to replace one file with another. Start IO on it
9765 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 */
Chris Mason8d875f92014-08-12 10:47:42 -07009767 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009768 filemap_flush(old_inode->i_mapping);
9769
Yan, Zheng76dda932009-09-21 16:00:26 -04009770 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009771 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009772 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009773 /*
9774 * We want to reserve the absolute worst case amount of items. So if
9775 * both inodes are subvols and we need to unlink them then that would
9776 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009778 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9779 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009780 * If our rename has the whiteout flag, we need more 5 units for the
9781 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9782 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009783 */
Filipe Manana5062af32016-05-05 10:26:26 +01009784 trans_num_items = 11;
9785 if (flags & RENAME_WHITEOUT)
9786 trans_num_items += 5;
9787 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009788 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 ret = PTR_ERR(trans);
9790 goto out_notrans;
9791 }
Chris Mason5f39d392007-10-15 16:14:19 -04009792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 if (dest != root)
9794 btrfs_record_root_in_trans(trans, dest);
9795
Yan, Zhenga5719522009-09-24 09:17:31 -04009796 ret = btrfs_set_inode_index(new_dir, &index);
9797 if (ret)
9798 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009799
Miao Xie67de1172013-12-26 13:07:06 +08009800 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009801 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009803 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009805 btrfs_pin_log_trans(root);
9806 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009807 ret = btrfs_insert_inode_ref(trans, dest,
9808 new_dentry->d_name.name,
9809 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009810 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009811 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009812 if (ret)
9813 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009814 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009815
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009816 inode_inc_iversion(old_dir);
9817 inode_inc_iversion(new_dir);
9818 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009819 old_dir->i_ctime = old_dir->i_mtime =
9820 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009821 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009822
Chris Mason12fcfd22009-03-24 10:24:20 -04009823 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009824 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9825 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009826
Li Zefan33345d012011-04-20 10:31:50 +08009827 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009828 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9829 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9830 old_dentry->d_name.name,
9831 old_dentry->d_name.len);
9832 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009833 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9834 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009835 old_dentry->d_name.name,
9836 old_dentry->d_name.len);
9837 if (!ret)
9838 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009839 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009840 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009841 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009842 goto out_fail;
9843 }
Chris Mason39279cc2007-06-12 06:35:45 -04009844
9845 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009846 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009847 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009848 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009849 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9850 root_objectid = BTRFS_I(new_inode)->location.objectid;
9851 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9852 root_objectid,
9853 new_dentry->d_name.name,
9854 new_dentry->d_name.len);
9855 BUG_ON(new_inode->i_nlink == 0);
9856 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009857 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9858 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009859 new_dentry->d_name.name,
9860 new_dentry->d_name.len);
9861 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009862 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009863 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009864 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009865 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009866 goto out_fail;
9867 }
Chris Mason39279cc2007-06-12 06:35:45 -04009868 }
Josef Bacikaec74772008-07-24 12:12:38 -04009869
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009870 ret = btrfs_add_link(trans, new_dir, old_inode,
9871 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009872 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009873 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009874 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 goto out_fail;
9876 }
Chris Mason39279cc2007-06-12 06:35:45 -04009877
Miao Xie67de1172013-12-26 13:07:06 +08009878 if (old_inode->i_nlink == 1)
9879 BTRFS_I(old_inode)->dir_index = index;
9880
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009881 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009882 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009883
David Sterbaf85b7372017-01-20 14:54:07 +01009884 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9885 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009887 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009888 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009889
9890 if (flags & RENAME_WHITEOUT) {
9891 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9892 old_dentry);
9893
9894 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009895 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 goto out_fail;
9897 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009898 }
Chris Mason39279cc2007-06-12 06:35:45 -04009899out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009900 /*
9901 * If we have pinned the log and an error happened, we unpin tasks
9902 * trying to sync the log and force them to fallback to a transaction
9903 * commit if the log currently contains any of the inodes involved in
9904 * this rename operation (to ensure we do not persist a log with an
9905 * inconsistent state for any of these inodes or leading to any
9906 * inconsistencies when replayed). If the transaction was aborted, the
9907 * abortion reason is propagated to userspace when attempting to commit
9908 * the transaction. If the log does not contain any of these inodes, we
9909 * allow the tasks to sync it.
9910 */
9911 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009912 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9913 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9914 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009915 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009916 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009917 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009918
9919 btrfs_end_log_trans(root);
9920 log_pinned = false;
9921 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009922 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009923out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009925 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009926
Chris Mason39279cc2007-06-12 06:35:45 -04009927 return ret;
9928}
9929
Miklos Szeredi80ace852014-07-23 15:15:32 +02009930static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9931 struct inode *new_dir, struct dentry *new_dentry,
9932 unsigned int flags)
9933{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009934 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009935 return -EINVAL;
9936
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009937 if (flags & RENAME_EXCHANGE)
9938 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9939 new_dentry);
9940
9941 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009942}
9943
Miao Xie8ccf6f192012-10-25 09:28:04 +00009944static void btrfs_run_delalloc_work(struct btrfs_work *work)
9945{
9946 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009947 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009948
9949 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9950 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009951 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009952 filemap_flush(inode->i_mapping);
9953 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9954 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009955 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009956
9957 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009958 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009959 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009960 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961 complete(&delalloc_work->completion);
9962}
9963
9964struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009965 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009966{
9967 struct btrfs_delalloc_work *work;
9968
David Sterba100d5702015-12-08 14:39:32 +01009969 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009970 if (!work)
9971 return NULL;
9972
9973 init_completion(&work->completion);
9974 INIT_LIST_HEAD(&work->list);
9975 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009976 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009977 WARN_ON_ONCE(!inode);
9978 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9979 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009980
9981 return work;
9982}
9983
9984void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9985{
9986 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009987 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009988}
9989
Chris Masond352ac62008-09-29 15:18:18 -04009990/*
9991 * some fairly slow code that needs optimization. This walks the list
9992 * of all the inodes with pending delalloc and forces them to disk.
9993 */
Miao Xie6c255e62014-03-06 13:55:01 +08009994static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9995 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009996{
Chris Masonea8c2812008-08-04 23:17:27 -04009997 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009998 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999 struct btrfs_delalloc_work *work, *next;
10000 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010001 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010003
Miao Xie8ccf6f192012-10-25 09:28:04 +000010004 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010005 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010006
Miao Xie573bfb72014-03-06 13:55:03 +080010007 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010008 spin_lock(&root->delalloc_lock);
10009 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010010 while (!list_empty(&splice)) {
10011 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010012 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010013
Miao Xieeb73c1b2013-05-15 07:48:22 +000010014 list_move_tail(&binode->delalloc_inodes,
10015 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010016 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010017 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010019 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010020 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010022
David Sterba651d4942015-11-27 19:24:16 +010010023 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010024 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010025 if (delay_iput)
10026 btrfs_add_delayed_iput(inode);
10027 else
10028 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010029 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010030 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010031 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010032 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010033 btrfs_queue_work(root->fs_info->flush_workers,
10034 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010035 ret++;
10036 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010037 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010038 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010040 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010041 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010042
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010043out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010044 list_for_each_entry_safe(work, next, &works, list) {
10045 list_del_init(&work->list);
10046 btrfs_wait_and_free_delalloc_work(work);
10047 }
10048
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 if (!list_empty_careful(&splice)) {
10050 spin_lock(&root->delalloc_lock);
10051 list_splice_tail(&splice, &root->delalloc_inodes);
10052 spin_unlock(&root->delalloc_lock);
10053 }
Miao Xie573bfb72014-03-06 13:55:03 +080010054 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010055 return ret;
10056}
10057
10058int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10059{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010060 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010061 int ret;
10062
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010063 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 return -EROFS;
10065
Miao Xie6c255e62014-03-06 13:55:01 +080010066 ret = __start_delalloc_inodes(root, delay_iput, -1);
10067 if (ret > 0)
10068 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 /*
10070 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010071 * we have to make sure the IO is actually started and that
10072 * ordered extents get created before we return
10073 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 atomic_inc(&fs_info->async_submit_draining);
10075 while (atomic_read(&fs_info->nr_async_submits) ||
10076 atomic_read(&fs_info->async_delalloc_pages)) {
10077 wait_event(fs_info->async_submit_wait,
10078 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10079 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010080 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010081 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
Miao Xie6c255e62014-03-06 13:55:01 +080010085int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10086 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087{
10088 struct btrfs_root *root;
10089 struct list_head splice;
10090 int ret;
10091
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010092 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 return -EROFS;
10094
10095 INIT_LIST_HEAD(&splice);
10096
Miao Xie573bfb72014-03-06 13:55:03 +080010097 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 spin_lock(&fs_info->delalloc_root_lock);
10099 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010100 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 root = list_first_entry(&splice, struct btrfs_root,
10102 delalloc_root);
10103 root = btrfs_grab_fs_root(root);
10104 BUG_ON(!root);
10105 list_move_tail(&root->delalloc_root,
10106 &fs_info->delalloc_roots);
10107 spin_unlock(&fs_info->delalloc_root_lock);
10108
Miao Xie6c255e62014-03-06 13:55:01 +080010109 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010111 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 goto out;
10113
Miao Xie6c255e62014-03-06 13:55:01 +080010114 if (nr != -1) {
10115 nr -= ret;
10116 WARN_ON(nr < 0);
10117 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 spin_lock(&fs_info->delalloc_root_lock);
10119 }
10120 spin_unlock(&fs_info->delalloc_root_lock);
10121
Miao Xie6c255e62014-03-06 13:55:01 +080010122 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 atomic_inc(&fs_info->async_submit_draining);
10124 while (atomic_read(&fs_info->nr_async_submits) ||
10125 atomic_read(&fs_info->async_delalloc_pages)) {
10126 wait_event(fs_info->async_submit_wait,
10127 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10128 atomic_read(&fs_info->async_delalloc_pages) == 0));
10129 }
10130 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010131out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_lock(&fs_info->delalloc_root_lock);
10134 list_splice_tail(&splice, &fs_info->delalloc_roots);
10135 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 }
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010139}
10140
Chris Mason39279cc2007-06-12 06:35:45 -040010141static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10142 const char *symname)
10143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010144 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010145 struct btrfs_trans_handle *trans;
10146 struct btrfs_root *root = BTRFS_I(dir)->root;
10147 struct btrfs_path *path;
10148 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010149 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010150 int err;
10151 int drop_inode = 0;
10152 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010153 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010154 int name_len;
10155 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010156 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010157 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010158 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010159
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010160 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010161 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010162 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010163
Josef Bacik9ed74f22009-09-11 16:12:44 -040010164 /*
10165 * 2 items for inode item and ref
10166 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010167 * 1 item for updating parent inode item
10168 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010169 * 1 item for xattr if selinux is on
10170 */
Filipe Manana9269d122015-12-31 18:16:29 +000010171 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010172 if (IS_ERR(trans))
10173 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010174
Li Zefan581bb052011-04-20 10:06:11 +080010175 err = btrfs_find_free_ino(root, &objectid);
10176 if (err)
10177 goto out_unlock;
10178
Josef Bacikaec74772008-07-24 12:12:38 -040010179 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010180 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10181 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010182 if (IS_ERR(inode)) {
10183 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010184 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010185 }
Chris Mason39279cc2007-06-12 06:35:45 -040010186
Casey Schauflerad19db72011-12-15 10:09:07 -050010187 /*
10188 * If the active LSM wants to access the inode during
10189 * d_instantiate it needs these. Smack checks to see
10190 * if the filesystem supports xattrs by looking at the
10191 * ops vector.
10192 */
10193 inode->i_fop = &btrfs_file_operations;
10194 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010196 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10197
10198 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10199 if (err)
10200 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010201
Chris Mason39279cc2007-06-12 06:35:45 -040010202 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010203 if (!path) {
10204 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010205 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010206 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010207 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010208 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010209 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010210 datasize = btrfs_file_extent_calc_inline_size(name_len);
10211 err = btrfs_insert_empty_item(trans, root, path, &key,
10212 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010213 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010214 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010215 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010216 }
Chris Mason5f39d392007-10-15 16:14:19 -040010217 leaf = path->nodes[0];
10218 ei = btrfs_item_ptr(leaf, path->slots[0],
10219 struct btrfs_file_extent_item);
10220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10221 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010222 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010223 btrfs_set_file_extent_encryption(leaf, ei, 0);
10224 btrfs_set_file_extent_compression(leaf, ei, 0);
10225 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10226 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10227
Chris Mason39279cc2007-06-12 06:35:45 -040010228 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010229 write_extent_buffer(leaf, symname, ptr, name_len);
10230 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010231 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010232
Chris Mason39279cc2007-06-12 06:35:45 -040010233 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010234 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010235 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010236 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010237 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010238 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010239 /*
10240 * Last step, add directory indexes for our symlink inode. This is the
10241 * last step to avoid extra cleanup of these indexes if an error happens
10242 * elsewhere above.
10243 */
10244 if (!err)
10245 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010246 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010247 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010248 goto out_unlock_inode;
10249 }
10250
10251 unlock_new_inode(inode);
10252 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010253
10254out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010255 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010256 if (drop_inode) {
10257 inode_dec_link_count(inode);
10258 iput(inode);
10259 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010260 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010261 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010262
10263out_unlock_inode:
10264 drop_inode = 1;
10265 unlock_new_inode(inode);
10266 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010267}
Chris Mason16432982008-04-10 10:23:21 -040010268
Josef Bacik0af3d002010-06-21 14:48:16 -040010269static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10270 u64 start, u64 num_bytes, u64 min_size,
10271 loff_t actual_len, u64 *alloc_hint,
10272 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010275 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10276 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010277 struct btrfs_root *root = BTRFS_I(inode)->root;
10278 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010279 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010280 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010281 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010282 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010283 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010284 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010285 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010286
Josef Bacik0af3d002010-06-21 14:48:16 -040010287 if (trans)
10288 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010289 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010290 if (own_trans) {
10291 trans = btrfs_start_transaction(root, 3);
10292 if (IS_ERR(trans)) {
10293 ret = PTR_ERR(trans);
10294 break;
10295 }
Yan Zhengd899e052008-10-30 14:25:28 -040010296 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010297
Byongho Leeee221842015-12-15 01:42:10 +090010298 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010299 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010300 /*
10301 * If we are severely fragmented we could end up with really
10302 * small allocations, so if the allocator is returning small
10303 * chunks lets make its job easier by only searching for those
10304 * sized chunks.
10305 */
10306 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010307 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10308 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010309 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010310 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010311 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010312 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010313 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010315
Josef Bacik0b670dc2015-09-23 17:11:16 -040010316 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010317 ret = insert_reserved_file_extent(trans, inode,
10318 cur_offset, ins.objectid,
10319 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010320 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010321 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010322 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010323 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010324 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010325 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010326 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010327 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010328 break;
10329 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010330
Chris Masona1ed8352009-09-11 12:27:37 -040010331 btrfs_drop_extent_cache(inode, cur_offset,
10332 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010333
Josef Bacik5dc562c2012-08-17 13:14:17 -040010334 em = alloc_extent_map();
10335 if (!em) {
10336 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10337 &BTRFS_I(inode)->runtime_flags);
10338 goto next;
10339 }
10340
10341 em->start = cur_offset;
10342 em->orig_start = cur_offset;
10343 em->len = ins.offset;
10344 em->block_start = ins.objectid;
10345 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010346 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010347 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010348 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010349 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10350 em->generation = trans->transid;
10351
10352 while (1) {
10353 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010354 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010355 write_unlock(&em_tree->lock);
10356 if (ret != -EEXIST)
10357 break;
10358 btrfs_drop_extent_cache(inode, cur_offset,
10359 cur_offset + ins.offset - 1,
10360 0);
10361 }
10362 free_extent_map(em);
10363next:
Yan Zhengd899e052008-10-30 14:25:28 -040010364 num_bytes -= ins.offset;
10365 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010366 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010367
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010368 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010369 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010370 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010371 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010372 (actual_len > inode->i_size) &&
10373 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010374 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010375 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010376 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010377 i_size = cur_offset;
10378 i_size_write(inode, i_size);
10379 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010380 }
10381
Yan Zhengd899e052008-10-30 14:25:28 -040010382 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010383
10384 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010385 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010386 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010387 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388 break;
10389 }
Yan Zhengd899e052008-10-30 14:25:28 -040010390
Josef Bacik0af3d002010-06-21 14:48:16 -040010391 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010392 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010393 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010394 if (cur_offset < end)
10395 btrfs_free_reserved_data_space(inode, cur_offset,
10396 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010397 return ret;
10398}
10399
Josef Bacik0af3d002010-06-21 14:48:16 -040010400int btrfs_prealloc_file_range(struct inode *inode, int mode,
10401 u64 start, u64 num_bytes, u64 min_size,
10402 loff_t actual_len, u64 *alloc_hint)
10403{
10404 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10405 min_size, actual_len, alloc_hint,
10406 NULL);
10407}
10408
10409int btrfs_prealloc_file_range_trans(struct inode *inode,
10410 struct btrfs_trans_handle *trans, int mode,
10411 u64 start, u64 num_bytes, u64 min_size,
10412 loff_t actual_len, u64 *alloc_hint)
10413{
10414 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10415 min_size, actual_len, alloc_hint, trans);
10416}
10417
Chris Masone6dcd2d2008-07-17 12:53:50 -040010418static int btrfs_set_page_dirty(struct page *page)
10419{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010420 return __set_page_dirty_nobuffers(page);
10421}
10422
Al Viro10556cb2011-06-20 19:28:19 -040010423static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010424{
Li Zefanb83cc962010-12-20 16:04:08 +080010425 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010426 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010427
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010428 if (mask & MAY_WRITE &&
10429 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10430 if (btrfs_root_readonly(root))
10431 return -EROFS;
10432 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10433 return -EACCES;
10434 }
Al Viro2830ba72011-06-20 19:16:29 -040010435 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010436}
Chris Mason39279cc2007-06-12 06:35:45 -040010437
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010438static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10439{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010440 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010441 struct btrfs_trans_handle *trans;
10442 struct btrfs_root *root = BTRFS_I(dir)->root;
10443 struct inode *inode = NULL;
10444 u64 objectid;
10445 u64 index;
10446 int ret = 0;
10447
10448 /*
10449 * 5 units required for adding orphan entry
10450 */
10451 trans = btrfs_start_transaction(root, 5);
10452 if (IS_ERR(trans))
10453 return PTR_ERR(trans);
10454
10455 ret = btrfs_find_free_ino(root, &objectid);
10456 if (ret)
10457 goto out;
10458
10459 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010460 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010461 if (IS_ERR(inode)) {
10462 ret = PTR_ERR(inode);
10463 inode = NULL;
10464 goto out;
10465 }
10466
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010467 inode->i_fop = &btrfs_file_operations;
10468 inode->i_op = &btrfs_file_inode_operations;
10469
10470 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010471 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10472
Chris Masonb0d5d102014-09-08 13:08:51 -070010473 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10474 if (ret)
10475 goto out_inode;
10476
10477 ret = btrfs_update_inode(trans, root, inode);
10478 if (ret)
10479 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010480 ret = btrfs_orphan_add(trans, inode);
10481 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010482 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010483
Filipe Manana5762b5c2014-08-01 00:10:32 +010010484 /*
10485 * We set number of links to 0 in btrfs_new_inode(), and here we set
10486 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10487 * through:
10488 *
10489 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10490 */
10491 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010492 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010493 d_tmpfile(dentry, inode);
10494 mark_inode_dirty(inode);
10495
10496out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010497 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498 if (ret)
10499 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010500 btrfs_balance_delayed_items(fs_info);
10501 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010502 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010503
10504out_inode:
10505 unlock_new_inode(inode);
10506 goto out;
10507
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010508}
10509
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010510static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010511 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010512 .lookup = btrfs_lookup,
10513 .create = btrfs_create,
10514 .unlink = btrfs_unlink,
10515 .link = btrfs_link,
10516 .mkdir = btrfs_mkdir,
10517 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010518 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010519 .symlink = btrfs_symlink,
10520 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010521 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010522 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010523 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010524 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010525 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010526 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010527 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010528};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010529static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010530 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010531 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010532 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010533};
Yan, Zheng76dda932009-09-21 16:00:26 -040010534
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010535static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010536 .llseek = generic_file_llseek,
10537 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010538 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010539 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010540#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010541 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010542#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010543 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010544 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010545};
10546
David Sterba20e55062015-11-19 11:42:28 +010010547static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010548 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010549 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010550 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010551 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010552 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010553 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010554 .set_bit_hook = btrfs_set_bit_hook,
10555 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010556 .merge_extent_hook = btrfs_merge_extent_hook,
10557 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010558};
10559
Chris Mason35054392009-01-21 13:11:13 -050010560/*
10561 * btrfs doesn't support the bmap operation because swapfiles
10562 * use bmap to make a mapping of extents in the file. They assume
10563 * these extents won't change over the life of the file and they
10564 * use the bmap result to do IO directly to the drive.
10565 *
10566 * the btrfs bmap call would return logical addresses that aren't
10567 * suitable for IO and they also will change frequently as COW
10568 * operations happen. So, swapfile + btrfs == corruption.
10569 *
10570 * For now we're avoiding this by dropping bmap.
10571 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010572static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010573 .readpage = btrfs_readpage,
10574 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010575 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010576 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010577 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010578 .invalidatepage = btrfs_invalidatepage,
10579 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010580 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010581 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010582};
10583
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010584static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010585 .readpage = btrfs_readpage,
10586 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010587 .invalidatepage = btrfs_invalidatepage,
10588 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010589};
10590
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010591static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010592 .getattr = btrfs_getattr,
10593 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010594 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010595 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010596 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010597 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010598 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010599 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010600};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010601static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010602 .getattr = btrfs_getattr,
10603 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010604 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010605 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010606 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010607 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010608 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010609};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010610static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010611 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010612 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010613 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010614 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010615 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010616 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010617};
Yan, Zheng76dda932009-09-21 16:00:26 -040010618
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010619const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010620 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010621 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010622};