blob: bfe04afa62771d5f201e76bef6f0f0ec7ec5d088 [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
Qu Wenruo524272602017-03-08 10:25:52 +0800118static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
121
122/*
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
125 *
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
134 */
135static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
136 const u64 offset,
137 const u64 bytes)
138{
139 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
140 bytes - PAGE_SIZE, false);
141}
142
Eric Sandeen48a3b632013-04-25 20:41:01 +0000143static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400144
Josef Bacik6a3891c2015-03-16 17:38:52 -0400145#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
146void btrfs_test_inode_set_ops(struct inode *inode)
147{
148 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
149}
150#endif
151
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000152static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500153 struct inode *inode, struct inode *dir,
154 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500155{
156 int err;
157
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000158 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500159 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500160 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500161 return err;
162}
163
Chris Masond352ac62008-09-29 15:18:18 -0400164/*
Chris Masonc8b97812008-10-29 14:49:59 -0400165 * this does all the hard work for inserting an inline extent into
166 * the btree. The caller should have done a btrfs_drop_extents so that
167 * no overlapping inline items exist in the btree
168 */
Chris Mason40f76582014-05-21 13:35:51 -0700169static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000170 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct btrfs_root *root, struct inode *inode,
172 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000173 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct page **compressed_pages)
175{
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct extent_buffer *leaf;
177 struct page *page = NULL;
178 char *kaddr;
179 unsigned long ptr;
180 struct btrfs_file_extent_item *ei;
181 int err = 0;
182 int ret;
183 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400184 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Li Zefanfe3f5662011-03-28 08:30:38 +0000186 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400187 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400188
Chris Masonc8b97812008-10-29 14:49:59 -0400189 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190
191 if (!extent_inserted) {
192 struct btrfs_key key;
193 size_t datasize;
194
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200195 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200197 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198
199 datasize = btrfs_file_extent_calc_inline_size(cur_size);
200 path->leave_spinning = 1;
201 ret = btrfs_insert_empty_item(trans, root, path, &key,
202 datasize);
203 if (ret) {
204 err = ret;
205 goto fail;
206 }
Chris Masonc8b97812008-10-29 14:49:59 -0400207 }
208 leaf = path->nodes[0];
209 ei = btrfs_item_ptr(leaf, path->slots[0],
210 struct btrfs_file_extent_item);
211 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
212 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
213 btrfs_set_file_extent_encryption(leaf, ei, 0);
214 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
215 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
216 ptr = btrfs_file_extent_inline_start(ei);
217
Li Zefan261507a02010-12-17 14:21:50 +0800218 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 struct page *cpage;
220 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500221 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400222 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500223 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300224 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
Cong Wang7ac687d2011-11-25 23:14:28 +0800226 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400227 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800228 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400229
230 i++;
231 ptr += cur_size;
232 compressed_size -= cur_size;
233 }
234 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800235 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 } else {
237 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300238 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800243 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 }
246 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000247 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400248
Yan, Zhengc2167752009-11-12 09:34:21 +0000249 /*
250 * we're an inline extent, so nobody can
251 * extend the file past i_size without locking
252 * a page we already have locked.
253 *
254 * We must do any isize and inode updates
255 * before we unlock the pages. Otherwise we
256 * could end up racing with unlink.
257 */
Chris Masonc8b97812008-10-29 14:49:59 -0400258 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000260
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400262fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400263 return err;
264}
265
266
267/*
268 * conditionally insert an inline extent into the file. This
269 * does the checks required to make sure the data is small enough
270 * to fit as an inline extent.
271 */
Josef Bacik00361582013-08-14 14:02:47 -0400272static noinline int cow_file_range_inline(struct btrfs_root *root,
273 struct inode *inode, u64 start,
274 u64 end, size_t compressed_size,
275 int compress_type,
276 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400278 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400279 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400280 u64 isize = i_size_read(inode);
281 u64 actual_end = min(end + 1, isize);
282 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400284 u64 data_len = inline_len;
285 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 struct btrfs_path *path;
287 int extent_inserted = 0;
288 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400289
290 if (compressed_size)
291 data_len = compressed_size;
292
293 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 actual_end > fs_info->sectorsize ||
295 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400296 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400298 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400300 return 1;
301 }
302
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 path = btrfs_alloc_path();
304 if (!path)
305 return -ENOMEM;
306
Josef Bacik00361582013-08-14 14:02:47 -0400307 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 if (IS_ERR(trans)) {
309 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400310 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400312 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400313
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 if (compressed_size && compressed_pages)
315 extent_item_size = btrfs_file_extent_calc_inline_size(
316 compressed_size);
317 else
318 extent_item_size = btrfs_file_extent_calc_inline_size(
319 inline_len);
320
321 ret = __btrfs_drop_extents(trans, root, inode, path,
322 start, aligned_end, NULL,
323 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400324 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400325 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400326 goto out;
327 }
Chris Masonc8b97812008-10-29 14:49:59 -0400328
329 if (isize > actual_end)
330 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000331 ret = insert_inline_extent(trans, path, extent_inserted,
332 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400333 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000334 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400335 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400336 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400337 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400338 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400339 ret = 1;
340 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100341 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400342
Josef Bacikbdc20e62013-02-28 13:23:38 -0500343 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200344 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200345 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400346out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800347 /*
348 * Don't forget to free the reserved space, as for inlined extent
349 * it won't count as data extent, free them directly here.
350 * And at reserve time, it's always aligned to page size, so
351 * just free one page here.
352 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300353 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000354 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400355 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400356 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400357}
358
Chris Mason771ed682008-11-06 22:02:51 -0500359struct async_extent {
360 u64 start;
361 u64 ram_size;
362 u64 compressed_size;
363 struct page **pages;
364 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800365 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head list;
367};
368
369struct async_cow {
370 struct inode *inode;
371 struct btrfs_root *root;
372 struct page *locked_page;
373 u64 start;
374 u64 end;
375 struct list_head extents;
376 struct btrfs_work work;
377};
378
379static noinline int add_async_extent(struct async_cow *cow,
380 u64 start, u64 ram_size,
381 u64 compressed_size,
382 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800383 unsigned long nr_pages,
384 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500385{
386 struct async_extent *async_extent;
387
388 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100389 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500390 async_extent->start = start;
391 async_extent->ram_size = ram_size;
392 async_extent->compressed_size = compressed_size;
393 async_extent->pages = pages;
394 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800395 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500396 list_add_tail(&async_extent->list, &cow->extents);
397 return 0;
398}
399
Wang Shilongf79707b2014-07-17 11:44:09 +0800400static inline int inode_need_compress(struct inode *inode)
401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400402 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800403
404 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400405 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800406 return 1;
407 /* bad compression ratios */
408 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
409 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800411 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
412 BTRFS_I(inode)->force_compress)
413 return 1;
414 return 0;
415}
416
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200417static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800418 u64 start, u64 end, u64 num_bytes, u64 small_write)
419{
420 /* If this is a small write inside eof, kick off a defrag */
421 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200422 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800423 btrfs_add_inode_defrag(NULL, inode);
424}
425
Chris Masonc8b97812008-10-29 14:49:59 -0400426/*
Chris Mason771ed682008-11-06 22:02:51 -0500427 * we create compressed extents in two phases. The first
428 * phase compresses a range of pages that have already been
429 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * This is done inside an ordered work queue, and the compression
432 * is spread across many cpus. The actual IO submission is step
433 * two, and the ordered work queue takes care of making sure that
434 * happens in the same order things were put onto the queue by
435 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400436 *
Chris Mason771ed682008-11-06 22:02:51 -0500437 * If this code finds it can't get good compression, it puts an
438 * entry onto the work queue to write the uncompressed bytes. This
439 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300440 * are written in the same order that the flusher thread sent them
441 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400442 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100443static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500444 struct page *locked_page,
445 u64 start, u64 end,
446 struct async_cow *async_cow,
447 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400449 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400450 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400451 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400452 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400453 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500454 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400455 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 struct page **pages = NULL;
457 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400458 unsigned long total_compressed = 0;
459 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400460 int i;
461 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400463 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400464
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200465 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
466 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400467
Chris Mason42dc7ba2008-12-15 11:44:56 -0500468 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400469again:
470 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300471 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100472 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
473 nr_pages = min_t(unsigned long, nr_pages,
474 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400475
Chris Masonf03d9301f2009-02-04 09:31:06 -0500476 /*
477 * we don't want to send crud past the end of i_size through
478 * compression, that's just a waste of CPU time. So, if the
479 * end of the file is before the start of our current
480 * requested range of bytes, we bail out to the uncompressed
481 * cleanup code that can deal with all of this.
482 *
483 * It isn't really the fastest way to fix things, but this is a
484 * very uncommon corner.
485 */
486 if (actual_end <= start)
487 goto cleanup_and_bail_uncompressed;
488
Chris Masonc8b97812008-10-29 14:49:59 -0400489 total_compressed = actual_end - start;
490
Shilong Wang4bcbb332014-10-07 18:44:35 -0400491 /*
492 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400493 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400494 */
495 if (total_compressed <= blocksize &&
496 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
497 goto cleanup_and_bail_uncompressed;
498
David Sterba069eac72017-02-14 19:36:54 +0100499 total_compressed = min_t(unsigned long, total_compressed,
500 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000501 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500502 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400503 total_in = 0;
504 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400505
Chris Mason771ed682008-11-06 22:02:51 -0500506 /*
507 * we do compression for mount -o compress and when the
508 * inode has not been flagged as nocompress. This flag can
509 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400510 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800511 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400512 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100513 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800514 if (!pages) {
515 /* just bail out to the uncompressed code */
516 goto cont;
517 }
Chris Mason179e29e2007-11-01 11:28:41 -0400518
Li Zefan261507a02010-12-17 14:21:50 +0800519 if (BTRFS_I(inode)->force_compress)
520 compress_type = BTRFS_I(inode)->force_compress;
521
Chris Mason4adaa612013-03-26 13:07:00 -0400522 /*
523 * we need to call clear_page_dirty_for_io on each
524 * page in the range. Otherwise applications with the file
525 * mmap'd can wander in and change the page contents while
526 * we are compressing them.
527 *
528 * If the compression fails for any reason, we set the pages
529 * dirty again later on.
530 */
531 extent_range_clear_dirty_for_io(inode, start, end);
532 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800533 ret = btrfs_compress_pages(compress_type,
534 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100535 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100536 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800537 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100538 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 if (!ret) {
541 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300542 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100543 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400544 char *kaddr;
545
546 /* zero the tail end of the last page, we might be
547 * sending it down to disk
548 */
549 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800550 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
555 will_compress = 1;
556 }
557 }
Li Zefan560f7d72011-09-08 10:22:01 +0800558cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (start == 0) {
560 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500561 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500563 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400564 */
Josef Bacik00361582013-08-14 14:02:47 -0400565 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800566 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500568 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400569 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000570 total_compressed,
571 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400574 unsigned long clear_flags = EXTENT_DELALLOC |
575 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 unsigned long page_error_op;
577
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100579 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400580
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 * inline extent creation worked or returned error,
583 * we don't need to create any more async work items.
584 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500585 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800586 extent_clear_unlock_delalloc(inode, start, end, end,
587 NULL, clear_flags,
588 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400589 PAGE_CLEAR_DIRTY |
590 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400592 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800593 btrfs_free_reserved_data_space_noquota(inode, start,
594 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400595 goto free_pages_out;
596 }
597 }
598
599 if (will_compress) {
600 /*
601 * we aren't doing an inline extent round the compressed size
602 * up to a block size boundary so the allocator does sane
603 * things
604 */
Qu Wenruofda28322013-02-26 08:10:22 +0000605 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400606
607 /*
608 * one last check to make sure the compression is really a
609 * win, compare the page count read with the blocks on disk
610 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300611 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400612 if (total_compressed >= total_in) {
613 will_compress = 0;
614 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400615 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700616 *num_added += 1;
617
618 /*
619 * The async work queues will take care of doing actual
620 * allocation on disk for these compressed pages, and
621 * will submit them to the elevator.
622 */
623 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100624 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700625 compress_type);
626
627 if (start + num_bytes < end) {
628 start += num_bytes;
629 pages = NULL;
630 cond_resched();
631 goto again;
632 }
633 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400634 }
635 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700636 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400637 /*
638 * the compression code ran but failed to make things smaller,
639 * free any pages it allocated and our page pointer array
640 */
David Sterba4d3a8002017-02-14 19:04:07 +0100641 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400642 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300643 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400644 }
645 kfree(pages);
646 pages = NULL;
647 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100648 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400649
650 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400651 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500652 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500653 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500654 }
Chris Masonc8b97812008-10-29 14:49:59 -0400655 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500656cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700657 /*
658 * No compression, but we still need to write the pages in the file
659 * we've been given so far. redirty the locked page if it corresponds
660 * to our extent and set things up for the async work queue to run
661 * cow_file_range to do the normal delalloc dance.
662 */
663 if (page_offset(locked_page) >= start &&
664 page_offset(locked_page) <= end)
665 __set_page_dirty_nobuffers(locked_page);
666 /* unlocked later on in the async handlers */
667
668 if (redirty)
669 extent_range_redirty_for_io(inode, start, end);
670 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
671 BTRFS_COMPRESS_NONE);
672 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500673
Filipe Mananac44f6492014-10-09 21:15:44 +0100674 return;
Chris Mason771ed682008-11-06 22:02:51 -0500675
676free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100677 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500678 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500680 }
Chris Masond3977122009-01-05 21:25:51 -0500681 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500682}
Chris Mason771ed682008-11-06 22:02:51 -0500683
Filipe Manana40ae8372014-10-06 22:14:24 +0100684static void free_async_extent_pages(struct async_extent *async_extent)
685{
686 int i;
687
688 if (!async_extent->pages)
689 return;
690
691 for (i = 0; i < async_extent->nr_pages; i++) {
692 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300693 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100694 }
695 kfree(async_extent->pages);
696 async_extent->nr_pages = 0;
697 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500698}
699
700/*
701 * phase two of compressed writeback. This is the ordered portion
702 * of the code, which only gets called in the order the work was
703 * queued. We walk all the async extents created by compress_file_range
704 * and send them down to the disk.
705 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100706static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500707 struct async_cow *async_cow)
708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500710 struct async_extent *async_extent;
711 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500712 struct btrfs_key ins;
713 struct extent_map *em;
714 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500715 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500716 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500717
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500718again:
Chris Masond3977122009-01-05 21:25:51 -0500719 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500720 async_extent = list_entry(async_cow->extents.next,
721 struct async_extent, list);
722 list_del(&async_extent->list);
723
724 io_tree = &BTRFS_I(inode)->io_tree;
725
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500726retry:
Chris Mason771ed682008-11-06 22:02:51 -0500727 /* did the compression code fall back to uncompressed IO? */
728 if (!async_extent->pages) {
729 int page_started = 0;
730 unsigned long nr_written = 0;
731
732 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000733 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100734 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500735
736 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500737 ret = cow_file_range(inode, async_cow->locked_page,
738 async_extent->start,
739 async_extent->start +
740 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800741 async_extent->start +
742 async_extent->ram_size - 1,
743 &page_started, &nr_written, 0,
744 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500745
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100746 /* JDM XXX */
747
Chris Mason771ed682008-11-06 22:02:51 -0500748 /*
749 * if page_started, cow_file_range inserted an
750 * inline extent and took care of all the unlocking
751 * and IO for us. Otherwise, we need to submit
752 * all those pages down to the drive.
753 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500755 extent_write_locked_range(io_tree,
756 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500757 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500758 async_extent->ram_size - 1,
759 btrfs_get_extent,
760 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 else if (ret)
762 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500763 kfree(async_extent);
764 cond_resched();
765 continue;
766 }
767
768 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100769 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500770
Wang Xiaoguang18513092016-07-25 15:51:40 +0800771 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500772 async_extent->compressed_size,
773 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800774 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500775 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100776 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400778 if (ret == -ENOSPC) {
779 unlock_extent(io_tree, async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800782
783 /*
784 * we need to redirty the pages if we decide to
785 * fallback to uncompressed IO, otherwise we
786 * will not submit these pages down to lower
787 * layers.
788 */
789 extent_range_redirty_for_io(inode,
790 async_extent->start,
791 async_extent->start +
792 async_extent->ram_size - 1);
793
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500796 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500797 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000798 /*
799 * here we're doing allocation and writeback of the
800 * compressed pages
801 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800802 em = create_io_em(inode, async_extent->start,
803 async_extent->ram_size, /* len */
804 async_extent->start, /* orig_start */
805 ins.objectid, /* block_start */
806 ins.offset, /* block_len */
807 ins.offset, /* orig_block_len */
808 async_extent->ram_size, /* ram_bytes */
809 async_extent->compress_type,
810 BTRFS_ORDERED_COMPRESSED);
811 if (IS_ERR(em))
812 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800814 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500815
Li Zefan261507a02010-12-17 14:21:50 +0800816 ret = btrfs_add_ordered_extent_compress(inode,
817 async_extent->start,
818 ins.objectid,
819 async_extent->ram_size,
820 ins.offset,
821 BTRFS_ORDERED_COMPRESSED,
822 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100823 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200824 btrfs_drop_extent_cache(BTRFS_I(inode),
825 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 async_extent->start +
827 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400830 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500831
Chris Mason771ed682008-11-06 22:02:51 -0500832 /*
833 * clear dirty, set writeback and unlock the pages.
834 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400835 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400836 async_extent->start +
837 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800838 async_extent->start +
839 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400840 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
841 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400842 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500843 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500844 async_extent->start,
845 async_extent->ram_size,
846 ins.objectid,
847 ins.offset, async_extent->pages,
848 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100849 if (ret) {
850 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
856 tree->ops->writepage_end_io_hook(p, start, end,
857 NULL, 0);
858 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800859 extent_clear_unlock_delalloc(inode, start, end, end,
860 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 PAGE_END_WRITEBACK |
862 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100863 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 }
Chris Mason771ed682008-11-06 22:02:51 -0500865 alloc_hint = ins.objectid + ins.offset;
866 kfree(async_extent);
867 cond_resched();
868 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100869 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400871 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400872 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 async_extent->start +
876 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 async_extent->start +
878 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400880 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
881 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100882 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
883 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100884 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500887}
888
Josef Bacik4b46fce2010-05-23 11:00:55 -0400889static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
890 u64 num_bytes)
891{
892 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
893 struct extent_map *em;
894 u64 alloc_hint = 0;
895
896 read_lock(&em_tree->lock);
897 em = search_extent_mapping(em_tree, start, num_bytes);
898 if (em) {
899 /*
900 * if block start isn't an actual block number then find the
901 * first block in this inode and use that as a hint. If that
902 * block is also bogus then just don't worry about it.
903 */
904 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
905 free_extent_map(em);
906 em = search_extent_mapping(em_tree, 0, 0);
907 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
908 alloc_hint = em->block_start;
909 if (em)
910 free_extent_map(em);
911 } else {
912 alloc_hint = em->block_start;
913 free_extent_map(em);
914 }
915 }
916 read_unlock(&em_tree->lock);
917
918 return alloc_hint;
919}
920
Chris Mason771ed682008-11-06 22:02:51 -0500921/*
922 * when extent_io.c finds a delayed allocation range in the file,
923 * the call backs end up in this code. The basic idea is to
924 * allocate extents on disk for the range, and create ordered data structs
925 * in ram to track those extents.
926 *
927 * locked_page is the page that writepage had locked already. We use
928 * it to make sure we don't do extra locks or unlocks.
929 *
930 * *page_started is set to one if we unlock locked_page and do everything
931 * required to start IO on it. It may be clean and already done with
932 * IO when we return.
933 */
Josef Bacik00361582013-08-14 14:02:47 -0400934static noinline int cow_file_range(struct inode *inode,
935 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800936 u64 start, u64 end, u64 delalloc_end,
937 int *page_started, unsigned long *nr_written,
938 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500939{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400941 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500942 u64 alloc_hint = 0;
943 u64 num_bytes;
944 unsigned long ram_size;
945 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000946 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400947 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500948 struct btrfs_key ins;
949 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000950 unsigned clear_bits;
951 unsigned long page_ops;
952 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500953 int ret = 0;
954
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200955 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500957 ret = -EINVAL;
958 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 }
Chris Mason771ed682008-11-06 22:02:51 -0500960
Qu Wenruofda28322013-02-26 08:10:22 +0000961 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500962 num_bytes = max(blocksize, num_bytes);
963 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500964
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200965 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400966
Chris Mason771ed682008-11-06 22:02:51 -0500967 if (start == 0) {
968 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800969 ret = cow_file_range_inline(root, inode, start, end, 0,
970 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500971 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800972 extent_clear_unlock_delalloc(inode, start, end,
973 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400974 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400975 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400976 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
977 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800978 btrfs_free_reserved_data_space_noquota(inode, start,
979 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500980 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300981 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500982 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500983 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100985 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500986 }
987 }
Chris Masonc8b97812008-10-29 14:49:59 -0400988
989 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400991
Josef Bacik4b46fce2010-05-23 11:00:55 -0400992 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200993 btrfs_drop_extent_cache(BTRFS_I(inode), start,
994 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400995
Chris Masond3977122009-01-05 21:25:51 -0500996 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +0000997 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800998 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400999 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001000 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001001 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001003 cur_alloc_size = ins.offset;
1004 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001005
Chris Mason771ed682008-11-06 22:02:51 -05001006 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001007 em = create_io_em(inode, start, ins.offset, /* len */
1008 start, /* orig_start */
1009 ins.objectid, /* block_start */
1010 ins.offset, /* block_len */
1011 ins.offset, /* orig_block_len */
1012 ram_size, /* ram_bytes */
1013 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001014 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001015 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001016 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001017 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001018
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001020 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001021 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001022 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001023
Yan Zheng17d217f2008-12-12 10:03:38 -05001024 if (root->root_key.objectid ==
1025 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1026 ret = btrfs_reloc_clone_csums(inode, start,
1027 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001028 /*
1029 * Only drop cache here, and process as normal.
1030 *
1031 * We must not allow extent_clear_unlock_delalloc()
1032 * at out_unlock label to free meta of this ordered
1033 * extent, as its meta should be freed by
1034 * btrfs_finish_ordered_io().
1035 *
1036 * So we must continue until @start is increased to
1037 * skip current ordered extent.
1038 */
Josef Bacik00361582013-08-14 14:02:47 -04001039 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001040 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1041 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 }
1043
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001044 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001045
Chris Masonc8b97812008-10-29 14:49:59 -04001046 /* we're not doing compressed IO, don't unlock the first
1047 * page (which the caller expects to stay locked), don't
1048 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001049 *
1050 * Do set the Private2 bit so we know this page was properly
1051 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001052 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001053 page_ops = unlock ? PAGE_UNLOCK : 0;
1054 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001055
Josef Bacikc2790a22013-07-29 11:20:47 -04001056 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001057 start + ram_size - 1,
1058 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001059 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001060 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001061 if (disk_num_bytes < cur_alloc_size)
1062 disk_num_bytes = 0;
1063 else
1064 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001065 num_bytes -= cur_alloc_size;
1066 alloc_hint = ins.objectid + ins.offset;
1067 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001068 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001069
1070 /*
1071 * btrfs_reloc_clone_csums() error, since start is increased
1072 * extent_clear_unlock_delalloc() at out_unlock label won't
1073 * free metadata of current ordered extent, we're OK to exit.
1074 */
1075 if (ret)
1076 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001077 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001079 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001080
Filipe Mananad9f85962014-08-25 10:43:00 +01001081out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001082 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001083out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001084 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001085 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001086out_unlock:
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DEFRAG |
1088 EXTENT_CLEAR_META_RESV;
1089 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1090 PAGE_END_WRITEBACK;
1091 /*
1092 * If we reserved an extent for our delalloc range (or a subrange) and
1093 * failed to create the respective ordered extent, then it means that
1094 * when we reserved the extent we decremented the extent's size from
1095 * the data space_info's bytes_may_use counter and incremented the
1096 * space_info's bytes_reserved counter by the same amount. We must make
1097 * sure extent_clear_unlock_delalloc() does not try to decrement again
1098 * the data space_info's bytes_may_use counter, therefore we do not pass
1099 * it the flag EXTENT_CLEAR_DATA_RESV.
1100 */
1101 if (extent_reserved) {
1102 extent_clear_unlock_delalloc(inode, start,
1103 start + cur_alloc_size,
1104 start + cur_alloc_size,
1105 locked_page,
1106 clear_bits,
1107 page_ops);
1108 start += cur_alloc_size;
1109 if (start >= end)
1110 goto out;
1111 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001112 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1113 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001114 clear_bits | EXTENT_CLEAR_DATA_RESV,
1115 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001117}
Chris Masonc8b97812008-10-29 14:49:59 -04001118
Chris Mason771ed682008-11-06 22:02:51 -05001119/*
1120 * work queue call back to started compression on a file and pages
1121 */
1122static noinline void async_cow_start(struct btrfs_work *work)
1123{
1124 struct async_cow *async_cow;
1125 int num_added = 0;
1126 async_cow = container_of(work, struct async_cow, work);
1127
1128 compress_file_range(async_cow->inode, async_cow->locked_page,
1129 async_cow->start, async_cow->end, async_cow,
1130 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001131 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001132 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 }
Chris Mason771ed682008-11-06 22:02:51 -05001135}
1136
1137/*
1138 * work queue call back to submit previously compressed pages
1139 */
1140static noinline void async_cow_submit(struct btrfs_work *work)
1141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001143 struct async_cow *async_cow;
1144 struct btrfs_root *root;
1145 unsigned long nr_pages;
1146
1147 async_cow = container_of(work, struct async_cow, work);
1148
1149 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001151 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1152 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001153
David Sterbaee863952015-02-16 19:41:40 +01001154 /*
1155 * atomic_sub_return implies a barrier for waitqueue_active
1156 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001157 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001158 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 waitqueue_active(&fs_info->async_submit_wait))
1160 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001161
Chris Masond3977122009-01-05 21:25:51 -05001162 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001163 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001164}
Chris Masonc8b97812008-10-29 14:49:59 -04001165
Chris Mason771ed682008-11-06 22:02:51 -05001166static noinline void async_cow_free(struct btrfs_work *work)
1167{
1168 struct async_cow *async_cow;
1169 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001170 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001171 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001172 kfree(async_cow);
1173}
1174
1175static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1176 u64 start, u64 end, int *page_started,
1177 unsigned long *nr_written)
1178{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001180 struct async_cow *async_cow;
1181 struct btrfs_root *root = BTRFS_I(inode)->root;
1182 unsigned long nr_pages;
1183 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001184
Chris Masona3429ab2009-10-08 12:30:20 -04001185 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1186 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001187 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001188 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001189 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001190 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001191 async_cow->root = root;
1192 async_cow->locked_page = locked_page;
1193 async_cow->start = start;
1194
Wang Shilongf79707b2014-07-17 11:44:09 +08001195 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001196 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001197 cur_end = end;
1198 else
Byongho Leeee221842015-12-15 01:42:10 +09001199 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001200
1201 async_cow->end = cur_end;
1202 INIT_LIST_HEAD(&async_cow->extents);
1203
Liu Bo9e0af232014-08-15 23:36:53 +08001204 btrfs_init_work(&async_cow->work,
1205 btrfs_delalloc_helper,
1206 async_cow_start, async_cow_submit,
1207 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001208
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001209 nr_pages = (cur_end - start + PAGE_SIZE) >>
1210 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001211 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001212
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 while (atomic_read(&fs_info->async_submit_draining) &&
1216 atomic_read(&fs_info->async_delalloc_pages)) {
1217 wait_event(fs_info->async_submit_wait,
1218 (atomic_read(&fs_info->async_delalloc_pages) ==
1219 0));
Chris Mason771ed682008-11-06 22:02:51 -05001220 }
1221
1222 *nr_written += nr_pages;
1223 start = cur_end + 1;
1224 }
1225 *page_started = 1;
1226 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227}
1228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001229static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001230 u64 bytenr, u64 num_bytes)
1231{
1232 int ret;
1233 struct btrfs_ordered_sum *sums;
1234 LIST_HEAD(list);
1235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001237 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001238 if (ret == 0 && list_empty(&list))
1239 return 0;
1240
1241 while (!list_empty(&list)) {
1242 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1243 list_del(&sums->list);
1244 kfree(sums);
1245 }
1246 return 1;
1247}
1248
Chris Masond352ac62008-09-29 15:18:18 -04001249/*
1250 * when nowcow writeback call back. This checks for snapshots or COW copies
1251 * of the extents that exist in the file, and COWs the file as required.
1252 *
1253 * If no cow copies or snapshots exist, we write directly to the existing
1254 * blocks on disk
1255 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001256static noinline int run_delalloc_nocow(struct inode *inode,
1257 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001258 u64 start, u64 end, int *page_started, int force,
1259 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001260{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001261 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001262 struct btrfs_root *root = BTRFS_I(inode)->root;
1263 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001267 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 u64 cow_start;
1269 u64 cur_offset;
1270 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 u64 disk_bytenr;
1273 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001274 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001275 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001277 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001278 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 int nocow;
1280 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001281 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001282 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
1284 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001285 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001286 extent_clear_unlock_delalloc(inode, start, end, end,
1287 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001288 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001289 EXTENT_DO_ACCOUNTING |
1290 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001291 PAGE_CLEAR_DIRTY |
1292 PAGE_SET_WRITEBACK |
1293 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001294 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001295 }
Li Zefan82d59022011-04-20 10:33:24 +08001296
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001297 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001298
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 cow_start = (u64)-1;
1300 cur_offset = start;
1301 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001302 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001304 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001305 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1307 leaf = path->nodes[0];
1308 btrfs_item_key_to_cpu(leaf, &found_key,
1309 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001310 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 found_key.type == BTRFS_EXTENT_DATA_KEY)
1312 path->slots[0]--;
1313 }
1314 check_prev = 0;
1315next_slot:
1316 leaf = path->nodes[0];
1317 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1318 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001319 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0)
1322 break;
1323 leaf = path->nodes[0];
1324 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001325
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 nocow = 0;
1327 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001330
Filipe Manana1d512cb2015-11-09 00:33:58 +00001331 if (found_key.objectid > ino)
1332 break;
1333 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1334 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 found_key.offset > end)
1340 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 if (found_key.offset > cur_offset) {
1343 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001344 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 goto out_check;
1346 }
Chris Masonc31f8832008-01-08 15:46:31 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 fi = btrfs_item_ptr(leaf, path->slots[0],
1349 struct btrfs_file_extent_item);
1350 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Josef Bacikcc95bef2013-04-04 14:31:27 -04001352 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001353 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1354 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 extent_end = found_key.offset +
1358 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001359 disk_num_bytes =
1360 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 if (extent_end <= start) {
1362 path->slots[0]++;
1363 goto next_slot;
1364 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001365 if (disk_bytenr == 0)
1366 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 if (btrfs_file_extent_compression(leaf, fi) ||
1368 btrfs_file_extent_encryption(leaf, fi) ||
1369 btrfs_file_extent_other_encoding(leaf, fi))
1370 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1372 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001373 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001375 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001376 found_key.offset -
1377 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001378 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001379 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001380 disk_bytenr += cur_offset - found_key.offset;
1381 num_bytes = min(end + 1, extent_end) - cur_offset;
1382 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001383 * if there are pending snapshots for this root,
1384 * we fall into common COW way.
1385 */
1386 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001387 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001388 if (!err)
1389 goto out_check;
1390 }
1391 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001392 * force cow if csum exists in the range.
1393 * this ensure that csum for a given extent are
1394 * either valid or do not exist.
1395 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001396 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001397 num_bytes)) {
1398 if (!nolock)
1399 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001400 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001401 }
1402 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1403 if (!nolock)
1404 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001405 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001406 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 nocow = 1;
1408 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1409 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001410 btrfs_file_extent_inline_len(leaf,
1411 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001412 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001413 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 } else {
1415 BUG_ON(1);
1416 }
1417out_check:
1418 if (extent_end <= start) {
1419 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001420 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001421 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001422 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 goto next_slot;
1425 }
1426 if (!nocow) {
1427 if (cow_start == (u64)-1)
1428 cow_start = cur_offset;
1429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
1432 path->slots[0]++;
1433 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001434 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001435
David Sterbab3b4aa72011-04-21 01:20:15 +02001436 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001438 ret = cow_file_range(inode, locked_page,
1439 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001440 end, page_started, nr_written, 1,
1441 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001442 if (ret) {
1443 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001444 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001445 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Yan Zhengd899e052008-10-30 14:25:28 -04001453 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001454 u64 orig_start = found_key.offset - extent_offset;
1455
1456 em = create_io_em(inode, cur_offset, num_bytes,
1457 orig_start,
1458 disk_bytenr, /* block_start */
1459 num_bytes, /* block_len */
1460 disk_num_bytes, /* orig_block_len */
1461 ram_bytes, BTRFS_COMPRESS_NONE,
1462 BTRFS_ORDERED_PREALLOC);
1463 if (IS_ERR(em)) {
1464 if (!nolock && nocow)
1465 btrfs_end_write_no_snapshoting(root);
1466 if (nocow)
1467 btrfs_dec_nocow_writers(fs_info,
1468 disk_bytenr);
1469 ret = PTR_ERR(em);
1470 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001471 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 free_extent_map(em);
1473 }
1474
1475 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001476 type = BTRFS_ORDERED_PREALLOC;
1477 } else {
1478 type = BTRFS_ORDERED_NOCOW;
1479 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001480
1481 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001482 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001483 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001484 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001485 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001486
Yan, Zhengefa56462010-05-16 10:49:59 -04001487 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001488 BTRFS_DATA_RELOC_TREE_OBJECTID)
1489 /*
1490 * Error handled later, as we must prevent
1491 * extent_clear_unlock_delalloc() in error handler
1492 * from freeing metadata of created ordered extent.
1493 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001494 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1495 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001496
Josef Bacikc2790a22013-07-29 11:20:47 -04001497 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001498 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001499 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001500 EXTENT_DELALLOC |
1501 EXTENT_CLEAR_DATA_RESV,
1502 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1503
Wang Shilonge9894fd2014-03-27 11:12:25 +08001504 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001505 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001506 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001507
1508 /*
1509 * btrfs_reloc_clone_csums() error, now we're OK to call error
1510 * handler, as metadata for created ordered extent will only
1511 * be freed by btrfs_finish_ordered_io().
1512 */
1513 if (ret)
1514 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001515 if (cur_offset > end)
1516 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001517 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001518 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001519
Josef Bacik17ca04a2012-05-31 15:58:55 -04001520 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 cur_offset = end;
1523 }
1524
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001526 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1527 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001528 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001529 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 }
1531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001533 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001534 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001535 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001536 EXTENT_DELALLOC | EXTENT_DEFRAG |
1537 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1538 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 PAGE_SET_WRITEBACK |
1540 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001541 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001543}
1544
Wang Shilong47059d92014-07-03 18:22:07 +08001545static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1546{
1547
1548 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1549 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1550 return 0;
1551
1552 /*
1553 * @defrag_bytes is a hint value, no spinlock held here,
1554 * if is not zero, it means the file is defragging.
1555 * Force cow if given extent needs to be defragged.
1556 */
1557 if (BTRFS_I(inode)->defrag_bytes &&
1558 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1559 EXTENT_DEFRAG, 0, NULL))
1560 return 1;
1561
1562 return 0;
1563}
1564
Chris Masond352ac62008-09-29 15:18:18 -04001565/*
1566 * extent_io.c call back to do delayed allocation processing
1567 */
Chris Masonc8b97812008-10-29 14:49:59 -04001568static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001569 u64 start, u64 end, int *page_started,
1570 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001571{
Chris Masonbe20aa92007-12-17 20:14:01 -05001572 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001573 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001574
Wang Shilong47059d92014-07-03 18:22:07 +08001575 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001576 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001577 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001578 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001579 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001580 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001581 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001582 ret = cow_file_range(inode, locked_page, start, end, end,
1583 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001584 } else {
1585 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1586 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001587 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001588 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001589 }
Qu Wenruo524272602017-03-08 10:25:52 +08001590 if (ret)
1591 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001592 return ret;
1593}
1594
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001595static void btrfs_split_extent_hook(struct inode *inode,
1596 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001597{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001598 u64 size;
1599
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001601 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001602 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 size = orig->end - orig->start + 1;
1605 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001606 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001607 u64 new_size;
1608
1609 /*
Josef Bacikba117212015-03-13 15:01:24 -04001610 * See the explanation in btrfs_merge_extent_hook, the same
1611 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 */
1613 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001614 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001615 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001616 num_extents += count_max_extents(new_size);
1617 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 return;
1619 }
1620
Josef Bacik9e0baf62011-07-15 15:16:44 +00001621 spin_lock(&BTRFS_I(inode)->lock);
1622 BTRFS_I(inode)->outstanding_extents++;
1623 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624}
1625
1626/*
1627 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1628 * extents so we can keep track of new extents that are just merged onto old
1629 * extents, such as when we are doing sequential writes, so we can properly
1630 * account for the metadata space we'll need.
1631 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001632static void btrfs_merge_extent_hook(struct inode *inode,
1633 struct extent_state *new,
1634 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001637 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638
Josef Bacik9ed74f22009-09-11 16:12:44 -04001639 /* not delalloc, ignore it */
1640 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001641 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001642
Josef Bacik8461a3d2015-03-13 15:12:08 -04001643 if (new->start > other->start)
1644 new_size = new->end - other->start + 1;
1645 else
1646 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647
1648 /* we're not bigger than the max, unreserve the space and go */
1649 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1650 spin_lock(&BTRFS_I(inode)->lock);
1651 BTRFS_I(inode)->outstanding_extents--;
1652 spin_unlock(&BTRFS_I(inode)->lock);
1653 return;
1654 }
1655
1656 /*
Josef Bacikba117212015-03-13 15:01:24 -04001657 * We have to add up either side to figure out how many extents were
1658 * accounted for before we merged into one big extent. If the number of
1659 * extents we accounted for is <= the amount we need for the new range
1660 * then we can return, otherwise drop. Think of it like this
1661 *
1662 * [ 4k][MAX_SIZE]
1663 *
1664 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1665 * need 2 outstanding extents, on one side we have 1 and the other side
1666 * we have 1 so they are == and we can return. But in this case
1667 *
1668 * [MAX_SIZE+4k][MAX_SIZE+4k]
1669 *
1670 * Each range on their own accounts for 2 extents, but merged together
1671 * they are only 3 extents worth of accounting, so we need to drop in
1672 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 */
Josef Bacikba117212015-03-13 15:01:24 -04001674 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001675 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001676 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001677 num_extents += count_max_extents(old_size);
1678 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001679 return;
1680
Josef Bacik9e0baf62011-07-15 15:16:44 +00001681 spin_lock(&BTRFS_I(inode)->lock);
1682 BTRFS_I(inode)->outstanding_extents--;
1683 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001684}
1685
Miao Xieeb73c1b2013-05-15 07:48:22 +00001686static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1687 struct inode *inode)
1688{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001689 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1690
Miao Xieeb73c1b2013-05-15 07:48:22 +00001691 spin_lock(&root->delalloc_lock);
1692 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1693 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1694 &root->delalloc_inodes);
1695 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1696 &BTRFS_I(inode)->runtime_flags);
1697 root->nr_delalloc_inodes++;
1698 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001699 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001700 BUG_ON(!list_empty(&root->delalloc_root));
1701 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 &fs_info->delalloc_roots);
1703 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001704 }
1705 }
1706 spin_unlock(&root->delalloc_lock);
1707}
1708
1709static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001710 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001711{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001712 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713
Miao Xieeb73c1b2013-05-15 07:48:22 +00001714 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001715 if (!list_empty(&inode->delalloc_inodes)) {
1716 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001718 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 root->nr_delalloc_inodes--;
1720 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001721 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001722 BUG_ON(list_empty(&root->delalloc_root));
1723 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001724 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 }
1726 }
1727 spin_unlock(&root->delalloc_lock);
1728}
1729
Chris Masond352ac62008-09-29 15:18:18 -04001730/*
1731 * extent_io.c set_bit_hook, used to track delayed allocation
1732 * bytes in this file, and to maintain the list of inodes that
1733 * have pending delalloc work to be done.
1734 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001735static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001736 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001737{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001738
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1740
Wang Shilong47059d92014-07-03 18:22:07 +08001741 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1742 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001743 /*
1744 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001745 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001746 * bit, which is only set or cleared with irqs on
1747 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001749 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001751 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Josef Bacik9e0baf62011-07-15 15:16:44 +00001753 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001755 } else {
1756 spin_lock(&BTRFS_I(inode)->lock);
1757 BTRFS_I(inode)->outstanding_extents++;
1758 spin_unlock(&BTRFS_I(inode)->lock);
1759 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001760
Josef Bacik6a3891c2015-03-16 17:38:52 -04001761 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001762 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 return;
1764
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001765 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1766 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001767 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001769 if (*bits & EXTENT_DEFRAG)
1770 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001771 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001772 &BTRFS_I(inode)->runtime_flags))
1773 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001775 }
Chris Mason291d6732008-01-29 15:55:23 -05001776}
1777
Chris Masond352ac62008-09-29 15:18:18 -04001778/*
1779 * extent_io.c clear_bit_hook, see set_bit_hook for why
1780 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001781static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001782 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001783 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001784{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001786 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001787 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001788
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001789 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001790 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001791 inode->defrag_bytes -= len;
1792 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001793
Chris Mason75eff682008-12-15 15:54:40 -05001794 /*
1795 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001796 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001797 * bit, which is only set or cleared with irqs on
1798 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001799 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001800 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001801 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001802
Josef Bacik9e0baf62011-07-15 15:16:44 +00001803 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001804 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001805 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001806 spin_lock(&inode->lock);
1807 inode->outstanding_extents -= num_extents;
1808 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001809 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001810
Josef Bacikb6d08f02013-09-27 14:57:43 -04001811 /*
1812 * We don't reserve metadata space for space cache inodes so we
1813 * don't need to call dellalloc_release_metadata if there is an
1814 * error.
1815 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001816 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001817 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001818 btrfs_delalloc_release_metadata(inode, len);
1819
Josef Bacik6a3891c2015-03-16 17:38:52 -04001820 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001821 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001822 return;
1823
Filipe Mananaa315e682017-03-06 23:04:20 +00001824 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1825 do_list && !(state->state & EXTENT_NORESERVE) &&
1826 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001827 btrfs_free_reserved_data_space_noquota(
1828 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001829 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001830
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001831 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1832 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001833 spin_lock(&inode->lock);
1834 inode->delalloc_bytes -= len;
1835 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001836 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001837 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001838 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001839 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001840 }
Chris Mason291d6732008-01-29 15:55:23 -05001841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/*
1844 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1845 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001846 *
1847 * return 1 if page cannot be merged to bio
1848 * return 0 if page can be merged to bio
1849 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001850 */
Mike Christie81a75f672016-06-05 14:31:54 -05001851int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001852 size_t size, struct bio *bio,
1853 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001854{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001855 struct inode *inode = page->mapping->host;
1856 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001857 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001858 u64 length = 0;
1859 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001860 int ret;
1861
Chris Mason771ed682008-11-06 22:02:51 -05001862 if (bio_flags & EXTENT_BIO_COMPRESSED)
1863 return 0;
1864
Kent Overstreet4f024f32013-10-11 15:44:27 -07001865 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001866 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001867 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1868 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001869 if (ret < 0)
1870 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001871 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001872 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001873 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * in order to insert checksums into the metadata in large chunks,
1878 * we wait until bio submission time. All the pages in the bio are
1879 * checksummed and sums are attached onto the ordered extent record.
1880 *
1881 * At IO completion time the cums attached on the ordered extent record
1882 * are inserted into the btree
1883 */
Mike Christie81a75f672016-06-05 14:31:54 -05001884static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1885 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001886 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001887{
Chris Mason065631f2008-02-20 12:07:25 -05001888 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001889
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001890 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001891 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001892 return 0;
1893}
Chris Masone0156402008-04-16 11:15:20 -04001894
Chris Mason4a69a412008-11-06 22:03:00 -05001895/*
1896 * in order to insert checksums into the metadata in large chunks,
1897 * we wait until bio submission time. All the pages in the bio are
1898 * checksummed and sums are attached onto the ordered extent record.
1899 *
1900 * At IO completion time the cums attached on the ordered extent record
1901 * are inserted into the btree
1902 */
Mike Christie81a75f672016-06-05 14:31:54 -05001903static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001904 int mirror_num, unsigned long bio_flags,
1905 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001906{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001907 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001908 int ret;
1909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001910 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001911 if (ret) {
1912 bio->bi_error = ret;
1913 bio_endio(bio);
1914 }
Stefan Behrens61891922012-11-05 18:51:52 +01001915 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001916}
1917
Chris Masond352ac62008-09-29 15:18:18 -04001918/*
Chris Masoncad321a2008-12-17 14:51:42 -05001919 * extent_io.c submission hook. This does the right thing for csum calculation
1920 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001921 */
Mike Christie81a75f672016-06-05 14:31:54 -05001922static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001923 int mirror_num, unsigned long bio_flags,
1924 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301928 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001929 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001930 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001931 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001932
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001933 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001934
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001935 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301936 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001937
Mike Christie37226b22016-06-05 14:31:52 -05001938 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001939 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001940 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001941 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001942
Chris Masond20f7042008-12-08 16:58:54 -05001943 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001944 ret = btrfs_submit_compressed_read(inode, bio,
1945 mirror_num,
1946 bio_flags);
1947 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001948 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001949 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001950 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001951 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001952 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001953 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001954 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001955 /* csum items have already been cloned */
1956 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1957 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001958 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001959 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1960 bio_flags, bio_offset,
1961 __btrfs_submit_bio_start,
1962 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001963 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001964 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001965 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001966 if (ret)
1967 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001968 }
1969
Chris Mason0b86a832008-03-24 15:01:56 -04001970mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001971 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001972
1973out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001974 if (ret < 0) {
1975 bio->bi_error = ret;
1976 bio_endio(bio);
1977 }
Stefan Behrens61891922012-11-05 18:51:52 +01001978 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001979}
Chris Mason6885f302008-02-20 16:11:05 -05001980
Chris Masond352ac62008-09-29 15:18:18 -04001981/*
1982 * given a list of ordered sums record them in the inode. This happens
1983 * at IO completion time based on sums calculated at bio submission time.
1984 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001985static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001986 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001987{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001988 struct btrfs_ordered_sum *sum;
1989
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001990 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001991 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001992 btrfs_csum_file_blocks(trans,
1993 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001994 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001995 }
1996 return 0;
1997}
1998
Josef Bacik2ac55d42010-02-03 19:33:23 +00001999int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002000 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002001{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002003 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002004 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002005}
2006
Chris Masond352ac62008-09-29 15:18:18 -04002007/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002008struct btrfs_writepage_fixup {
2009 struct page *page;
2010 struct btrfs_work work;
2011};
2012
Christoph Hellwigb2950862008-12-02 09:54:17 -05002013static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002014{
2015 struct btrfs_writepage_fixup *fixup;
2016 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002017 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002018 struct page *page;
2019 struct inode *inode;
2020 u64 page_start;
2021 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002022 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002023
2024 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2025 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002026again:
Chris Mason247e7432008-07-17 12:53:51 -04002027 lock_page(page);
2028 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2029 ClearPageChecked(page);
2030 goto out_page;
2031 }
2032
2033 inode = page->mapping->host;
2034 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002035 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002036
David Sterbaff13db42015-12-03 14:30:40 +01002037 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002038 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002039
2040 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002041 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002042 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002043
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002044 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002045 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002046 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002047 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2048 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002049 unlock_page(page);
2050 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002051 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002052 goto again;
2053 }
Chris Mason247e7432008-07-17 12:53:51 -04002054
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002055 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002057 if (ret) {
2058 mapping_set_error(page->mapping, ret);
2059 end_extent_writepage(page, ret, page_start, page_end);
2060 ClearPageChecked(page);
2061 goto out;
2062 }
2063
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002064 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2065 0);
Chris Mason247e7432008-07-17 12:53:51 -04002066 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002067 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002068out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002069 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2070 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002071out_page:
2072 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002074 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002075}
2076
2077/*
2078 * There are a few paths in the higher layers of the kernel that directly
2079 * set the page dirty bit without asking the filesystem if it is a
2080 * good idea. This causes problems because we want to make sure COW
2081 * properly happens and the data=ordered rules are followed.
2082 *
Chris Masonc8b97812008-10-29 14:49:59 -04002083 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002084 * hasn't been properly setup for IO. We kick off an async process
2085 * to fix it up. The async helper will wait for ordered extents, set
2086 * the delalloc bit and make it safe to write the page.
2087 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002088static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002089{
2090 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002092 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002093
Chris Mason8b62b722009-09-02 16:53:46 -04002094 /* this page is properly in the ordered list */
2095 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002096 return 0;
2097
2098 if (PageChecked(page))
2099 return -EAGAIN;
2100
2101 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2102 if (!fixup)
2103 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002104
Chris Mason247e7432008-07-17 12:53:51 -04002105 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002107 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2108 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002109 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002110 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002111 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002112}
2113
Yan Zhengd899e052008-10-30 14:25:28 -04002114static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2115 struct inode *inode, u64 file_pos,
2116 u64 disk_bytenr, u64 disk_num_bytes,
2117 u64 num_bytes, u64 ram_bytes,
2118 u8 compression, u8 encryption,
2119 u16 other_encoding, int extent_type)
2120{
2121 struct btrfs_root *root = BTRFS_I(inode)->root;
2122 struct btrfs_file_extent_item *fi;
2123 struct btrfs_path *path;
2124 struct extent_buffer *leaf;
2125 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002126 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002127 int ret;
2128
2129 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002130 if (!path)
2131 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002132
Chris Masona1ed8352009-09-11 12:27:37 -04002133 /*
2134 * we may be replacing one extent in the tree with another.
2135 * The new extent is pinned in the extent map, and we don't want
2136 * to drop it from the cache until it is completely in the btree.
2137 *
2138 * So, tell btrfs_drop_extents to leave this extent in the cache.
2139 * the caller is expected to unpin it and allow it to be merged
2140 * with the others.
2141 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002142 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2143 file_pos + num_bytes, NULL, 0,
2144 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002145 if (ret)
2146 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002147
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002148 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002149 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002150 ins.offset = file_pos;
2151 ins.type = BTRFS_EXTENT_DATA_KEY;
2152
2153 path->leave_spinning = 1;
2154 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2155 sizeof(*fi));
2156 if (ret)
2157 goto out;
2158 }
Yan Zhengd899e052008-10-30 14:25:28 -04002159 leaf = path->nodes[0];
2160 fi = btrfs_item_ptr(leaf, path->slots[0],
2161 struct btrfs_file_extent_item);
2162 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2163 btrfs_set_file_extent_type(leaf, fi, extent_type);
2164 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2165 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2166 btrfs_set_file_extent_offset(leaf, fi, 0);
2167 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2168 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2169 btrfs_set_file_extent_compression(leaf, fi, compression);
2170 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2171 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002172
Yan Zhengd899e052008-10-30 14:25:28 -04002173 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002174 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002175
2176 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002177
2178 ins.objectid = disk_bytenr;
2179 ins.offset = disk_num_bytes;
2180 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002181 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002182 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002183 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002184 * Release the reserved range from inode dirty range map, as it is
2185 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002186 */
2187 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002188out:
Yan Zhengd899e052008-10-30 14:25:28 -04002189 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002190
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002191 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002192}
2193
Liu Bo38c227d2013-01-29 03:18:40 +00002194/* snapshot-aware defrag */
2195struct sa_defrag_extent_backref {
2196 struct rb_node node;
2197 struct old_sa_defrag_extent *old;
2198 u64 root_id;
2199 u64 inum;
2200 u64 file_pos;
2201 u64 extent_offset;
2202 u64 num_bytes;
2203 u64 generation;
2204};
2205
2206struct old_sa_defrag_extent {
2207 struct list_head list;
2208 struct new_sa_defrag_extent *new;
2209
2210 u64 extent_offset;
2211 u64 bytenr;
2212 u64 offset;
2213 u64 len;
2214 int count;
2215};
2216
2217struct new_sa_defrag_extent {
2218 struct rb_root root;
2219 struct list_head head;
2220 struct btrfs_path *path;
2221 struct inode *inode;
2222 u64 file_pos;
2223 u64 len;
2224 u64 bytenr;
2225 u64 disk_len;
2226 u8 compress_type;
2227};
2228
2229static int backref_comp(struct sa_defrag_extent_backref *b1,
2230 struct sa_defrag_extent_backref *b2)
2231{
2232 if (b1->root_id < b2->root_id)
2233 return -1;
2234 else if (b1->root_id > b2->root_id)
2235 return 1;
2236
2237 if (b1->inum < b2->inum)
2238 return -1;
2239 else if (b1->inum > b2->inum)
2240 return 1;
2241
2242 if (b1->file_pos < b2->file_pos)
2243 return -1;
2244 else if (b1->file_pos > b2->file_pos)
2245 return 1;
2246
2247 /*
2248 * [------------------------------] ===> (a range of space)
2249 * |<--->| |<---->| =============> (fs/file tree A)
2250 * |<---------------------------->| ===> (fs/file tree B)
2251 *
2252 * A range of space can refer to two file extents in one tree while
2253 * refer to only one file extent in another tree.
2254 *
2255 * So we may process a disk offset more than one time(two extents in A)
2256 * and locate at the same extent(one extent in B), then insert two same
2257 * backrefs(both refer to the extent in B).
2258 */
2259 return 0;
2260}
2261
2262static void backref_insert(struct rb_root *root,
2263 struct sa_defrag_extent_backref *backref)
2264{
2265 struct rb_node **p = &root->rb_node;
2266 struct rb_node *parent = NULL;
2267 struct sa_defrag_extent_backref *entry;
2268 int ret;
2269
2270 while (*p) {
2271 parent = *p;
2272 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2273
2274 ret = backref_comp(backref, entry);
2275 if (ret < 0)
2276 p = &(*p)->rb_left;
2277 else
2278 p = &(*p)->rb_right;
2279 }
2280
2281 rb_link_node(&backref->node, parent, p);
2282 rb_insert_color(&backref->node, root);
2283}
2284
2285/*
2286 * Note the backref might has changed, and in this case we just return 0.
2287 */
2288static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2289 void *ctx)
2290{
2291 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002292 struct old_sa_defrag_extent *old = ctx;
2293 struct new_sa_defrag_extent *new = old->new;
2294 struct btrfs_path *path = new->path;
2295 struct btrfs_key key;
2296 struct btrfs_root *root;
2297 struct sa_defrag_extent_backref *backref;
2298 struct extent_buffer *leaf;
2299 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002300 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002301 int slot;
2302 int ret;
2303 u64 extent_offset;
2304 u64 num_bytes;
2305
2306 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002307 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002308 return 0;
2309
2310 key.objectid = root_id;
2311 key.type = BTRFS_ROOT_ITEM_KEY;
2312 key.offset = (u64)-1;
2313
Liu Bo38c227d2013-01-29 03:18:40 +00002314 root = btrfs_read_fs_root_no_name(fs_info, &key);
2315 if (IS_ERR(root)) {
2316 if (PTR_ERR(root) == -ENOENT)
2317 return 0;
2318 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002319 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002320 inum, offset, root_id);
2321 return PTR_ERR(root);
2322 }
2323
2324 key.objectid = inum;
2325 key.type = BTRFS_EXTENT_DATA_KEY;
2326 if (offset > (u64)-1 << 32)
2327 key.offset = 0;
2328 else
2329 key.offset = offset;
2330
2331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302332 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002333 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002334 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002335
2336 while (1) {
2337 cond_resched();
2338
2339 leaf = path->nodes[0];
2340 slot = path->slots[0];
2341
2342 if (slot >= btrfs_header_nritems(leaf)) {
2343 ret = btrfs_next_leaf(root, path);
2344 if (ret < 0) {
2345 goto out;
2346 } else if (ret > 0) {
2347 ret = 0;
2348 goto out;
2349 }
2350 continue;
2351 }
2352
2353 path->slots[0]++;
2354
2355 btrfs_item_key_to_cpu(leaf, &key, slot);
2356
2357 if (key.objectid > inum)
2358 goto out;
2359
2360 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2361 continue;
2362
2363 extent = btrfs_item_ptr(leaf, slot,
2364 struct btrfs_file_extent_item);
2365
2366 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2367 continue;
2368
Liu Boe68afa42013-07-01 22:13:26 +08002369 /*
2370 * 'offset' refers to the exact key.offset,
2371 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2372 * (key.offset - extent_offset).
2373 */
2374 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002375 continue;
2376
Liu Boe68afa42013-07-01 22:13:26 +08002377 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002378 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002379
Liu Bo38c227d2013-01-29 03:18:40 +00002380 if (extent_offset >= old->extent_offset + old->offset +
2381 old->len || extent_offset + num_bytes <=
2382 old->extent_offset + old->offset)
2383 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002384 break;
2385 }
2386
2387 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2388 if (!backref) {
2389 ret = -ENOENT;
2390 goto out;
2391 }
2392
2393 backref->root_id = root_id;
2394 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002395 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002396 backref->num_bytes = num_bytes;
2397 backref->extent_offset = extent_offset;
2398 backref->generation = btrfs_file_extent_generation(leaf, extent);
2399 backref->old = old;
2400 backref_insert(&new->root, backref);
2401 old->count++;
2402out:
2403 btrfs_release_path(path);
2404 WARN_ON(ret);
2405 return ret;
2406}
2407
2408static noinline bool record_extent_backrefs(struct btrfs_path *path,
2409 struct new_sa_defrag_extent *new)
2410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002411 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002412 struct old_sa_defrag_extent *old, *tmp;
2413 int ret;
2414
2415 new->path = path;
2416
2417 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002418 ret = iterate_inodes_from_logical(old->bytenr +
2419 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002420 path, record_one_backref,
2421 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002422 if (ret < 0 && ret != -ENOENT)
2423 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002424
2425 /* no backref to be processed for this extent */
2426 if (!old->count) {
2427 list_del(&old->list);
2428 kfree(old);
2429 }
2430 }
2431
2432 if (list_empty(&new->head))
2433 return false;
2434
2435 return true;
2436}
2437
2438static int relink_is_mergable(struct extent_buffer *leaf,
2439 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002440 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002441{
Liu Bo116e0022013-08-02 16:30:40 +08002442 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002443 return 0;
2444
2445 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2446 return 0;
2447
Liu Bo116e0022013-08-02 16:30:40 +08002448 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2449 return 0;
2450
2451 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002452 btrfs_file_extent_other_encoding(leaf, fi))
2453 return 0;
2454
2455 return 1;
2456}
2457
2458/*
2459 * Note the backref might has changed, and in this case we just return 0.
2460 */
2461static noinline int relink_extent_backref(struct btrfs_path *path,
2462 struct sa_defrag_extent_backref *prev,
2463 struct sa_defrag_extent_backref *backref)
2464{
2465 struct btrfs_file_extent_item *extent;
2466 struct btrfs_file_extent_item *item;
2467 struct btrfs_ordered_extent *ordered;
2468 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002469 struct btrfs_root *root;
2470 struct btrfs_key key;
2471 struct extent_buffer *leaf;
2472 struct old_sa_defrag_extent *old = backref->old;
2473 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002474 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002475 struct inode *inode;
2476 struct extent_state *cached = NULL;
2477 int ret = 0;
2478 u64 start;
2479 u64 len;
2480 u64 lock_start;
2481 u64 lock_end;
2482 bool merge = false;
2483 int index;
2484
2485 if (prev && prev->root_id == backref->root_id &&
2486 prev->inum == backref->inum &&
2487 prev->file_pos + prev->num_bytes == backref->file_pos)
2488 merge = true;
2489
2490 /* step 1: get root */
2491 key.objectid = backref->root_id;
2492 key.type = BTRFS_ROOT_ITEM_KEY;
2493 key.offset = (u64)-1;
2494
Liu Bo38c227d2013-01-29 03:18:40 +00002495 index = srcu_read_lock(&fs_info->subvol_srcu);
2496
2497 root = btrfs_read_fs_root_no_name(fs_info, &key);
2498 if (IS_ERR(root)) {
2499 srcu_read_unlock(&fs_info->subvol_srcu, index);
2500 if (PTR_ERR(root) == -ENOENT)
2501 return 0;
2502 return PTR_ERR(root);
2503 }
Liu Bo38c227d2013-01-29 03:18:40 +00002504
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002505 if (btrfs_root_readonly(root)) {
2506 srcu_read_unlock(&fs_info->subvol_srcu, index);
2507 return 0;
2508 }
2509
Liu Bo38c227d2013-01-29 03:18:40 +00002510 /* step 2: get inode */
2511 key.objectid = backref->inum;
2512 key.type = BTRFS_INODE_ITEM_KEY;
2513 key.offset = 0;
2514
2515 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2516 if (IS_ERR(inode)) {
2517 srcu_read_unlock(&fs_info->subvol_srcu, index);
2518 return 0;
2519 }
2520
2521 srcu_read_unlock(&fs_info->subvol_srcu, index);
2522
2523 /* step 3: relink backref */
2524 lock_start = backref->file_pos;
2525 lock_end = backref->file_pos + backref->num_bytes - 1;
2526 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002527 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002528
2529 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2530 if (ordered) {
2531 btrfs_put_ordered_extent(ordered);
2532 goto out_unlock;
2533 }
2534
2535 trans = btrfs_join_transaction(root);
2536 if (IS_ERR(trans)) {
2537 ret = PTR_ERR(trans);
2538 goto out_unlock;
2539 }
2540
2541 key.objectid = backref->inum;
2542 key.type = BTRFS_EXTENT_DATA_KEY;
2543 key.offset = backref->file_pos;
2544
2545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2546 if (ret < 0) {
2547 goto out_free_path;
2548 } else if (ret > 0) {
2549 ret = 0;
2550 goto out_free_path;
2551 }
2552
2553 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2554 struct btrfs_file_extent_item);
2555
2556 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2557 backref->generation)
2558 goto out_free_path;
2559
2560 btrfs_release_path(path);
2561
2562 start = backref->file_pos;
2563 if (backref->extent_offset < old->extent_offset + old->offset)
2564 start += old->extent_offset + old->offset -
2565 backref->extent_offset;
2566
2567 len = min(backref->extent_offset + backref->num_bytes,
2568 old->extent_offset + old->offset + old->len);
2569 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2570
2571 ret = btrfs_drop_extents(trans, root, inode, start,
2572 start + len, 1);
2573 if (ret)
2574 goto out_free_path;
2575again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002576 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002577 key.type = BTRFS_EXTENT_DATA_KEY;
2578 key.offset = start;
2579
Liu Boa09a0a72013-03-11 09:20:58 +00002580 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002581 if (merge) {
2582 struct btrfs_file_extent_item *fi;
2583 u64 extent_len;
2584 struct btrfs_key found_key;
2585
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002586 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002587 if (ret < 0)
2588 goto out_free_path;
2589
2590 path->slots[0]--;
2591 leaf = path->nodes[0];
2592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2593
2594 fi = btrfs_item_ptr(leaf, path->slots[0],
2595 struct btrfs_file_extent_item);
2596 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2597
Liu Bo116e0022013-08-02 16:30:40 +08002598 if (extent_len + found_key.offset == start &&
2599 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002600 btrfs_set_file_extent_num_bytes(leaf, fi,
2601 extent_len + len);
2602 btrfs_mark_buffer_dirty(leaf);
2603 inode_add_bytes(inode, len);
2604
2605 ret = 1;
2606 goto out_free_path;
2607 } else {
2608 merge = false;
2609 btrfs_release_path(path);
2610 goto again;
2611 }
2612 }
2613
2614 ret = btrfs_insert_empty_item(trans, root, path, &key,
2615 sizeof(*extent));
2616 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002617 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002618 goto out_free_path;
2619 }
2620
2621 leaf = path->nodes[0];
2622 item = btrfs_item_ptr(leaf, path->slots[0],
2623 struct btrfs_file_extent_item);
2624 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2625 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2626 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2627 btrfs_set_file_extent_num_bytes(leaf, item, len);
2628 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2629 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2630 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2631 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2632 btrfs_set_file_extent_encryption(leaf, item, 0);
2633 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2634
2635 btrfs_mark_buffer_dirty(leaf);
2636 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002637 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002638
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002639 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002640 new->disk_len, 0,
2641 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002642 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002643 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002644 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002645 goto out_free_path;
2646 }
2647
2648 ret = 1;
2649out_free_path:
2650 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002651 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002652 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002653out_unlock:
2654 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2655 &cached, GFP_NOFS);
2656 iput(inode);
2657 return ret;
2658}
2659
Liu Bo6f519562013-10-29 10:45:05 +08002660static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2661{
2662 struct old_sa_defrag_extent *old, *tmp;
2663
2664 if (!new)
2665 return;
2666
2667 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002668 kfree(old);
2669 }
2670 kfree(new);
2671}
2672
Liu Bo38c227d2013-01-29 03:18:40 +00002673static void relink_file_extents(struct new_sa_defrag_extent *new)
2674{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002676 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002677 struct sa_defrag_extent_backref *backref;
2678 struct sa_defrag_extent_backref *prev = NULL;
2679 struct inode *inode;
2680 struct btrfs_root *root;
2681 struct rb_node *node;
2682 int ret;
2683
2684 inode = new->inode;
2685 root = BTRFS_I(inode)->root;
2686
2687 path = btrfs_alloc_path();
2688 if (!path)
2689 return;
2690
2691 if (!record_extent_backrefs(path, new)) {
2692 btrfs_free_path(path);
2693 goto out;
2694 }
2695 btrfs_release_path(path);
2696
2697 while (1) {
2698 node = rb_first(&new->root);
2699 if (!node)
2700 break;
2701 rb_erase(node, &new->root);
2702
2703 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2704
2705 ret = relink_extent_backref(path, prev, backref);
2706 WARN_ON(ret < 0);
2707
2708 kfree(prev);
2709
2710 if (ret == 1)
2711 prev = backref;
2712 else
2713 prev = NULL;
2714 cond_resched();
2715 }
2716 kfree(prev);
2717
2718 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002719out:
Liu Bo6f519562013-10-29 10:45:05 +08002720 free_sa_defrag_extent(new);
2721
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002722 atomic_dec(&fs_info->defrag_running);
2723 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002724}
2725
2726static struct new_sa_defrag_extent *
2727record_old_file_extents(struct inode *inode,
2728 struct btrfs_ordered_extent *ordered)
2729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002731 struct btrfs_root *root = BTRFS_I(inode)->root;
2732 struct btrfs_path *path;
2733 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002734 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002735 struct new_sa_defrag_extent *new;
2736 int ret;
2737
2738 new = kmalloc(sizeof(*new), GFP_NOFS);
2739 if (!new)
2740 return NULL;
2741
2742 new->inode = inode;
2743 new->file_pos = ordered->file_offset;
2744 new->len = ordered->len;
2745 new->bytenr = ordered->start;
2746 new->disk_len = ordered->disk_len;
2747 new->compress_type = ordered->compress_type;
2748 new->root = RB_ROOT;
2749 INIT_LIST_HEAD(&new->head);
2750
2751 path = btrfs_alloc_path();
2752 if (!path)
2753 goto out_kfree;
2754
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002755 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002756 key.type = BTRFS_EXTENT_DATA_KEY;
2757 key.offset = new->file_pos;
2758
2759 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2760 if (ret < 0)
2761 goto out_free_path;
2762 if (ret > 0 && path->slots[0] > 0)
2763 path->slots[0]--;
2764
2765 /* find out all the old extents for the file range */
2766 while (1) {
2767 struct btrfs_file_extent_item *extent;
2768 struct extent_buffer *l;
2769 int slot;
2770 u64 num_bytes;
2771 u64 offset;
2772 u64 end;
2773 u64 disk_bytenr;
2774 u64 extent_offset;
2775
2776 l = path->nodes[0];
2777 slot = path->slots[0];
2778
2779 if (slot >= btrfs_header_nritems(l)) {
2780 ret = btrfs_next_leaf(root, path);
2781 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002782 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002783 else if (ret > 0)
2784 break;
2785 continue;
2786 }
2787
2788 btrfs_item_key_to_cpu(l, &key, slot);
2789
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002790 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002791 break;
2792 if (key.type != BTRFS_EXTENT_DATA_KEY)
2793 break;
2794 if (key.offset >= new->file_pos + new->len)
2795 break;
2796
2797 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2798
2799 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2800 if (key.offset + num_bytes < new->file_pos)
2801 goto next;
2802
2803 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2804 if (!disk_bytenr)
2805 goto next;
2806
2807 extent_offset = btrfs_file_extent_offset(l, extent);
2808
2809 old = kmalloc(sizeof(*old), GFP_NOFS);
2810 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002811 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002812
2813 offset = max(new->file_pos, key.offset);
2814 end = min(new->file_pos + new->len, key.offset + num_bytes);
2815
2816 old->bytenr = disk_bytenr;
2817 old->extent_offset = extent_offset;
2818 old->offset = offset - key.offset;
2819 old->len = end - offset;
2820 old->new = new;
2821 old->count = 0;
2822 list_add_tail(&old->list, &new->head);
2823next:
2824 path->slots[0]++;
2825 cond_resched();
2826 }
2827
2828 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002829 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002830
2831 return new;
2832
Liu Bo38c227d2013-01-29 03:18:40 +00002833out_free_path:
2834 btrfs_free_path(path);
2835out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002836 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002837 return NULL;
2838}
2839
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002840static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002841 u64 start, u64 len)
2842{
2843 struct btrfs_block_group_cache *cache;
2844
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002845 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002846 ASSERT(cache);
2847
2848 spin_lock(&cache->lock);
2849 cache->delalloc_bytes -= len;
2850 spin_unlock(&cache->lock);
2851
2852 btrfs_put_block_group(cache);
2853}
2854
Chris Masond352ac62008-09-29 15:18:18 -04002855/* as ordered data IO finishes, this gets called so we can finish
2856 * an ordered extent if the range of bytes in the file it covers are
2857 * fully written.
2858 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002859static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002860{
Josef Bacik5fd02042012-05-02 14:00:54 -04002861 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002864 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002865 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002866 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002867 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002868 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002869 int ret = 0;
2870 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002871 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002874 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002875
Josef Bacik5fd02042012-05-02 14:00:54 -04002876 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2877 ret = -EIO;
2878 goto out;
2879 }
2880
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002881 btrfs_free_io_failure_record(BTRFS_I(inode),
2882 ordered_extent->file_offset,
2883 ordered_extent->file_offset +
2884 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002885
Josef Bacik77cef2e2013-08-29 13:57:21 -04002886 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2887 truncated = true;
2888 logical_len = ordered_extent->truncated_len;
2889 /* Truncated the entire extent, don't bother adding */
2890 if (!logical_len)
2891 goto out;
2892 }
2893
Yan, Zhengc2167752009-11-12 09:34:21 +00002894 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002895 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002896
2897 /*
2898 * For mwrite(mmap + memset to write) case, we still reserve
2899 * space for NOCOW range.
2900 * As NOCOW won't cause a new delayed ref, just free the space
2901 */
2902 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2903 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 if (nolock)
2906 trans = btrfs_join_transaction_nolock(root);
2907 else
2908 trans = btrfs_join_transaction(root);
2909 if (IS_ERR(trans)) {
2910 ret = PTR_ERR(trans);
2911 trans = NULL;
2912 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002913 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002914 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002915 ret = btrfs_update_inode_fallback(trans, root, inode);
2916 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002917 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002918 goto out;
2919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002920
Josef Bacik2ac55d42010-02-03 19:33:23 +00002921 lock_extent_bits(io_tree, ordered_extent->file_offset,
2922 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002923 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924
Liu Bo38c227d2013-01-29 03:18:40 +00002925 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2926 ordered_extent->file_offset + ordered_extent->len - 1,
2927 EXTENT_DEFRAG, 1, cached_state);
2928 if (ret) {
2929 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002930 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002931 /* the inode is shared */
2932 new = record_old_file_extents(inode, ordered_extent);
2933
2934 clear_extent_bit(io_tree, ordered_extent->file_offset,
2935 ordered_extent->file_offset + ordered_extent->len - 1,
2936 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2937 }
2938
Josef Bacik0cb59c92010-07-02 12:14:14 -04002939 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002940 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002941 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002942 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002943 if (IS_ERR(trans)) {
2944 ret = PTR_ERR(trans);
2945 trans = NULL;
2946 goto out_unlock;
2947 }
Chris Masona79b7d42014-05-22 16:18:52 -07002948
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002949 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002950
Chris Masonc8b97812008-10-29 14:49:59 -04002951 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002952 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002953 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002954 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002955 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002956 ordered_extent->file_offset,
2957 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002958 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002959 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002961 ret = insert_reserved_file_extent(trans, inode,
2962 ordered_extent->file_offset,
2963 ordered_extent->start,
2964 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002966 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002967 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002968 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002969 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002970 ordered_extent->start,
2971 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002972 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002973 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2974 ordered_extent->file_offset, ordered_extent->len,
2975 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002976 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002977 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002978 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002980
David Sterbadf9f6282017-02-10 19:35:37 +01002981 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002982
Josef Bacik6c760c02012-11-09 10:53:21 -05002983 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2984 ret = btrfs_update_inode_fallback(trans, root, inode);
2985 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002986 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002987 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002988 }
2989 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002990out_unlock:
2991 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2992 ordered_extent->file_offset +
2993 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002994out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002996 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2997 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002998 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002999 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003000
Josef Bacik77cef2e2013-08-29 13:57:21 -04003001 if (ret || truncated) {
3002 u64 start, end;
3003
3004 if (truncated)
3005 start = ordered_extent->file_offset + logical_len;
3006 else
3007 start = ordered_extent->file_offset;
3008 end = ordered_extent->file_offset + ordered_extent->len - 1;
3009 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3010
3011 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003012 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003013
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003014 /*
3015 * If the ordered extent had an IOERR or something else went
3016 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003017 * back to the allocator. We only free the extent in the
3018 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003019 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003020 if ((ret || !logical_len) &&
3021 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003022 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023 btrfs_free_reserved_extent(fs_info,
3024 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003025 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003026 }
3027
3028
Josef Bacik5fd02042012-05-02 14:00:54 -04003029 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003030 * This needs to be done to make sure anybody waiting knows we are done
3031 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003032 */
3033 btrfs_remove_ordered_extent(inode, ordered_extent);
3034
Liu Bo38c227d2013-01-29 03:18:40 +00003035 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003036 if (new) {
3037 if (ret) {
3038 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003040 } else {
3041 relink_file_extents(new);
3042 }
3043 }
Liu Bo38c227d2013-01-29 03:18:40 +00003044
Chris Masone6dcd2d2008-07-17 12:53:50 -04003045 /* once for us */
3046 btrfs_put_ordered_extent(ordered_extent);
3047 /* once for the tree */
3048 btrfs_put_ordered_extent(ordered_extent);
3049
Josef Bacik5fd02042012-05-02 14:00:54 -04003050 return ret;
3051}
3052
3053static void finish_ordered_fn(struct btrfs_work *work)
3054{
3055 struct btrfs_ordered_extent *ordered_extent;
3056 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3057 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058}
3059
David Sterbac3988d62017-02-17 15:18:32 +01003060static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003061 struct extent_state *state, int uptodate)
3062{
Josef Bacik5fd02042012-05-02 14:00:54 -04003063 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003064 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003066 struct btrfs_workqueue *wq;
3067 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003068
liubo1abe9b82011-03-24 11:18:59 +00003069 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3070
Chris Mason8b62b722009-09-02 16:53:46 -04003071 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003072 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3073 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003074 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003075
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003076 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003077 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003078 func = btrfs_freespace_write_helper;
3079 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003081 func = btrfs_endio_write_helper;
3082 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003083
Liu Bo9e0af232014-08-15 23:36:53 +08003084 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3085 NULL);
3086 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003087}
3088
Miao Xiedc380ae2014-09-12 18:43:55 +08003089static int __readpage_endio_check(struct inode *inode,
3090 struct btrfs_io_bio *io_bio,
3091 int icsum, struct page *page,
3092 int pgoff, u64 start, size_t len)
3093{
3094 char *kaddr;
3095 u32 csum_expected;
3096 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003097
3098 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3099
3100 kaddr = kmap_atomic(page);
3101 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003102 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003103 if (csum != csum_expected)
3104 goto zeroit;
3105
3106 kunmap_atomic(kaddr);
3107 return 0;
3108zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003109 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003110 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003111 memset(kaddr + pgoff, 1, len);
3112 flush_dcache_page(page);
3113 kunmap_atomic(kaddr);
3114 if (csum_expected == 0)
3115 return 0;
3116 return -EIO;
3117}
3118
Chris Masond352ac62008-09-29 15:18:18 -04003119/*
Chris Masond352ac62008-09-29 15:18:18 -04003120 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003121 * if there's a match, we allow the bio to finish. If not, the code in
3122 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003123 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003124static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3125 u64 phy_offset, struct page *page,
3126 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003127{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003128 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003129 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003130 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003131 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003132
Chris Masond20f7042008-12-08 16:58:54 -05003133 if (PageChecked(page)) {
3134 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003135 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003136 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003137
3138 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003139 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003140
Yan Zheng17d217f2008-12-12 10:03:38 -05003141 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003142 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003143 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003144 return 0;
3145 }
3146
Miao Xiefacc8a222013-07-25 19:22:34 +08003147 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003148 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3149 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003150}
Chris Masonb888db22007-08-27 16:49:44 -04003151
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003152void btrfs_add_delayed_iput(struct inode *inode)
3153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003155 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003156
3157 if (atomic_add_unless(&inode->i_count, -1, 1))
3158 return;
3159
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003160 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003161 if (binode->delayed_iput_count == 0) {
3162 ASSERT(list_empty(&binode->delayed_iput));
3163 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3164 } else {
3165 binode->delayed_iput_count++;
3166 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003167 spin_unlock(&fs_info->delayed_iput_lock);
3168}
3169
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003170void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003171{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003172
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003173 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003174 while (!list_empty(&fs_info->delayed_iputs)) {
3175 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003176
David Sterba8089fe62015-11-19 14:15:51 +01003177 inode = list_first_entry(&fs_info->delayed_iputs,
3178 struct btrfs_inode, delayed_iput);
3179 if (inode->delayed_iput_count) {
3180 inode->delayed_iput_count--;
3181 list_move_tail(&inode->delayed_iput,
3182 &fs_info->delayed_iputs);
3183 } else {
3184 list_del_init(&inode->delayed_iput);
3185 }
3186 spin_unlock(&fs_info->delayed_iput_lock);
3187 iput(&inode->vfs_inode);
3188 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003189 }
David Sterba8089fe62015-11-19 14:15:51 +01003190 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003191}
3192
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003194 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003195 * files in the subvolume, it removes orphan item and frees block_rsv
3196 * structure.
3197 */
3198void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3199 struct btrfs_root *root)
3200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003202 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003203 int ret;
3204
Josef Bacik8a35d952012-05-23 14:26:42 -04003205 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3207 return;
3208
Josef Bacik90290e12011-12-02 15:44:12 -05003209 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003210 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003211 spin_unlock(&root->orphan_lock);
3212 return;
3213 }
3214
3215 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3216 spin_unlock(&root->orphan_lock);
3217 return;
3218 }
3219
3220 block_rsv = root->orphan_block_rsv;
3221 root->orphan_block_rsv = NULL;
3222 spin_unlock(&root->orphan_lock);
3223
Miao Xie27cdeb72014-04-02 19:51:05 +08003224 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003226 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003227 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003228 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003229 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003230 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003231 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3232 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 }
3234
Josef Bacik90290e12011-12-02 15:44:12 -05003235 if (block_rsv) {
3236 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003237 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 }
3239}
3240
3241/*
Josef Bacik7b128762008-07-24 12:17:14 -04003242 * This creates an orphan entry for the given inode in case something goes
3243 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 *
3245 * NOTE: caller of this function should reserve 5 units of metadata for
3246 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003247 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003248int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3249 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003250{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3252 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 struct btrfs_block_rsv *block_rsv = NULL;
3254 int reserve = 0;
3255 int insert = 0;
3256 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003257
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003259 block_rsv = btrfs_alloc_block_rsv(fs_info,
3260 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003261 if (!block_rsv)
3262 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003263 }
3264
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 spin_lock(&root->orphan_lock);
3266 if (!root->orphan_block_rsv) {
3267 root->orphan_block_rsv = block_rsv;
3268 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003269 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 block_rsv = NULL;
3271 }
Josef Bacik7b128762008-07-24 12:17:14 -04003272
Josef Bacik8a35d952012-05-23 14:26:42 -04003273 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003274 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275#if 0
3276 /*
3277 * For proper ENOSPC handling, we should do orphan
3278 * cleanup when mounting. But this introduces backward
3279 * compatibility issue.
3280 */
3281 if (!xchg(&root->orphan_item_inserted, 1))
3282 insert = 2;
3283 else
3284 insert = 1;
3285#endif
3286 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003287 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 }
Josef Bacik7b128762008-07-24 12:17:14 -04003289
Josef Bacik72ac3c02012-05-23 14:13:11 -04003290 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003291 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 /* grab metadata reservation from transaction handle */
3296 if (reserve) {
3297 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003298 ASSERT(!ret);
3299 if (ret) {
3300 atomic_dec(&root->orphan_inodes);
3301 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003302 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003303 if (insert)
3304 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003305 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003306 return ret;
3307 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 }
3309
3310 /* insert an orphan item to track this unlinked/truncated file */
3311 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003312 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003313 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003314 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003315 if (reserve) {
3316 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003317 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003318 btrfs_orphan_release_metadata(inode);
3319 }
3320 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003321 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003322 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003323 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003324 return ret;
3325 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003326 }
3327 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003328 }
3329
3330 /* insert an orphan item to track subvolume contains orphan files */
3331 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003332 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003334 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003335 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003336 return ret;
3337 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 }
3339 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003340}
3341
3342/*
3343 * We have done the truncate/delete so we can go ahead and remove the orphan
3344 * item for this particular inode.
3345 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003346static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003347 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003348{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003349 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 int delete_item = 0;
3351 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003352 int ret = 0;
3353
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003355 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003356 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003358
Josef Bacik72ac3c02012-05-23 14:13:11 -04003359 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003360 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003361 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362 spin_unlock(&root->orphan_lock);
3363
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003364 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003365 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003366 if (trans)
3367 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003368 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003369 }
Josef Bacik7b128762008-07-24 12:17:14 -04003370
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003371 if (release_rsv)
3372 btrfs_orphan_release_metadata(inode);
3373
Josef Bacik4ef31a42013-08-13 14:10:08 -04003374 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003375}
3376
3377/*
3378 * this cleans up any orphans that may be left on the list from the last use
3379 * of this root.
3380 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003381int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003384 struct btrfs_path *path;
3385 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003386 struct btrfs_key key, found_key;
3387 struct btrfs_trans_handle *trans;
3388 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003389 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003390 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3391
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003393 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003394
3395 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003396 if (!path) {
3397 ret = -ENOMEM;
3398 goto out;
3399 }
David Sterbae4058b52015-11-27 16:31:35 +01003400 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003401
3402 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003403 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003404 key.offset = (u64)-1;
3405
Josef Bacik7b128762008-07-24 12:17:14 -04003406 while (1) {
3407 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003408 if (ret < 0)
3409 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003410
3411 /*
3412 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003413 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003414 * find the key and see if we have stuff that matches
3415 */
3416 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003417 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003418 if (path->slots[0] == 0)
3419 break;
3420 path->slots[0]--;
3421 }
3422
3423 /* pull out the item */
3424 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003425 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3426
3427 /* make sure the item matches what we want */
3428 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3429 break;
David Sterba962a2982014-06-04 18:41:45 +02003430 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003431 break;
3432
3433 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003434 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003435
3436 /*
3437 * this is where we are basically btrfs_lookup, without the
3438 * crossing root thing. we store the inode number in the
3439 * offset of the orphan item.
3440 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003441
3442 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003443 btrfs_err(fs_info,
3444 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003445 ret = -EINVAL;
3446 goto out;
3447 }
3448
3449 last_objectid = found_key.offset;
3450
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003451 found_key.objectid = found_key.offset;
3452 found_key.type = BTRFS_INODE_ITEM_KEY;
3453 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003454 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303455 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003456 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003458
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003460 struct btrfs_root *dead_root;
3461 struct btrfs_fs_info *fs_info = root->fs_info;
3462 int is_dead_root = 0;
3463
3464 /*
3465 * this is an orphan in the tree root. Currently these
3466 * could come from 2 sources:
3467 * a) a snapshot deletion in progress
3468 * b) a free space cache inode
3469 * We need to distinguish those two, as the snapshot
3470 * orphan must not get deleted.
3471 * find_dead_roots already ran before us, so if this
3472 * is a snapshot deletion, we should find the root
3473 * in the dead_roots list
3474 */
3475 spin_lock(&fs_info->trans_lock);
3476 list_for_each_entry(dead_root, &fs_info->dead_roots,
3477 root_list) {
3478 if (dead_root->root_key.objectid ==
3479 found_key.objectid) {
3480 is_dead_root = 1;
3481 break;
3482 }
3483 }
3484 spin_unlock(&fs_info->trans_lock);
3485 if (is_dead_root) {
3486 /* prevent this orphan from being found again */
3487 key.offset = found_key.objectid - 1;
3488 continue;
3489 }
3490 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003491 /*
3492 * Inode is already gone but the orphan item is still there,
3493 * kill the orphan item.
3494 */
Filipe Manana67710892016-06-06 11:51:25 +01003495 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003496 trans = btrfs_start_transaction(root, 1);
3497 if (IS_ERR(trans)) {
3498 ret = PTR_ERR(trans);
3499 goto out;
3500 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003501 btrfs_debug(fs_info, "auto deleting %Lu",
3502 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003503 ret = btrfs_del_orphan_item(trans, root,
3504 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003505 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003506 if (ret)
3507 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003508 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 }
Josef Bacik7b128762008-07-24 12:17:14 -04003510
Josef Bacik7b128762008-07-24 12:17:14 -04003511 /*
3512 * add this inode to the orphan list so btrfs_orphan_del does
3513 * the proper thing when we hit it
3514 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003515 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3516 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003517 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003518
Josef Bacik7b128762008-07-24 12:17:14 -04003519 /* if we have links, this was a truncate, lets do that */
3520 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303521 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003522 iput(inode);
3523 continue;
3524 }
Josef Bacik7b128762008-07-24 12:17:14 -04003525 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003526
3527 /* 1 for the orphan item deletion. */
3528 trans = btrfs_start_transaction(root, 1);
3529 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003530 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003531 ret = PTR_ERR(trans);
3532 goto out;
3533 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003534 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003535 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003536 if (ret) {
3537 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003538 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003539 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003540
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003541 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003542 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003543 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003544 } else {
3545 nr_unlink++;
3546 }
3547
3548 /* this will do delete_inode and everything for us */
3549 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003550 if (ret)
3551 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003552 }
Miao Xie3254c872011-11-10 20:45:05 -05003553 /* release the path since we're done with it */
3554 btrfs_release_path(path);
3555
Yan, Zhengd68fc572010-05-16 10:49:58 -04003556 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3557
3558 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003559 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003560 (u64)-1);
3561
Miao Xie27cdeb72014-04-02 19:51:05 +08003562 if (root->orphan_block_rsv ||
3563 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003564 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003565 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003566 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003567 }
Josef Bacik7b128762008-07-24 12:17:14 -04003568
3569 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003570 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003571 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003572 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003573
3574out:
3575 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003576 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003577 btrfs_free_path(path);
3578 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003579}
3580
Chris Masond352ac62008-09-29 15:18:18 -04003581/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003582 * very simple check to peek ahead in the leaf looking for xattrs. If we
3583 * don't find any xattrs, we know there can't be any acls.
3584 *
3585 * slot is the slot the inode is in, objectid is the objectid of the inode
3586 */
3587static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003588 int slot, u64 objectid,
3589 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003590{
3591 u32 nritems = btrfs_header_nritems(leaf);
3592 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003593 static u64 xattr_access = 0;
3594 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003595 int scanned = 0;
3596
Josef Bacikf23b5a52013-06-19 10:16:26 -04003597 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003598 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3599 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3600 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3601 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003602 }
3603
Chris Mason46a53cc2009-04-27 11:47:50 -04003604 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003605 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003606 while (slot < nritems) {
3607 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3608
3609 /* we found a different objectid, there must not be acls */
3610 if (found_key.objectid != objectid)
3611 return 0;
3612
3613 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003614 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003615 if (*first_xattr_slot == -1)
3616 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003617 if (found_key.offset == xattr_access ||
3618 found_key.offset == xattr_default)
3619 return 1;
3620 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003621
3622 /*
3623 * we found a key greater than an xattr key, there can't
3624 * be any acls later on
3625 */
3626 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3627 return 0;
3628
3629 slot++;
3630 scanned++;
3631
3632 /*
3633 * it goes inode, inode backrefs, xattrs, extents,
3634 * so if there are a ton of hard links to an inode there can
3635 * be a lot of backrefs. Don't waste time searching too hard,
3636 * this is just an optimization
3637 */
3638 if (scanned >= 8)
3639 break;
3640 }
3641 /* we hit the end of the leaf before we found an xattr or
3642 * something larger than an xattr. We have to assume the inode
3643 * has acls
3644 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003645 if (*first_xattr_slot == -1)
3646 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 return 1;
3648}
3649
3650/*
Chris Masond352ac62008-09-29 15:18:18 -04003651 * read an inode from the btree into the in-memory inode
3652 */
Filipe Manana67710892016-06-06 11:51:25 +01003653static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003654{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003656 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003657 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003658 struct btrfs_inode_item *inode_item;
3659 struct btrfs_root *root = BTRFS_I(inode)->root;
3660 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003661 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003662 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003663 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003665 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003666 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003667
3668 ret = btrfs_fill_inode(inode, &rdev);
3669 if (!ret)
3670 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003671
3672 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003673 if (!path) {
3674 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003675 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003676 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003677
Chris Mason39279cc2007-06-12 06:35:45 -04003678 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003679
Chris Mason39279cc2007-06-12 06:35:45 -04003680 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003681 if (ret) {
3682 if (ret > 0)
3683 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003684 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003685 }
Chris Mason39279cc2007-06-12 06:35:45 -04003686
Chris Mason5f39d392007-10-15 16:14:19 -04003687 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003688
3689 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003690 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003691
Chris Mason5f39d392007-10-15 16:14:19 -04003692 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3693 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003694 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003695 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003696 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3697 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003698 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003699
David Sterbaa937b972014-12-12 17:39:12 +01003700 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3701 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003702
David Sterbaa937b972014-12-12 17:39:12 +01003703 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3704 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003705
David Sterbaa937b972014-12-12 17:39:12 +01003706 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3707 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003708
chandan r9cc97d62012-07-04 12:48:07 +05303709 BTRFS_I(inode)->i_otime.tv_sec =
3710 btrfs_timespec_sec(leaf, &inode_item->otime);
3711 BTRFS_I(inode)->i_otime.tv_nsec =
3712 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003713
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003714 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003715 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003716 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3717
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003718 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003719 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003720 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003721 rdev = btrfs_inode_rdev(leaf, inode_item);
3722
Josef Bacikaec74772008-07-24 12:12:38 -04003723 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003724 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003725
3726cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003727 /*
3728 * If we were modified in the current generation and evicted from memory
3729 * and then re-read we need to do a full sync since we don't have any
3730 * idea about which extents were modified before we were evicted from
3731 * cache.
3732 *
3733 * This is required for both inode re-read from disk and delayed inode
3734 * in delayed_nodes_tree.
3735 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003736 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003737 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3738 &BTRFS_I(inode)->runtime_flags);
3739
Filipe Mananabde6c242015-07-24 00:00:19 +01003740 /*
3741 * We don't persist the id of the transaction where an unlink operation
3742 * against the inode was last made. So here we assume the inode might
3743 * have been evicted, and therefore the exact value of last_unlink_trans
3744 * lost, and set it to last_trans to avoid metadata inconsistencies
3745 * between the inode and its parent if the inode is fsync'ed and the log
3746 * replayed. For example, in the scenario:
3747 *
3748 * touch mydir/foo
3749 * ln mydir/foo mydir/bar
3750 * sync
3751 * unlink mydir/bar
3752 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3753 * xfs_io -c fsync mydir/foo
3754 * <power failure>
3755 * mount fs, triggers fsync log replay
3756 *
3757 * We must make sure that when we fsync our inode foo we also log its
3758 * parent inode, otherwise after log replay the parent still has the
3759 * dentry with the "bar" name but our inode foo has a link count of 1
3760 * and doesn't have an inode ref with the name "bar" anymore.
3761 *
3762 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003763 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003764 * transaction commits on fsync if our inode is a directory, or if our
3765 * inode is not a directory, logging its parent unnecessarily.
3766 */
3767 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3768
Miao Xie67de1172013-12-26 13:07:06 +08003769 path->slots[0]++;
3770 if (inode->i_nlink != 1 ||
3771 path->slots[0] >= btrfs_header_nritems(leaf))
3772 goto cache_acl;
3773
3774 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003775 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003776 goto cache_acl;
3777
3778 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3779 if (location.type == BTRFS_INODE_REF_KEY) {
3780 struct btrfs_inode_ref *ref;
3781
3782 ref = (struct btrfs_inode_ref *)ptr;
3783 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3784 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3785 struct btrfs_inode_extref *extref;
3786
3787 extref = (struct btrfs_inode_extref *)ptr;
3788 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3789 extref);
3790 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003791cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003792 /*
3793 * try to precache a NULL acl entry for files that don't have
3794 * any xattrs or acls
3795 */
Li Zefan33345d012011-04-20 10:31:50 +08003796 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003797 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003798 if (first_xattr_slot != -1) {
3799 path->slots[0] = first_xattr_slot;
3800 ret = btrfs_load_inode_props(inode, path);
3801 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003802 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003803 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003804 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003805 root->root_key.objectid, ret);
3806 }
3807 btrfs_free_path(path);
3808
Al Viro72c04902009-06-24 16:58:48 -04003809 if (!maybe_acls)
3810 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003811
Chris Mason39279cc2007-06-12 06:35:45 -04003812 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003813 case S_IFREG:
3814 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003815 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003816 inode->i_fop = &btrfs_file_operations;
3817 inode->i_op = &btrfs_file_inode_operations;
3818 break;
3819 case S_IFDIR:
3820 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003821 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003822 break;
3823 case S_IFLNK:
3824 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003825 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003826 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3827 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003828 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003829 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003830 init_special_inode(inode, inode->i_mode, rdev);
3831 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003832 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003833
3834 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003835 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003836
3837make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003838 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003839 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003840 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003841}
3842
Chris Masond352ac62008-09-29 15:18:18 -04003843/*
3844 * given a leaf and an inode, copy the inode fields into the leaf
3845 */
Chris Masone02119d2008-09-05 16:13:11 -04003846static void fill_inode_item(struct btrfs_trans_handle *trans,
3847 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003848 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003849 struct inode *inode)
3850{
Liu Bo51fab692012-12-27 09:01:21 +00003851 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003852
Liu Bo51fab692012-12-27 09:01:21 +00003853 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Liu Bo51fab692012-12-27 09:01:21 +00003855 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3856 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3857 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3858 &token);
3859 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3860 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003861
David Sterbaa937b972014-12-12 17:39:12 +01003862 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003863 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003864 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003865 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003866
David Sterbaa937b972014-12-12 17:39:12 +01003867 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003868 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003869 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003870 inode->i_mtime.tv_nsec, &token);
3871
David Sterbaa937b972014-12-12 17:39:12 +01003872 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003873 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003874 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003875 inode->i_ctime.tv_nsec, &token);
3876
chandan r9cc97d62012-07-04 12:48:07 +05303877 btrfs_set_token_timespec_sec(leaf, &item->otime,
3878 BTRFS_I(inode)->i_otime.tv_sec, &token);
3879 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3880 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3881
Liu Bo51fab692012-12-27 09:01:21 +00003882 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3883 &token);
3884 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3885 &token);
3886 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3887 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3888 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3889 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3890 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003891}
3892
Chris Masond352ac62008-09-29 15:18:18 -04003893/*
3894 * copy everything in the in-memory inode into the btree.
3895 */
Chris Mason21151332011-11-10 20:39:08 -05003896static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003897 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003898{
3899 struct btrfs_inode_item *inode_item;
3900 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 int ret;
3903
3904 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003905 if (!path)
3906 return -ENOMEM;
3907
Chris Masonb9473432009-03-13 11:00:37 -04003908 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003909 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3910 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003911 if (ret) {
3912 if (ret > 0)
3913 ret = -ENOENT;
3914 goto failed;
3915 }
3916
Chris Mason5f39d392007-10-15 16:14:19 -04003917 leaf = path->nodes[0];
3918 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003919 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
Chris Masone02119d2008-09-05 16:13:11 -04003921 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003922 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003923 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003924 ret = 0;
3925failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003926 btrfs_free_path(path);
3927 return ret;
3928}
3929
Chris Masond352ac62008-09-29 15:18:18 -04003930/*
Chris Mason21151332011-11-10 20:39:08 -05003931 * copy everything in the in-memory inode into the btree.
3932 */
3933noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3934 struct btrfs_root *root, struct inode *inode)
3935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003936 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003937 int ret;
3938
3939 /*
3940 * If the inode is a free space inode, we can deadlock during commit
3941 * if we put it into the delayed code.
3942 *
3943 * The data relocation inode should also be directly updated
3944 * without delay
3945 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003946 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003947 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003948 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003949 btrfs_update_root_times(trans, root);
3950
Chris Mason21151332011-11-10 20:39:08 -05003951 ret = btrfs_delayed_update_inode(trans, root, inode);
3952 if (!ret)
3953 btrfs_set_inode_last_trans(trans, inode);
3954 return ret;
3955 }
3956
3957 return btrfs_update_inode_item(trans, root, inode);
3958}
3959
Josef Bacikbe6aef62012-10-22 15:43:12 -04003960noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3961 struct btrfs_root *root,
3962 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003963{
3964 int ret;
3965
3966 ret = btrfs_update_inode(trans, root, inode);
3967 if (ret == -ENOSPC)
3968 return btrfs_update_inode_item(trans, root, inode);
3969 return ret;
3970}
3971
3972/*
Chris Masond352ac62008-09-29 15:18:18 -04003973 * unlink helper that gets used here in inode.c and in the tree logging
3974 * recovery code. It remove a link in a directory with a given name, and
3975 * also drops the back refs in the inode to the directory
3976 */
Al Viro92986792011-03-04 17:14:37 +00003977static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3978 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003979 struct btrfs_inode *dir,
3980 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003981 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003982{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003984 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003986 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003987 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003989 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003990 u64 ino = btrfs_ino(inode);
3991 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003992
3993 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003994 if (!path) {
3995 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003996 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003997 }
3998
Chris Masonb9473432009-03-13 11:00:37 -04003999 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004000 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004001 name, name_len, -1);
4002 if (IS_ERR(di)) {
4003 ret = PTR_ERR(di);
4004 goto err;
4005 }
4006 if (!di) {
4007 ret = -ENOENT;
4008 goto err;
4009 }
Chris Mason5f39d392007-10-15 16:14:19 -04004010 leaf = path->nodes[0];
4011 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004012 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004013 if (ret)
4014 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004015 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004016
Miao Xie67de1172013-12-26 13:07:06 +08004017 /*
4018 * If we don't have dir index, we have to get it by looking up
4019 * the inode ref, since we get the inode ref, remove it directly,
4020 * it is unnecessary to do delayed deletion.
4021 *
4022 * But if we have dir index, needn't search inode ref to get it.
4023 * Since the inode ref is close to the inode item, it is better
4024 * that we delay to delete it, and just do this deletion when
4025 * we update the inode item.
4026 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004027 if (inode->dir_index) {
4028 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004029 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004030 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004031 goto skip_backref;
4032 }
4033 }
4034
Li Zefan33345d012011-04-20 10:31:50 +08004035 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4036 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004037 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004038 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004039 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004040 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004041 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004042 goto err;
4043 }
Miao Xie67de1172013-12-26 13:07:06 +08004044skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004045 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004046 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004047 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004048 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004049 }
Chris Mason39279cc2007-06-12 06:35:45 -04004050
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4052 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004053 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004054 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 goto err;
4056 }
Chris Masone02119d2008-09-05 16:13:11 -04004057
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004058 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4059 index);
Chris Mason6418c962010-10-30 07:34:24 -04004060 if (ret == -ENOENT)
4061 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004062 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004063 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004064err:
4065 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004066 if (ret)
4067 goto out;
4068
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004069 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004070 inode_inc_iversion(&inode->vfs_inode);
4071 inode_inc_iversion(&dir->vfs_inode);
4072 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4073 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4074 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004075out:
Chris Mason39279cc2007-06-12 06:35:45 -04004076 return ret;
4077}
4078
Al Viro92986792011-03-04 17:14:37 +00004079int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4080 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004081 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004082 const char *name, int name_len)
4083{
4084 int ret;
4085 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4086 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004087 drop_nlink(&inode->vfs_inode);
4088 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004089 }
4090 return ret;
4091}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004092
4093/*
4094 * helper to start transaction for unlink and rmdir.
4095 *
Josef Bacikd52be812013-05-29 14:54:47 -04004096 * unlink and rmdir are special in btrfs, they do not always free space, so
4097 * if we cannot make our reservations the normal way try and see if there is
4098 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4099 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004100 */
Josef Bacikd52be812013-05-29 14:54:47 -04004101static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004102{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004103 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004104
Josef Bacike70bea52011-10-11 14:18:24 -04004105 /*
4106 * 1 for the possible orphan item
4107 * 1 for the dir item
4108 * 1 for the dir index
4109 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004110 * 1 for the inode
4111 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004112 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004113}
4114
Chris Mason39279cc2007-06-12 06:35:45 -04004115static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4116{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004117 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004118 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004119 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004120 int ret;
4121
Josef Bacikd52be812013-05-29 14:54:47 -04004122 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004123 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004124 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004125
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004126 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4127 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004128
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4130 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4131 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004132 if (ret)
4133 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004134
Yan, Zhenga22285a2010-05-16 10:48:46 -04004135 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004136 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004137 if (ret)
4138 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139 }
Josef Bacik7b128762008-07-24 12:17:14 -04004140
Tsutomu Itohb5324022011-07-19 07:27:20 +00004141out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004142 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004143 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004144 return ret;
4145}
4146
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4148 struct btrfs_root *root,
4149 struct inode *dir, u64 objectid,
4150 const char *name, int name_len)
4151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004152 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 struct btrfs_path *path;
4154 struct extent_buffer *leaf;
4155 struct btrfs_dir_item *di;
4156 struct btrfs_key key;
4157 u64 index;
4158 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004159 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004160
4161 path = btrfs_alloc_path();
4162 if (!path)
4163 return -ENOMEM;
4164
Li Zefan33345d012011-04-20 10:31:50 +08004165 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167 if (IS_ERR_OR_NULL(di)) {
4168 if (!di)
4169 ret = -ENOENT;
4170 else
4171 ret = PTR_ERR(di);
4172 goto out;
4173 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004174
4175 leaf = path->nodes[0];
4176 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4177 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4178 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004179 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004180 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004181 goto out;
4182 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004183 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004184
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004185 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4186 root->root_key.objectid, dir_ino,
4187 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004188 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004189 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004190 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004191 goto out;
4192 }
Li Zefan33345d012011-04-20 10:31:50 +08004193 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004195 if (IS_ERR_OR_NULL(di)) {
4196 if (!di)
4197 ret = -ENOENT;
4198 else
4199 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004200 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 goto out;
4202 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004203
4204 leaf = path->nodes[0];
4205 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004206 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207 index = key.offset;
4208 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004209 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004210
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004211 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004212 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004213 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004214 goto out;
4215 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004216
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004217 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004218 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004219 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004220 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004221 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004222 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004224 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226}
4227
Chris Mason39279cc2007-06-12 06:35:45 -04004228static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4229{
David Howells2b0143b2015-03-17 22:25:59 +00004230 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004231 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004232 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004233 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004234 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004235
David Sterbab3ae2442012-09-13 16:04:34 -06004236 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004237 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004238 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004239 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004240
Josef Bacikd52be812013-05-29 14:54:47 -04004241 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004242 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004243 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004244
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004245 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 err = btrfs_unlink_subvol(trans, root, dir,
4247 BTRFS_I(inode)->location.objectid,
4248 dentry->d_name.name,
4249 dentry->d_name.len);
4250 goto out;
4251 }
4252
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004253 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004254 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004256
Filipe Manana44f714d2016-06-06 16:11:13 +01004257 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4258
Chris Mason39279cc2007-06-12 06:35:45 -04004259 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004260 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4261 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4262 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004263 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004264 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004265 /*
4266 * Propagate the last_unlink_trans value of the deleted dir to
4267 * its parent directory. This is to prevent an unrecoverable
4268 * log tree in the case we do something like this:
4269 * 1) create dir foo
4270 * 2) create snapshot under dir foo
4271 * 3) delete the snapshot
4272 * 4) rmdir foo
4273 * 5) mkdir foo
4274 * 6) fsync foo or some file inside foo
4275 */
4276 if (last_unlink_trans >= trans->transid)
4277 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4278 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004280 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004281 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004282
Chris Mason39279cc2007-06-12 06:35:45 -04004283 return err;
4284}
4285
Chris Mason28f75a02015-02-04 06:59:29 -08004286static int truncate_space_check(struct btrfs_trans_handle *trans,
4287 struct btrfs_root *root,
4288 u64 bytes_deleted)
4289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004290 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004291 int ret;
4292
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004293 /*
4294 * This is only used to apply pressure to the enospc system, we don't
4295 * intend to use this reservation at all.
4296 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004297 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004298 bytes_deleted *= fs_info->nodesize;
4299 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004300 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004301 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004302 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004303 trans->transid,
4304 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004305 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004306 }
Chris Mason28f75a02015-02-04 06:59:29 -08004307 return ret;
4308
4309}
4310
Filipe Manana0305cd52015-10-16 12:34:25 +01004311static int truncate_inline_extent(struct inode *inode,
4312 struct btrfs_path *path,
4313 struct btrfs_key *found_key,
4314 const u64 item_end,
4315 const u64 new_size)
4316{
4317 struct extent_buffer *leaf = path->nodes[0];
4318 int slot = path->slots[0];
4319 struct btrfs_file_extent_item *fi;
4320 u32 size = (u32)(new_size - found_key->offset);
4321 struct btrfs_root *root = BTRFS_I(inode)->root;
4322
4323 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4324
4325 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4326 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004327 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004328
4329 /*
4330 * Zero out the remaining of the last page of our inline extent,
4331 * instead of directly truncating our inline extent here - that
4332 * would be much more complex (decompressing all the data, then
4333 * compressing the truncated data, which might be bigger than
4334 * the size of the inline extent, resize the extent, etc).
4335 * We release the path because to get the page we might need to
4336 * read the extent item from disk (data not in the page cache).
4337 */
4338 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304339 return btrfs_truncate_block(inode, offset, page_end - offset,
4340 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004341 }
4342
4343 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4344 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004345 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004346
4347 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4348 inode_sub_bytes(inode, item_end + 1 - new_size);
4349
4350 return 0;
4351}
4352
Chris Mason323ac952008-10-01 19:05:46 -04004353/*
Chris Mason39279cc2007-06-12 06:35:45 -04004354 * this can truncate away extent items, csum items and directory items.
4355 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004356 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004357 *
4358 * csum items that cross the new i_size are truncated to the new size
4359 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004360 *
4361 * min_type is the minimum key type to truncate down to. If set to 0, this
4362 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004363 */
Yan, Zheng80825102009-11-12 09:35:36 +00004364int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4365 struct btrfs_root *root,
4366 struct inode *inode,
4367 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004368{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004369 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004370 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004371 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004373 struct btrfs_key key;
4374 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004375 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004376 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004377 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004378 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004379 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004380 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 int found_extent;
4382 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004383 int pending_del_nr = 0;
4384 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004385 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004386 int ret;
4387 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004388 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004389 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004390 bool be_nice = 0;
4391 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004392 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004393
4394 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004395
Chris Mason28ed1342014-12-17 09:41:04 -08004396 /*
4397 * for non-free space inodes and ref cows, we want to back off from
4398 * time to time
4399 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004400 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004401 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4402 be_nice = 1;
4403
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004404 path = btrfs_alloc_path();
4405 if (!path)
4406 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004407 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004408
Josef Bacik5dc562c2012-08-17 13:14:17 -04004409 /*
4410 * We want to drop from the next block forward in case this new size is
4411 * not block aligned since we will be keeping the last block of the
4412 * extent just the way it is.
4413 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004414 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004415 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004416 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004418 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004419
Miao Xie16cdcec2011-04-22 18:12:22 +08004420 /*
4421 * This function is also used to drop the items in the log tree before
4422 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4423 * it is used to drop the loged items. So we shouldn't kill the delayed
4424 * items.
4425 */
4426 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004427 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004428
Li Zefan33345d012011-04-20 10:31:50 +08004429 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004431 key.type = (u8)-1;
4432
Chris Mason85e21ba2008-01-29 15:11:36 -05004433search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004434 /*
4435 * with a 16K leaf size and 128MB extents, you can actually queue
4436 * up a huge file in a single leaf. Most of the time that
4437 * bytes_deleted is > 0, it will be huge by the time we get here
4438 */
Byongho Leeee221842015-12-15 01:42:10 +09004439 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004440 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004441 err = -EAGAIN;
4442 goto error;
4443 }
4444 }
4445
4446
Chris Masonb9473432009-03-13 11:00:37 -04004447 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004448 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004449 if (ret < 0) {
4450 err = ret;
4451 goto out;
4452 }
Chris Masond3977122009-01-05 21:25:51 -05004453
Chris Mason85e21ba2008-01-29 15:11:36 -05004454 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004455 /* there are no items in the tree for us to truncate, we're
4456 * done
4457 */
Yan, Zheng80825102009-11-12 09:35:36 +00004458 if (path->slots[0] == 0)
4459 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004460 path->slots[0]--;
4461 }
4462
Chris Masond3977122009-01-05 21:25:51 -05004463 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004464 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004465 leaf = path->nodes[0];
4466 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004467 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004468
Li Zefan33345d012011-04-20 10:31:50 +08004469 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004470 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004471
Chris Mason85e21ba2008-01-29 15:11:36 -05004472 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004473 break;
4474
Chris Mason5f39d392007-10-15 16:14:19 -04004475 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004477 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004478 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004479 extent_type = btrfs_file_extent_type(leaf, fi);
4480 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004481 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004482 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004483 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004484 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004485 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004486 }
Yan008630c2007-11-07 13:31:09 -05004487 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004488 }
Yan, Zheng80825102009-11-12 09:35:36 +00004489 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004490 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004491 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004492 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004493 break;
4494 if (found_key.offset >= new_size)
4495 del_item = 1;
4496 else
4497 del_item = 0;
4498 }
Chris Mason39279cc2007-06-12 06:35:45 -04004499 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004501 if (found_type != BTRFS_EXTENT_DATA_KEY)
4502 goto delete;
4503
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004504 if (del_item)
4505 last_size = found_key.offset;
4506 else
4507 last_size = new_size;
4508
Chris Mason179e29e2007-11-01 11:28:41 -04004509 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004510 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004511 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004512 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004513 u64 orig_num_bytes =
4514 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004515 extent_num_bytes = ALIGN(new_size -
4516 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004517 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004518 btrfs_set_file_extent_num_bytes(leaf, fi,
4519 extent_num_bytes);
4520 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004521 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004522 if (test_bit(BTRFS_ROOT_REF_COWS,
4523 &root->state) &&
4524 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004525 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004526 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004527 } else {
Chris Masondb945352007-10-15 16:15:53 -04004528 extent_num_bytes =
4529 btrfs_file_extent_disk_num_bytes(leaf,
4530 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004531 extent_offset = found_key.offset -
4532 btrfs_file_extent_offset(leaf, fi);
4533
Chris Mason39279cc2007-06-12 06:35:45 -04004534 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004535 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004536 if (extent_start != 0) {
4537 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004538 if (test_bit(BTRFS_ROOT_REF_COWS,
4539 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004540 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004541 }
4542 }
Chris Mason90692182008-02-08 13:49:28 -05004543 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004544 /*
4545 * we can't truncate inline items that have had
4546 * special encodings
4547 */
4548 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004549 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4550 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004551
4552 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004553 * Need to release path in order to truncate a
4554 * compressed extent. So delete any accumulated
4555 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004556 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004557 if (btrfs_file_extent_compression(leaf, fi) !=
4558 BTRFS_COMPRESS_NONE && pending_del_nr) {
4559 err = btrfs_del_items(trans, root, path,
4560 pending_del_slot,
4561 pending_del_nr);
4562 if (err) {
4563 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004564 err);
4565 goto error;
4566 }
4567 pending_del_nr = 0;
4568 }
4569
4570 err = truncate_inline_extent(inode, path,
4571 &found_key,
4572 item_end,
4573 new_size);
4574 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004575 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004576 goto error;
4577 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004578 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4579 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004580 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004581 }
Chris Mason39279cc2007-06-12 06:35:45 -04004582 }
Chris Mason179e29e2007-11-01 11:28:41 -04004583delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004584 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004585 if (!pending_del_nr) {
4586 /* no pending yet, add ourselves */
4587 pending_del_slot = path->slots[0];
4588 pending_del_nr = 1;
4589 } else if (pending_del_nr &&
4590 path->slots[0] + 1 == pending_del_slot) {
4591 /* hop on the pending chunk */
4592 pending_del_nr++;
4593 pending_del_slot = path->slots[0];
4594 } else {
Chris Masond3977122009-01-05 21:25:51 -05004595 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004596 }
Chris Mason39279cc2007-06-12 06:35:45 -04004597 } else {
4598 break;
4599 }
Chris Mason28f75a02015-02-04 06:59:29 -08004600 should_throttle = 0;
4601
Miao Xie27cdeb72014-04-02 19:51:05 +08004602 if (found_extent &&
4603 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004604 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004605 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004606 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004607 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004608 extent_num_bytes, 0,
4609 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004610 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004612 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4613 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004614 trans->delayed_ref_updates * 2,
4615 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004616 if (be_nice) {
4617 if (truncate_space_check(trans, root,
4618 extent_num_bytes)) {
4619 should_end = 1;
4620 }
4621 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004622 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004623 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004624 }
Chris Mason39279cc2007-06-12 06:35:45 -04004625 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004626
Yan, Zheng80825102009-11-12 09:35:36 +00004627 if (found_type == BTRFS_INODE_ITEM_KEY)
4628 break;
4629
4630 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004631 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004632 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004633 if (pending_del_nr) {
4634 ret = btrfs_del_items(trans, root, path,
4635 pending_del_slot,
4636 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004637 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004638 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004639 goto error;
4640 }
Yan, Zheng80825102009-11-12 09:35:36 +00004641 pending_del_nr = 0;
4642 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004643 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004644 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004645 unsigned long updates = trans->delayed_ref_updates;
4646 if (updates) {
4647 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004648 ret = btrfs_run_delayed_refs(trans,
4649 fs_info,
4650 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004651 if (ret && !err)
4652 err = ret;
4653 }
4654 }
Chris Mason28f75a02015-02-04 06:59:29 -08004655 /*
4656 * if we failed to refill our space rsv, bail out
4657 * and let the transaction restart
4658 */
4659 if (should_end) {
4660 err = -EAGAIN;
4661 goto error;
4662 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004663 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004664 } else {
4665 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004666 }
Chris Mason39279cc2007-06-12 06:35:45 -04004667 }
Yan, Zheng80825102009-11-12 09:35:36 +00004668out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004669 if (pending_del_nr) {
4670 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4671 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004672 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004673 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004674 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004675error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004676 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4677 ASSERT(last_size >= new_size);
4678 if (!err && last_size > new_size)
4679 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004680 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004681 }
Chris Mason28ed1342014-12-17 09:41:04 -08004682
Chris Mason39279cc2007-06-12 06:35:45 -04004683 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004684
Liu Bo19fd2df2016-12-01 13:01:02 -08004685 if (err == 0) {
4686 /* only inline file may have last_size != new_size */
4687 if (new_size >= fs_info->sectorsize ||
4688 new_size > fs_info->max_inline)
4689 ASSERT(last_size == new_size);
4690 }
4691
Byongho Leeee221842015-12-15 01:42:10 +09004692 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004693 unsigned long updates = trans->delayed_ref_updates;
4694 if (updates) {
4695 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004696 ret = btrfs_run_delayed_refs(trans, fs_info,
4697 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004698 if (ret && !err)
4699 err = ret;
4700 }
4701 }
Yan, Zheng80825102009-11-12 09:35:36 +00004702 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004703}
4704
Chris Masona52d9a82007-08-27 16:49:44 -04004705/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004707 * @inode - inode that we're zeroing
4708 * @from - the offset to start zeroing
4709 * @len - the length to zero, 0 to zero the entire range respective to the
4710 * offset
4711 * @front - zero up to the offset instead of from the offset on
4712 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304713 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004714 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004715 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304716int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004717 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004718{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004720 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004721 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4722 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004723 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004724 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004725 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004726 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004728 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004729 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004730 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304731 u64 block_start;
4732 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004733
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 if ((offset & (blocksize - 1)) == 0 &&
4735 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004736 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304737
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004738 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304739 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004740 if (ret)
4741 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004742
Chris Mason211c17f2008-05-15 09:13:45 -04004743again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004744 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004745 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004746 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 round_down(from, blocksize),
4748 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004749 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004750 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004751 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 block_start = round_down(from, blocksize);
4754 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004755
Chris Masona52d9a82007-08-27 16:49:44 -04004756 if (!PageUptodate(page)) {
4757 ret = btrfs_readpage(NULL, page);
4758 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004759 if (page->mapping != mapping) {
4760 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004761 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004762 goto again;
4763 }
Chris Masona52d9a82007-08-27 16:49:44 -04004764 if (!PageUptodate(page)) {
4765 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004766 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004767 }
4768 }
Chris Mason211c17f2008-05-15 09:13:45 -04004769 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304771 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004772 set_page_extent_mapped(page);
4773
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304776 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004779 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004780 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 btrfs_put_ordered_extent(ordered);
4782 goto again;
4783 }
4784
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004786 EXTENT_DIRTY | EXTENT_DELALLOC |
4787 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004788 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004789
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004791 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004792 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004794 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004795 goto out_unlock;
4796 }
4797
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004799 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304800 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004802 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 memset(kaddr + (block_start - page_offset(page)),
4804 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004805 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 memset(kaddr + (block_start - page_offset(page)) + offset,
4807 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004808 flush_dcache_page(page);
4809 kunmap(page);
4810 }
Chris Mason247e7432008-07-17 12:53:51 -04004811 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004812 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004814 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004815
Chris Mason89642222008-07-24 09:41:53 -04004816out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004817 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 btrfs_delalloc_release_space(inode, block_start,
4819 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004820 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004821 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004822out:
4823 return ret;
4824}
4825
Josef Bacik16e75492013-10-22 12:18:51 -04004826static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4827 u64 offset, u64 len)
4828{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004830 struct btrfs_trans_handle *trans;
4831 int ret;
4832
4833 /*
4834 * Still need to make sure the inode looks like it's been updated so
4835 * that any holes get logged if we fsync.
4836 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004837 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4838 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004839 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4840 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4841 return 0;
4842 }
4843
4844 /*
4845 * 1 - for the one we're dropping
4846 * 1 - for the one we're adding
4847 * 1 - for updating the inode.
4848 */
4849 trans = btrfs_start_transaction(root, 3);
4850 if (IS_ERR(trans))
4851 return PTR_ERR(trans);
4852
4853 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4854 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004855 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004856 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004857 return ret;
4858 }
4859
David Sterbaf85b7372017-01-20 14:54:07 +01004860 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4861 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004862 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004863 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004864 else
4865 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004866 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004867 return ret;
4868}
4869
Josef Bacik695a0d02011-03-04 15:46:53 -05004870/*
4871 * This function puts in dummy file extents for the area we're creating a hole
4872 * for. So if we are truncating this file to a larger size we need to insert
4873 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4874 * the range between oldsize and size
4875 */
Josef Bacika41ad392011-01-31 15:30:16 -05004876int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004879 struct btrfs_root *root = BTRFS_I(inode)->root;
4880 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004881 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004882 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004883 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4885 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004886 u64 last_byte;
4887 u64 cur_offset;
4888 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004889 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004890
Josef Bacika71754f2013-06-17 17:14:39 -04004891 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304892 * If our size started in the middle of a block we need to zero out the
4893 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004894 * expose stale data.
4895 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304896 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004897 if (err)
4898 return err;
4899
Yan Zheng9036c102008-10-30 14:19:41 -04004900 if (size <= hole_start)
4901 return 0;
4902
Yan Zheng9036c102008-10-30 14:19:41 -04004903 while (1) {
4904 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004905
David Sterbaff13db42015-12-03 14:30:40 +01004906 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004907 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004908 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004909 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004910 if (!ordered)
4911 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004912 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4913 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004914 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004915 btrfs_put_ordered_extent(ordered);
4916 }
4917
Yan Zheng9036c102008-10-30 14:19:41 -04004918 cur_offset = hole_start;
4919 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004920 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004921 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004922 if (IS_ERR(em)) {
4923 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004924 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004925 break;
4926 }
Yan Zheng9036c102008-10-30 14:19:41 -04004927 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004929 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004930 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004931 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004932
Josef Bacik16e75492013-10-22 12:18:51 -04004933 err = maybe_insert_hole(root, inode, cur_offset,
4934 hole_size);
4935 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004936 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004937 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 cur_offset + hole_size - 1, 0);
4939 hole_em = alloc_extent_map();
4940 if (!hole_em) {
4941 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4942 &BTRFS_I(inode)->runtime_flags);
4943 goto next;
4944 }
4945 hole_em->start = cur_offset;
4946 hole_em->len = hole_size;
4947 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004948
Josef Bacik5dc562c2012-08-17 13:14:17 -04004949 hole_em->block_start = EXTENT_MAP_HOLE;
4950 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004951 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004952 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004953 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004954 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004955 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004956
4957 while (1) {
4958 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004959 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004960 write_unlock(&em_tree->lock);
4961 if (err != -EEXIST)
4962 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004963 btrfs_drop_extent_cache(BTRFS_I(inode),
4964 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004965 cur_offset +
4966 hole_size - 1, 0);
4967 }
4968 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004969 }
Josef Bacik16e75492013-10-22 12:18:51 -04004970next:
Yan Zheng9036c102008-10-30 14:19:41 -04004971 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004972 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004973 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004974 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004975 break;
4976 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004977 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004978 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4979 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004980 return err;
4981}
4982
Eric Sandeen3972f262013-01-12 02:57:22 +00004983static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004984{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004985 struct btrfs_root *root = BTRFS_I(inode)->root;
4986 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004987 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004988 loff_t newsize = attr->ia_size;
4989 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004990 int ret;
4991
Eric Sandeen3972f262013-01-12 02:57:22 +00004992 /*
4993 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4994 * special case where we need to update the times despite not having
4995 * these flags set. For all other operations the VFS set these flags
4996 * explicitly if it wants a timestamp update.
4997 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004998 if (newsize != oldsize) {
4999 inode_inc_iversion(inode);
5000 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5001 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005002 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005003 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005004
Josef Bacika41ad392011-01-31 15:30:16 -05005005 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005006 /*
5007 * Don't do an expanding truncate while snapshoting is ongoing.
5008 * This is to ensure the snapshot captures a fully consistent
5009 * state of this file - if the snapshot captures this expanding
5010 * truncation, it must capture all writes that happened before
5011 * this truncation.
5012 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005013 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005014 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005015 if (ret) {
5016 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005017 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005018 }
Yan, Zheng80825102009-11-12 09:35:36 +00005019
Miao Xief4a2f4c2011-12-14 20:12:01 -05005020 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005021 if (IS_ERR(trans)) {
5022 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005023 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005024 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005025
5026 i_size_write(inode, newsize);
5027 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305028 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005029 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005030 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005031 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005032 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005033
Josef Bacika41ad392011-01-31 15:30:16 -05005034 /*
5035 * We're truncating a file that used to have good data down to
5036 * zero. Make sure it gets into the ordered flush list so that
5037 * any new writes get down to disk quickly.
5038 */
5039 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005040 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5041 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005042
Josef Bacikf3fe8202013-01-07 17:03:21 -05005043 /*
5044 * 1 for the orphan item we're going to add
5045 * 1 for the orphan item deletion.
5046 */
5047 trans = btrfs_start_transaction(root, 2);
5048 if (IS_ERR(trans))
5049 return PTR_ERR(trans);
5050
5051 /*
5052 * We need to do this in case we fail at _any_ point during the
5053 * actual truncate. Once we do the truncate_setsize we could
5054 * invalidate pages which forces any outstanding ordered io to
5055 * be instantly completed which will give us extents that need
5056 * to be truncated. If we fail to get an orphan inode down we
5057 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005058 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005059 * will be consistent.
5060 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005061 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005062 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005063 if (ret)
5064 return ret;
5065
Josef Bacika41ad392011-01-31 15:30:16 -05005066 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5067 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005068
5069 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005070 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005071 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005072 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005073
Josef Bacika41ad392011-01-31 15:30:16 -05005074 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005075 if (ret && inode->i_nlink) {
5076 int err;
5077
Liu Bo19fd2df2016-12-01 13:01:02 -08005078 /* To get a stable disk_i_size */
5079 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5080 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005081 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005082 return err;
5083 }
5084
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005085 /*
5086 * failed to truncate, disk_i_size is only adjusted down
5087 * as we remove extents, so it should represent the true
5088 * size of the inode, so reset the in memory size and
5089 * delete our orphan entry.
5090 */
5091 trans = btrfs_join_transaction(root);
5092 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005093 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005094 return ret;
5095 }
5096 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005097 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005098 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005099 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005100 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005101 }
Yan, Zheng80825102009-11-12 09:35:36 +00005102 }
5103
Josef Bacika41ad392011-01-31 15:30:16 -05005104 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005105}
5106
Chris Mason39279cc2007-06-12 06:35:45 -04005107static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5108{
David Howells2b0143b2015-03-17 22:25:59 +00005109 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005110 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005111 int err;
5112
Li Zefanb83cc962010-12-20 16:04:08 +08005113 if (btrfs_root_readonly(root))
5114 return -EROFS;
5115
Jan Kara31051c82016-05-26 16:55:18 +02005116 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005117 if (err)
5118 return err;
5119
Chris Mason5a3f23d2009-03-31 13:27:11 -04005120 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005121 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005122 if (err)
5123 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005124 }
Yan Zheng9036c102008-10-30 14:19:41 -04005125
Christoph Hellwig10257742010-06-04 11:30:02 +02005126 if (attr->ia_valid) {
5127 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005128 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005129 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005130
Josef Bacik22c44fe2011-11-30 10:45:38 -05005131 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005132 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005133 }
5134
Chris Mason39279cc2007-06-12 06:35:45 -04005135 return err;
5136}
Chris Mason61295eb2008-01-14 16:24:38 -05005137
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005138/*
5139 * While truncating the inode pages during eviction, we get the VFS calling
5140 * btrfs_invalidatepage() against each page of the inode. This is slow because
5141 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5142 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5143 * extent_state structures over and over, wasting lots of time.
5144 *
5145 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5146 * those expensive operations on a per page basis and do only the ordered io
5147 * finishing, while we release here the extent_map and extent_state structures,
5148 * without the excessive merging and splitting.
5149 */
5150static void evict_inode_truncate_pages(struct inode *inode)
5151{
5152 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5153 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5154 struct rb_node *node;
5155
5156 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005157 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005158
5159 write_lock(&map_tree->lock);
5160 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5161 struct extent_map *em;
5162
5163 node = rb_first(&map_tree->map);
5164 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005165 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5166 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005167 remove_extent_mapping(map_tree, em);
5168 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005169 if (need_resched()) {
5170 write_unlock(&map_tree->lock);
5171 cond_resched();
5172 write_lock(&map_tree->lock);
5173 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005174 }
5175 write_unlock(&map_tree->lock);
5176
Filipe Manana6ca07092015-05-26 00:55:42 +01005177 /*
5178 * Keep looping until we have no more ranges in the io tree.
5179 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005180 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5181 * still in progress (unlocked the pages in the bio but did not yet
5182 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005183 * ranges can still be locked and eviction started because before
5184 * submitting those bios, which are executed by a separate task (work
5185 * queue kthread), inode references (inode->i_count) were not taken
5186 * (which would be dropped in the end io callback of each bio).
5187 * Therefore here we effectively end up waiting for those bios and
5188 * anyone else holding locked ranges without having bumped the inode's
5189 * reference count - if we don't do it, when they access the inode's
5190 * io_tree to unlock a range it may be too late, leading to an
5191 * use-after-free issue.
5192 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005193 spin_lock(&io_tree->lock);
5194 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5195 struct extent_state *state;
5196 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005197 u64 start;
5198 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005199
5200 node = rb_first(&io_tree->state);
5201 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005202 start = state->start;
5203 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005204 spin_unlock(&io_tree->lock);
5205
David Sterbaff13db42015-12-03 14:30:40 +01005206 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005207
5208 /*
5209 * If still has DELALLOC flag, the extent didn't reach disk,
5210 * and its reserved space won't be freed by delayed_ref.
5211 * So we need to free its reserved space here.
5212 * (Refer to comment in btrfs_invalidatepage, case 2)
5213 *
5214 * Note, end is the bytenr of last byte, so we need + 1 here.
5215 */
5216 if (state->state & EXTENT_DELALLOC)
5217 btrfs_qgroup_free_data(inode, start, end - start + 1);
5218
Filipe Manana6ca07092015-05-26 00:55:42 +01005219 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005220 EXTENT_LOCKED | EXTENT_DIRTY |
5221 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5222 EXTENT_DEFRAG, 1, 1,
5223 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005224
Filipe Manana7064dd52014-08-08 02:47:05 +01005225 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005226 spin_lock(&io_tree->lock);
5227 }
5228 spin_unlock(&io_tree->lock);
5229}
5230
Al Virobd555972010-06-07 11:35:40 -04005231void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005232{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005233 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005234 struct btrfs_trans_handle *trans;
5235 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005236 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005237 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005238 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005239 int ret;
5240
liubo1abe9b82011-03-24 11:18:59 +00005241 trace_btrfs_inode_evict(inode);
5242
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005243 if (!root) {
5244 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5245 return;
5246 }
5247
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005249
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005250 evict_inode_truncate_pages(inode);
5251
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005252 if (inode->i_nlink &&
5253 ((btrfs_root_refs(&root->root_item) != 0 &&
5254 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005255 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005256 goto no_delete;
5257
Chris Mason39279cc2007-06-12 06:35:45 -04005258 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005259 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005260 goto no_delete;
5261 }
Al Virobd555972010-06-07 11:35:40 -04005262 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005263 if (!special_file(inode->i_mode))
5264 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005265
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005266 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005267
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005268 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005269 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005270 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005271 goto no_delete;
5272 }
5273
Yan, Zheng76dda932009-09-21 16:00:26 -04005274 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005275 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5276 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005277 goto no_delete;
5278 }
5279
Nikolay Borisovaa790212017-01-10 20:35:40 +02005280 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005281 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005282 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005283 goto no_delete;
5284 }
5285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005286 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005287 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005288 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005289 goto no_delete;
5290 }
Josef Bacik4a338542011-08-29 11:01:31 -04005291 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005292 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005293 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005294
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005295 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005296
Josef Bacik4289a662011-08-05 13:22:24 -04005297 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005298 * This is a bit simpler than btrfs_truncate since we've already
5299 * reserved our space for our orphan item in the unlink, so we just
5300 * need to reserve some slack space in case we add bytes and update
5301 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005302 */
Yan, Zheng80825102009-11-12 09:35:36 +00005303 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005304 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5305 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005306
Josef Bacik726c35f2011-09-26 15:46:06 -04005307 /*
5308 * Try and steal from the global reserve since we will
5309 * likely not use this space anyway, we want to try as
5310 * hard as possible to get this to work.
5311 */
5312 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005313 steal_from_global++;
5314 else
5315 steal_from_global = 0;
5316 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005317
Josef Bacik3bce8762015-02-24 12:35:51 -08005318 /*
5319 * steal_from_global == 0: we reserved stuff, hooray!
5320 * steal_from_global == 1: we didn't reserve stuff, boo!
5321 * steal_from_global == 2: we've committed, still not a lot of
5322 * room but maybe we'll have room in the global reserve this
5323 * time.
5324 * steal_from_global == 3: abandon all hope!
5325 */
5326 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005327 btrfs_warn(fs_info,
5328 "Could not get space for a delete, will truncate on mount %d",
5329 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005330 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005331 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005332 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005333 }
5334
Miao Xie0e8c36a2012-12-19 06:59:51 +00005335 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005336 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005337 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005338 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005339 goto no_delete;
5340 }
5341
Josef Bacik3bce8762015-02-24 12:35:51 -08005342 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005343 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005344 * sure there is room to do it, if not we need to commit and try
5345 * again.
5346 */
5347 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005348 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005349 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005350 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005351 else
5352 ret = -ENOSPC;
5353 }
5354
5355 /*
5356 * Couldn't steal from the global reserve, we have too much
5357 * pending stuff built up, commit the transaction and try it
5358 * again.
5359 */
5360 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005361 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005362 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005363 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005364 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005365 goto no_delete;
5366 }
5367 continue;
5368 } else {
5369 steal_from_global = 0;
5370 }
5371
Josef Bacik4289a662011-08-05 13:22:24 -04005372 trans->block_rsv = rsv;
5373
Yan, Zhengd68fc572010-05-16 10:49:58 -04005374 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005375 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005376 break;
5377
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005378 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005379 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005380 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005381 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005382 }
5383
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005384 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005385
Josef Bacik4ef31a42013-08-13 14:10:08 -04005386 /*
5387 * Errors here aren't a big deal, it just means we leave orphan items
5388 * in the tree. They will be cleaned up on the next mount.
5389 */
Yan, Zheng80825102009-11-12 09:35:36 +00005390 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005391 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005392 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005393 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005394 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005395 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 trans->block_rsv = &fs_info->trans_block_rsv;
5398 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005399 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005400 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005401
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005402 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005404no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005405 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005406 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005407}
5408
5409/*
5410 * this returns the key found in the dir entry in the location pointer.
5411 * If no dir entries were found, location->objectid is 0.
5412 */
5413static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5414 struct btrfs_key *location)
5415{
5416 const char *name = dentry->d_name.name;
5417 int namelen = dentry->d_name.len;
5418 struct btrfs_dir_item *di;
5419 struct btrfs_path *path;
5420 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005421 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005422
5423 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005424 if (!path)
5425 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005426
David Sterbaf85b7372017-01-20 14:54:07 +01005427 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5428 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005429 if (IS_ERR(di))
5430 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005431
David Sterbac7040052011-04-19 18:00:01 +02005432 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005433 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005434
Chris Mason5f39d392007-10-15 16:14:19 -04005435 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005436out:
Chris Mason39279cc2007-06-12 06:35:45 -04005437 btrfs_free_path(path);
5438 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005439out_err:
5440 location->objectid = 0;
5441 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005442}
5443
5444/*
5445 * when we hit a tree root in a directory, the btrfs part of the inode
5446 * needs to be changed to reflect the root directory of the tree root. This
5447 * is kind of like crossing a mount point.
5448 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005449static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005450 struct inode *dir,
5451 struct dentry *dentry,
5452 struct btrfs_key *location,
5453 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005454{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005455 struct btrfs_path *path;
5456 struct btrfs_root *new_root;
5457 struct btrfs_root_ref *ref;
5458 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005459 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 int ret;
5461 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005462
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 path = btrfs_alloc_path();
5464 if (!path) {
5465 err = -ENOMEM;
5466 goto out;
5467 }
Chris Mason39279cc2007-06-12 06:35:45 -04005468
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005469 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005470 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5471 key.type = BTRFS_ROOT_REF_KEY;
5472 key.offset = location->objectid;
5473
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005474 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005475 if (ret) {
5476 if (ret < 0)
5477 err = ret;
5478 goto out;
5479 }
Chris Mason39279cc2007-06-12 06:35:45 -04005480
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 leaf = path->nodes[0];
5482 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005483 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5485 goto out;
5486
5487 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5488 (unsigned long)(ref + 1),
5489 dentry->d_name.len);
5490 if (ret)
5491 goto out;
5492
David Sterbab3b4aa72011-04-21 01:20:15 +02005493 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005495 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005496 if (IS_ERR(new_root)) {
5497 err = PTR_ERR(new_root);
5498 goto out;
5499 }
5500
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 *sub_root = new_root;
5502 location->objectid = btrfs_root_dirid(&new_root->root_item);
5503 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005504 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 err = 0;
5506out:
5507 btrfs_free_path(path);
5508 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005509}
5510
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005511static void inode_tree_add(struct inode *inode)
5512{
5513 struct btrfs_root *root = BTRFS_I(inode)->root;
5514 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005515 struct rb_node **p;
5516 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005517 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005518 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005519
Al Viro1d3382cb2010-10-23 15:19:20 -04005520 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005522 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005523 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005524 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005525 while (*p) {
5526 parent = *p;
5527 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5528
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005530 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005531 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005532 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005533 else {
5534 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005535 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005536 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005537 RB_CLEAR_NODE(parent);
5538 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005539 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540 }
5541 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005542 rb_link_node(new, parent, p);
5543 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005544 spin_unlock(&root->inode_lock);
5545}
5546
5547static void inode_tree_del(struct inode *inode)
5548{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005549 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005551 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005553 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005557 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005559 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005560
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005561 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005563 spin_lock(&root->inode_lock);
5564 empty = RB_EMPTY_ROOT(&root->inode_tree);
5565 spin_unlock(&root->inode_lock);
5566 if (empty)
5567 btrfs_add_dead_root(root);
5568 }
5569}
5570
Jeff Mahoney143bede2012-03-01 14:56:26 +01005571void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005573 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005574 struct rb_node *node;
5575 struct rb_node *prev;
5576 struct btrfs_inode *entry;
5577 struct inode *inode;
5578 u64 objectid = 0;
5579
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005580 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005581 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005582
5583 spin_lock(&root->inode_lock);
5584again:
5585 node = root->inode_tree.rb_node;
5586 prev = NULL;
5587 while (node) {
5588 prev = node;
5589 entry = rb_entry(node, struct btrfs_inode, rb_node);
5590
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005591 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005592 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005593 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005594 node = node->rb_right;
5595 else
5596 break;
5597 }
5598 if (!node) {
5599 while (prev) {
5600 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005601 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005602 node = prev;
5603 break;
5604 }
5605 prev = rb_next(prev);
5606 }
5607 }
5608 while (node) {
5609 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005610 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005611 inode = igrab(&entry->vfs_inode);
5612 if (inode) {
5613 spin_unlock(&root->inode_lock);
5614 if (atomic_read(&inode->i_count) > 1)
5615 d_prune_aliases(inode);
5616 /*
Al Viro45321ac2010-06-07 13:43:19 -04005617 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 * the inode cache when its usage count
5619 * hits zero.
5620 */
5621 iput(inode);
5622 cond_resched();
5623 spin_lock(&root->inode_lock);
5624 goto again;
5625 }
5626
5627 if (cond_resched_lock(&root->inode_lock))
5628 goto again;
5629
5630 node = rb_next(node);
5631 }
5632 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005633}
5634
Chris Masone02119d2008-09-05 16:13:11 -04005635static int btrfs_init_locked_inode(struct inode *inode, void *p)
5636{
5637 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005638 inode->i_ino = args->location->objectid;
5639 memcpy(&BTRFS_I(inode)->location, args->location,
5640 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005641 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005642 return 0;
5643}
5644
5645static int btrfs_find_actor(struct inode *inode, void *opaque)
5646{
5647 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005648 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005649 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005650}
5651
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005653 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005655{
5656 struct inode *inode;
5657 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005658 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005659
Chris Mason90d3e592014-01-09 17:28:00 -08005660 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005661 args.root = root;
5662
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005663 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005664 btrfs_init_locked_inode,
5665 (void *)&args);
5666 return inode;
5667}
5668
Balaji Rao1a54ef82008-07-21 02:01:04 +05305669/* Get an inode object given its location and corresponding root.
5670 * Returns in *is_new if the inode was read from disk
5671 */
5672struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005673 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305674{
5675 struct inode *inode;
5676
Chris Mason90d3e592014-01-09 17:28:00 -08005677 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305678 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305680
5681 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005682 int ret;
5683
5684 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005685 if (!is_bad_inode(inode)) {
5686 inode_tree_add(inode);
5687 unlock_new_inode(inode);
5688 if (new)
5689 *new = 1;
5690 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005691 unlock_new_inode(inode);
5692 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005693 ASSERT(ret < 0);
5694 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005695 }
5696 }
5697
Balaji Rao1a54ef82008-07-21 02:01:04 +05305698 return inode;
5699}
5700
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005701static struct inode *new_simple_dir(struct super_block *s,
5702 struct btrfs_key *key,
5703 struct btrfs_root *root)
5704{
5705 struct inode *inode = new_inode(s);
5706
5707 if (!inode)
5708 return ERR_PTR(-ENOMEM);
5709
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005710 BTRFS_I(inode)->root = root;
5711 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005712 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005713
5714 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005715 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005716 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717 inode->i_fop = &simple_dir_operations;
5718 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005719 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305720 inode->i_atime = inode->i_mtime;
5721 inode->i_ctime = inode->i_mtime;
5722 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005723
5724 return inode;
5725}
5726
Chris Mason3de45862008-11-17 21:02:50 -05005727struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005728{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005729 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005730 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005732 struct btrfs_root *sub_root = root;
5733 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005734 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005735 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005736
5737 if (dentry->d_name.len > BTRFS_NAME_LEN)
5738 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005739
Jeff Layton39e3c952012-11-28 11:30:53 -05005740 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005741 if (ret < 0)
5742 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005743
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005745 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746
5747 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005748 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005750 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751
5752 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5753
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005754 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005755 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756 &location, &sub_root);
5757 if (ret < 0) {
5758 if (ret != -ENOENT)
5759 inode = ERR_PTR(ret);
5760 else
5761 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5762 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005763 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005764 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005765 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005766
Julia Lawall34d19ba2011-01-24 19:55:19 +00005767 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005768 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005769 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005770 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005771 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005772 if (ret) {
5773 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005774 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005775 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005776 }
5777
Chris Mason3de45862008-11-17 21:02:50 -05005778 return inode;
5779}
5780
Nick Pigginfe15ce42011-01-07 17:49:23 +11005781static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005782{
5783 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005784 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005785
Li Zefan848cce02012-02-21 17:04:28 +08005786 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005787 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005788
Li Zefan848cce02012-02-21 17:04:28 +08005789 if (inode) {
5790 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005791 if (btrfs_root_refs(&root->root_item) == 0)
5792 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005793
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005794 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005795 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005796 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005797 return 0;
5798}
5799
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005800static void btrfs_dentry_release(struct dentry *dentry)
5801{
Daeseok Youn944a4512014-04-14 15:37:02 +09005802 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005803}
5804
Chris Mason3de45862008-11-17 21:02:50 -05005805static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005806 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005807{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005808 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005809
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005810 inode = btrfs_lookup_dentry(dir, dentry);
5811 if (IS_ERR(inode)) {
5812 if (PTR_ERR(inode) == -ENOENT)
5813 inode = NULL;
5814 else
5815 return ERR_CAST(inode);
5816 }
5817
Al Viro41d28bc2014-10-12 22:24:21 -04005818 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005819}
5820
Miao Xie16cdcec2011-04-22 18:12:22 +08005821unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005822 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5823};
5824
Al Viro9cdda8d2013-05-22 16:48:09 -04005825static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005826{
Al Viro9cdda8d2013-05-22 16:48:09 -04005827 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005829 struct btrfs_root *root = BTRFS_I(inode)->root;
5830 struct btrfs_item *item;
5831 struct btrfs_dir_item *di;
5832 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005833 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005834 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005835 struct list_head ins_list;
5836 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005837 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005838 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005839 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005840 unsigned char d_type;
5841 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005842 char tmp_name[32];
5843 char *name_ptr;
5844 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005845 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005846 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005847
Al Viro9cdda8d2013-05-22 16:48:09 -04005848 if (!dir_emit_dots(file, ctx))
5849 return 0;
5850
David Woodhouse49593bf2008-08-17 17:08:36 +01005851 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005852 if (!path)
5853 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005854
David Sterbae4058b52015-11-27 16:31:35 +01005855 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005856
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005857 INIT_LIST_HEAD(&ins_list);
5858 INIT_LIST_HEAD(&del_list);
5859 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005860
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005861 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005862 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005863 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005864
Chris Mason39279cc2007-06-12 06:35:45 -04005865 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5866 if (ret < 0)
5867 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005868
5869 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005870 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005871 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005872 if (slot >= btrfs_header_nritems(leaf)) {
5873 ret = btrfs_next_leaf(root, path);
5874 if (ret < 0)
5875 goto err;
5876 else if (ret > 0)
5877 break;
5878 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005879 }
Chris Mason3de45862008-11-17 21:02:50 -05005880
Ross Kirkdd3cc162013-09-16 15:58:09 +01005881 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005882 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5883
5884 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005885 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005886 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005887 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005888 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005889 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005890 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005891 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005892
Al Viro9cdda8d2013-05-22 16:48:09 -04005893 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005894
Chris Mason39279cc2007-06-12 06:35:45 -04005895 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005896 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005898
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005899 name_len = btrfs_dir_name_len(leaf, di);
5900 if (name_len <= sizeof(tmp_name)) {
5901 name_ptr = tmp_name;
5902 } else {
5903 name_ptr = kmalloc(name_len, GFP_KERNEL);
5904 if (!name_ptr) {
5905 ret = -ENOMEM;
5906 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005907 }
Chris Mason39279cc2007-06-12 06:35:45 -04005908 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005909 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5910 name_len);
5911
5912 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5913 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5914
5915 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5916 d_type);
5917
5918 if (name_ptr != tmp_name)
5919 kfree(name_ptr);
5920
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005921 if (over)
5922 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005923 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005924next:
5925 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005927
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005928 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005929 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005930 goto nopos;
5931
Zach Browndb62efb2013-07-11 16:19:42 -07005932 /*
5933 * Stop new entries from being returned after we return the last
5934 * entry.
5935 *
5936 * New directory entries are assigned a strictly increasing
5937 * offset. This means that new entries created during readdir
5938 * are *guaranteed* to be seen in the future by that readdir.
5939 * This has broken buggy programs which operate on names as
5940 * they're returned by readdir. Until we re-use freed offsets
5941 * we have this hack to stop new entries from being returned
5942 * under the assumption that they'll never reach this huge
5943 * offset.
5944 *
5945 * This is being careful not to overflow 32bit loff_t unless the
5946 * last entry requires it because doing so has broken 32bit apps
5947 * in the past.
5948 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 if (ctx->pos >= INT_MAX)
5950 ctx->pos = LLONG_MAX;
5951 else
5952 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005953nopos:
5954 ret = 0;
5955err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005956 if (put)
5957 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005958 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 return ret;
5960}
5961
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005962int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005963{
5964 struct btrfs_root *root = BTRFS_I(inode)->root;
5965 struct btrfs_trans_handle *trans;
5966 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005967 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005968
Josef Bacik72ac3c02012-05-23 14:13:11 -04005969 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005970 return 0;
5971
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005972 if (btrfs_fs_closing(root->fs_info) &&
5973 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005974 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005975
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005976 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005977 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005978 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005979 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005980 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005981 if (IS_ERR(trans))
5982 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005983 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005984 }
5985 return ret;
5986}
5987
5988/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005989 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005990 * inode changes. But, it is most likely to find the inode in cache.
5991 * FIXME, needs more benchmarking...there are no reasons other than performance
5992 * to keep or drop this code.
5993 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005994static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005995{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005996 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005997 struct btrfs_root *root = BTRFS_I(inode)->root;
5998 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005999 int ret;
6000
Josef Bacik72ac3c02012-05-23 14:13:11 -04006001 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006002 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006003
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006004 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006005 if (IS_ERR(trans))
6006 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006007
6008 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006009 if (ret && ret == -ENOSPC) {
6010 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006011 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006012 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006013 if (IS_ERR(trans))
6014 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006015
Chris Mason94b60442010-05-26 11:02:00 -04006016 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006017 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006018 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006019 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006020 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006021
6022 return ret;
6023}
6024
6025/*
6026 * This is a copy of file_update_time. We need this so we can return error on
6027 * ENOSPC for updating the inode in the case of file write and mmap writes.
6028 */
Josef Bacike41f9412012-03-26 09:46:47 -04006029static int btrfs_update_time(struct inode *inode, struct timespec *now,
6030 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006031{
Alexander Block2bc5565282012-06-15 09:49:33 +02006032 struct btrfs_root *root = BTRFS_I(inode)->root;
6033
6034 if (btrfs_root_readonly(root))
6035 return -EROFS;
6036
Josef Bacike41f9412012-03-26 09:46:47 -04006037 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006038 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006039 if (flags & S_CTIME)
6040 inode->i_ctime = *now;
6041 if (flags & S_MTIME)
6042 inode->i_mtime = *now;
6043 if (flags & S_ATIME)
6044 inode->i_atime = *now;
6045 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006046}
6047
Chris Masond352ac62008-09-29 15:18:18 -04006048/*
6049 * find the highest existing sequence number in a directory
6050 * and then set the in-memory index_cnt variable to reflect
6051 * free sequence numbers
6052 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006053static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006054{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006055 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006056 struct btrfs_key key, found_key;
6057 struct btrfs_path *path;
6058 struct extent_buffer *leaf;
6059 int ret;
6060
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006061 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006062 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006063 key.offset = (u64)-1;
6064
6065 path = btrfs_alloc_path();
6066 if (!path)
6067 return -ENOMEM;
6068
6069 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6070 if (ret < 0)
6071 goto out;
6072 /* FIXME: we should be able to handle this */
6073 if (ret == 0)
6074 goto out;
6075 ret = 0;
6076
6077 /*
6078 * MAGIC NUMBER EXPLANATION:
6079 * since we search a directory based on f_pos we have to start at 2
6080 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6081 * else has to start at 2
6082 */
6083 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006084 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006085 goto out;
6086 }
6087
6088 path->slots[0]--;
6089
6090 leaf = path->nodes[0];
6091 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6092
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006093 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006094 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006095 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006096 goto out;
6097 }
6098
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006099 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006100out:
6101 btrfs_free_path(path);
6102 return ret;
6103}
6104
Chris Masond352ac62008-09-29 15:18:18 -04006105/*
6106 * helper to find a free sequence number in a given directory. This current
6107 * code is very simple, later versions will do smarter things in the btree
6108 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006109int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006110{
6111 int ret = 0;
6112
Nikolay Borisov877574e2017-02-20 13:50:33 +02006113 if (dir->index_cnt == (u64)-1) {
6114 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006115 if (ret) {
6116 ret = btrfs_set_inode_index_count(dir);
6117 if (ret)
6118 return ret;
6119 }
Josef Bacikaec74772008-07-24 12:12:38 -04006120 }
6121
Nikolay Borisov877574e2017-02-20 13:50:33 +02006122 *index = dir->index_cnt;
6123 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006124
6125 return ret;
6126}
6127
Chris Masonb0d5d102014-09-08 13:08:51 -07006128static int btrfs_insert_inode_locked(struct inode *inode)
6129{
6130 struct btrfs_iget_args args;
6131 args.location = &BTRFS_I(inode)->location;
6132 args.root = BTRFS_I(inode)->root;
6133
6134 return insert_inode_locked4(inode,
6135 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6136 btrfs_find_actor, &args);
6137}
6138
Chris Mason39279cc2007-06-12 06:35:45 -04006139static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6140 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006141 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006142 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006143 u64 ref_objectid, u64 objectid,
6144 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006146 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006147 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006148 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006149 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006150 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006151 struct btrfs_inode_ref *ref;
6152 struct btrfs_key key[2];
6153 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006154 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006156 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006157
Chris Mason5f39d392007-10-15 16:14:19 -04006158 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006159 if (!path)
6160 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006161
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006163 if (!inode) {
6164 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006165 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006166 }
Chris Mason39279cc2007-06-12 06:35:45 -04006167
Li Zefan581bb052011-04-20 10:06:11 +08006168 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006169 * O_TMPFILE, set link count to 0, so that after this point,
6170 * we fill in an inode item with the correct link count.
6171 */
6172 if (!name)
6173 set_nlink(inode, 0);
6174
6175 /*
Li Zefan581bb052011-04-20 10:06:11 +08006176 * we have to initialize this early, so we can reclaim the inode
6177 * number if we fail afterwards in this function.
6178 */
6179 inode->i_ino = objectid;
6180
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006181 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006182 trace_btrfs_inode_request(dir);
6183
Nikolay Borisov877574e2017-02-20 13:50:33 +02006184 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006185 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006186 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006187 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006188 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006189 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006190 } else if (dir) {
6191 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006192 }
6193 /*
6194 * index_cnt is ignored for everything but a dir,
6195 * btrfs_get_inode_index_count has an explanation for the magic
6196 * number
6197 */
6198 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006199 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006200 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006201 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006202 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006203
Josef Bacik5dc562c2012-08-17 13:14:17 -04006204 /*
6205 * We could have gotten an inode number from somebody who was fsynced
6206 * and then removed in this same transaction, so let's just set full
6207 * sync since it will be a full sync anyway and this will blow away the
6208 * old info in the log.
6209 */
6210 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6211
Chris Mason9c583092008-01-29 15:15:18 -05006212 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006213 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006214 key[0].offset = 0;
6215
Chris Mason9c583092008-01-29 15:15:18 -05006216 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006217
6218 if (name) {
6219 /*
6220 * Start new inodes with an inode_ref. This is slightly more
6221 * efficient for small numbers of hard links since they will
6222 * be packed into one item. Extended refs will kick in if we
6223 * add more hard links than can fit in the ref item.
6224 */
6225 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006226 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 key[1].offset = ref_objectid;
6228
6229 sizes[1] = name_len + sizeof(*ref);
6230 }
Chris Mason9c583092008-01-29 15:15:18 -05006231
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 location = &BTRFS_I(inode)->location;
6233 location->objectid = objectid;
6234 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006235 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
6237 ret = btrfs_insert_inode_locked(inode);
6238 if (ret < 0)
6239 goto fail;
6240
Chris Masonb9473432009-03-13 11:00:37 -04006241 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006242 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006243 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006244 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006245
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006246 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006247 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306248
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006249 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306250 inode->i_atime = inode->i_mtime;
6251 inode->i_ctime = inode->i_mtime;
6252 BTRFS_I(inode)->i_otime = inode->i_mtime;
6253
Chris Mason5f39d392007-10-15 16:14:19 -04006254 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6255 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006256 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006257 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006258 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006259
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006260 if (name) {
6261 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6262 struct btrfs_inode_ref);
6263 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6264 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6265 ptr = (unsigned long)(ref + 1);
6266 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6267 }
Chris Mason9c583092008-01-29 15:15:18 -05006268
Chris Mason5f39d392007-10-15 16:14:19 -04006269 btrfs_mark_buffer_dirty(path->nodes[0]);
6270 btrfs_free_path(path);
6271
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006272 btrfs_inherit_iflags(inode, dir);
6273
Al Viro569254b2011-07-24 17:08:40 -04006274 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006275 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6279 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006280 }
6281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006282 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006283
6284 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006285 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006286
Alexander Block8ea05e32012-07-25 17:35:53 +02006287 btrfs_update_root_times(trans, root);
6288
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006289 ret = btrfs_inode_inherit_props(trans, inode, dir);
6290 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006292 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006293 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006294
Chris Mason39279cc2007-06-12 06:35:45 -04006295 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296
6297fail_unlock:
6298 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006299fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006300 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006301 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006302 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006303 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006304 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006305}
6306
6307static inline u8 btrfs_inode_type(struct inode *inode)
6308{
6309 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6310}
6311
Chris Masond352ac62008-09-29 15:18:18 -04006312/*
6313 * utility function to add 'inode' into 'parent_inode' with
6314 * a give name and a given sequence number.
6315 * if 'add_backref' is true, also insert a backref from the
6316 * inode to the parent directory.
6317 */
Chris Masone02119d2008-09-05 16:13:11 -04006318int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006319 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006320 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006321{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006323 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006324 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006325 struct btrfs_root *root = parent_inode->root;
6326 u64 ino = btrfs_ino(inode);
6327 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Li Zefan33345d012011-04-20 10:31:50 +08006329 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006330 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006331 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006332 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006333 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006334 key.offset = 0;
6335 }
Chris Mason39279cc2007-06-12 06:35:45 -04006336
Li Zefan33345d012011-04-20 10:31:50 +08006337 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006338 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6339 root->root_key.objectid, parent_ino,
6340 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006341 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006342 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6343 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006344 }
6345
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006346 /* Nothing to clean up yet */
6347 if (ret)
6348 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006349
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006350 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6351 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006352 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006353 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 goto fail_dir_item;
6355 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006356 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006357 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006358 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006359
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006360 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006361 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006362 inode_inc_iversion(&parent_inode->vfs_inode);
6363 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6364 current_time(&parent_inode->vfs_inode);
6365 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006366 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006367 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006368 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006369
6370fail_dir_item:
6371 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6372 u64 local_index;
6373 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6375 root->root_key.objectid, parent_ino,
6376 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006377
6378 } else if (add_backref) {
6379 u64 local_index;
6380 int err;
6381
6382 err = btrfs_del_inode_ref(trans, root, name, name_len,
6383 ino, parent_ino, &local_index);
6384 }
6385 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006386}
6387
6388static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006389 struct btrfs_inode *dir, struct dentry *dentry,
6390 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006391{
Josef Bacika1b075d2010-11-19 20:36:11 +00006392 int err = btrfs_add_link(trans, dir, inode,
6393 dentry->d_name.name, dentry->d_name.len,
6394 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006395 if (err > 0)
6396 err = -EEXIST;
6397 return err;
6398}
6399
Josef Bacik618e21d2007-07-11 10:18:17 -04006400static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006401 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006402{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006404 struct btrfs_trans_handle *trans;
6405 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006406 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006407 int err;
6408 int drop_inode = 0;
6409 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006410 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006411
Josef Bacik9ed74f22009-09-11 16:12:44 -04006412 /*
6413 * 2 for inode item and ref
6414 * 2 for dir items
6415 * 1 for xattr if selinux is on
6416 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006417 trans = btrfs_start_transaction(root, 5);
6418 if (IS_ERR(trans))
6419 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006420
Li Zefan581bb052011-04-20 10:06:11 +08006421 err = btrfs_find_free_ino(root, &objectid);
6422 if (err)
6423 goto out_unlock;
6424
Josef Bacikaec74772008-07-24 12:12:38 -04006425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006426 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6427 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006428 if (IS_ERR(inode)) {
6429 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006430 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006431 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006432
Casey Schauflerad19db72011-12-15 10:09:07 -05006433 /*
6434 * If the active LSM wants to access the inode during
6435 * d_instantiate it needs these. Smack checks to see
6436 * if the filesystem supports xattrs by looking at the
6437 * ops vector.
6438 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006439 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006440 init_special_inode(inode, inode->i_mode, rdev);
6441
6442 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006443 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006444 goto out_unlock_inode;
6445
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006446 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6447 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006448 if (err) {
6449 goto out_unlock_inode;
6450 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006451 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006453 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006454 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006455
Josef Bacik618e21d2007-07-11 10:18:17 -04006456out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006457 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006458 btrfs_balance_delayed_items(fs_info);
6459 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006460 if (drop_inode) {
6461 inode_dec_link_count(inode);
6462 iput(inode);
6463 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006464 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465
6466out_unlock_inode:
6467 drop_inode = 1;
6468 unlock_new_inode(inode);
6469 goto out_unlock;
6470
Josef Bacik618e21d2007-07-11 10:18:17 -04006471}
6472
Chris Mason39279cc2007-06-12 06:35:45 -04006473static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006474 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006475{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006476 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006477 struct btrfs_trans_handle *trans;
6478 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006479 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006480 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006481 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006482 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006483 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006484
Josef Bacik9ed74f22009-09-11 16:12:44 -04006485 /*
6486 * 2 for inode item and ref
6487 * 2 for dir items
6488 * 1 for xattr if selinux is on
6489 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006490 trans = btrfs_start_transaction(root, 5);
6491 if (IS_ERR(trans))
6492 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006493
Li Zefan581bb052011-04-20 10:06:11 +08006494 err = btrfs_find_free_ino(root, &objectid);
6495 if (err)
6496 goto out_unlock;
6497
Josef Bacikaec74772008-07-24 12:12:38 -04006498 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006499 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6500 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006501 if (IS_ERR(inode)) {
6502 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006503 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006504 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006505 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006506 /*
6507 * If the active LSM wants to access the inode during
6508 * d_instantiate it needs these. Smack checks to see
6509 * if the filesystem supports xattrs by looking at the
6510 * ops vector.
6511 */
6512 inode->i_fop = &btrfs_file_operations;
6513 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006514 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006515
6516 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6517 if (err)
6518 goto out_unlock_inode;
6519
6520 err = btrfs_update_inode(trans, root, inode);
6521 if (err)
6522 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006523
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006524 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6525 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006526 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006527 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006528
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006529 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006530 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006531 d_instantiate(dentry, inode);
6532
Chris Mason39279cc2007-06-12 06:35:45 -04006533out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006534 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006535 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006536 inode_dec_link_count(inode);
6537 iput(inode);
6538 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006539 btrfs_balance_delayed_items(fs_info);
6540 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006541 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006542
6543out_unlock_inode:
6544 unlock_new_inode(inode);
6545 goto out_unlock;
6546
Chris Mason39279cc2007-06-12 06:35:45 -04006547}
6548
6549static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6550 struct dentry *dentry)
6551{
Filipe Manana271dba42016-01-05 16:24:05 +00006552 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006553 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006554 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006555 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006556 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006557 int err;
6558 int drop_inode = 0;
6559
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006560 /* do not allow sys_link's with other subvols of the same device */
6561 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006562 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006563
Mark Fashehf1863732012-08-08 11:32:27 -07006564 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006565 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006566
Nikolay Borisov877574e2017-02-20 13:50:33 +02006567 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006568 if (err)
6569 goto fail;
6570
Yan, Zhenga22285a2010-05-16 10:48:46 -04006571 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006572 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006574 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006575 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006576 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006577 if (IS_ERR(trans)) {
6578 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006579 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006580 goto fail;
6581 }
Chris Mason5f39d392007-10-15 16:14:19 -04006582
Miao Xie67de1172013-12-26 13:07:06 +08006583 /* There are several dir indexes for this inode, clear the cache. */
6584 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006585 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006586 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006587 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006588 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006589 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006590
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006591 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6592 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006593
Yan, Zhenga5719522009-09-24 09:17:31 -04006594 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006595 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006596 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006597 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006598 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006599 if (err)
6600 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006601 if (inode->i_nlink == 1) {
6602 /*
6603 * If new hard link count is 1, it's a file created
6604 * with open(2) O_TMPFILE flag.
6605 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006606 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006607 if (err)
6608 goto fail;
6609 }
Al Viro08c422c2011-12-23 07:58:13 -05006610 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006611 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006612 }
Chris Mason39279cc2007-06-12 06:35:45 -04006613
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006615fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006616 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006618 if (drop_inode) {
6619 inode_dec_link_count(inode);
6620 iput(inode);
6621 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006623 return err;
6624}
6625
Al Viro18bb1db2011-07-26 01:41:39 -04006626static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006627{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006628 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006629 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006630 struct btrfs_trans_handle *trans;
6631 struct btrfs_root *root = BTRFS_I(dir)->root;
6632 int err = 0;
6633 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006634 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006635 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006636
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637 /*
6638 * 2 items for inode and ref
6639 * 2 items for dir items
6640 * 1 for xattr if selinux is on
6641 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006642 trans = btrfs_start_transaction(root, 5);
6643 if (IS_ERR(trans))
6644 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006645
Li Zefan581bb052011-04-20 10:06:11 +08006646 err = btrfs_find_free_ino(root, &objectid);
6647 if (err)
6648 goto out_fail;
6649
Josef Bacikaec74772008-07-24 12:12:38 -04006650 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006651 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6652 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006653 if (IS_ERR(inode)) {
6654 err = PTR_ERR(inode);
6655 goto out_fail;
6656 }
Chris Mason5f39d392007-10-15 16:14:19 -04006657
Chris Mason39279cc2007-06-12 06:35:45 -04006658 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006659 /* these must be set before we unlock the inode */
6660 inode->i_op = &btrfs_dir_inode_operations;
6661 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006662
Eric Paris2a7dba32011-02-01 11:05:39 -05006663 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006664 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006665 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006666
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006667 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 err = btrfs_update_inode(trans, root, inode);
6669 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006671
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006672 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6673 dentry->d_name.name,
6674 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006675 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006676 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006677
Chris Mason39279cc2007-06-12 06:35:45 -04006678 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006679 /*
6680 * mkdir is special. We're unlocking after we call d_instantiate
6681 * to avoid a race with nfsd calling d_instantiate.
6682 */
6683 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006685
6686out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006687 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006688 if (drop_on_err) {
6689 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006691 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006692 btrfs_balance_delayed_items(fs_info);
6693 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006694 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006695
6696out_fail_inode:
6697 unlock_new_inode(inode);
6698 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006699}
6700
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006701/* Find next extent map of a given extent map, caller needs to ensure locks */
6702static struct extent_map *next_extent_map(struct extent_map *em)
6703{
6704 struct rb_node *next;
6705
6706 next = rb_next(&em->rb_node);
6707 if (!next)
6708 return NULL;
6709 return container_of(next, struct extent_map, rb_node);
6710}
6711
6712static struct extent_map *prev_extent_map(struct extent_map *em)
6713{
6714 struct rb_node *prev;
6715
6716 prev = rb_prev(&em->rb_node);
6717 if (!prev)
6718 return NULL;
6719 return container_of(prev, struct extent_map, rb_node);
6720}
6721
Chris Masond352ac62008-09-29 15:18:18 -04006722/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006723 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006724 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006725 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006726 */
Chris Mason3b951512008-04-17 11:29:12 -04006727static int merge_extent_mapping(struct extent_map_tree *em_tree,
6728 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006729 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006730 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006731{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006732 struct extent_map *prev;
6733 struct extent_map *next;
6734 u64 start;
6735 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006736 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006737
Chris Masone6dcd2d2008-07-17 12:53:50 -04006738 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006739
6740 if (existing->start > map_start) {
6741 next = existing;
6742 prev = prev_extent_map(next);
6743 } else {
6744 prev = existing;
6745 next = next_extent_map(prev);
6746 }
6747
6748 start = prev ? extent_map_end(prev) : em->start;
6749 start = max_t(u64, start, em->start);
6750 end = next ? next->start : extent_map_end(em);
6751 end = min_t(u64, end, extent_map_end(em));
6752 start_diff = start - em->start;
6753 em->start = start;
6754 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006755 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6756 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006757 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006758 em->block_len -= start_diff;
6759 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006760 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006761}
6762
Chris Masonc8b97812008-10-29 14:49:59 -04006763static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006764 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006765 size_t pg_offset, u64 extent_offset,
6766 struct btrfs_file_extent_item *item)
6767{
6768 int ret;
6769 struct extent_buffer *leaf = path->nodes[0];
6770 char *tmp;
6771 size_t max_size;
6772 unsigned long inline_size;
6773 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006774 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006775
6776 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006777 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006778 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6779 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006780 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006781 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006782 if (!tmp)
6783 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006784 ptr = btrfs_file_extent_inline_start(item);
6785
6786 read_extent_buffer(leaf, tmp, ptr, inline_size);
6787
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006788 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006789 ret = btrfs_decompress(compress_type, tmp, page,
6790 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006791
6792 /*
6793 * decompression code contains a memset to fill in any space between the end
6794 * of the uncompressed data and the end of max_size in case the decompressed
6795 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6796 * the end of an inline extent and the beginning of the next block, so we
6797 * cover that region here.
6798 */
6799
6800 if (max_size + pg_offset < PAGE_SIZE) {
6801 char *map = kmap(page);
6802 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6803 kunmap(page);
6804 }
Chris Masonc8b97812008-10-29 14:49:59 -04006805 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006806 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006807}
6808
Chris Masond352ac62008-09-29 15:18:18 -04006809/*
6810 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006811 * the ugly parts come from merging extents from the disk with the in-ram
6812 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006813 * where the in-ram extents might be locked pending data=ordered completion.
6814 *
6815 * This also copies inline extents directly into the page.
6816 */
Chris Masond3977122009-01-05 21:25:51 -05006817
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006818struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6819 struct page *page,
6820 size_t pg_offset, u64 start, u64 len,
6821 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006822{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006823 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006824 int ret;
6825 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006826 u64 extent_start = 0;
6827 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006828 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006829 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006830 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006831 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006832 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006833 struct extent_buffer *leaf;
6834 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006835 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006836 struct extent_map_tree *em_tree = &inode->extent_tree;
6837 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006838 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006839 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006840
Chris Masona52d9a82007-08-27 16:49:44 -04006841again:
Chris Mason890871b2009-09-02 16:24:52 -04006842 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006843 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006844 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006845 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006846 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006847
Chris Masona52d9a82007-08-27 16:49:44 -04006848 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006849 if (em->start > start || em->start + em->len <= start)
6850 free_extent_map(em);
6851 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006852 free_extent_map(em);
6853 else
6854 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006855 }
David Sterba172ddd62011-04-21 00:48:27 +02006856 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006857 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006858 err = -ENOMEM;
6859 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006860 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006861 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006862 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006863 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006864 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006865 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006866
6867 if (!path) {
6868 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006869 if (!path) {
6870 err = -ENOMEM;
6871 goto out;
6872 }
6873 /*
6874 * Chances are we'll be called again, so go ahead and do
6875 * readahead
6876 */
David Sterbae4058b52015-11-27 16:31:35 +01006877 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006878 }
6879
Chris Mason179e29e2007-11-01 11:28:41 -04006880 ret = btrfs_lookup_file_extent(trans, root, path,
6881 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006882 if (ret < 0) {
6883 err = ret;
6884 goto out;
6885 }
6886
6887 if (ret != 0) {
6888 if (path->slots[0] == 0)
6889 goto not_found;
6890 path->slots[0]--;
6891 }
6892
Chris Mason5f39d392007-10-15 16:14:19 -04006893 leaf = path->nodes[0];
6894 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006895 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006896 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006897 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006898 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006899 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006900 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006901 /*
6902 * If we backup past the first extent we want to move forward
6903 * and see if there is an extent in front of us, otherwise we'll
6904 * say there is a hole for our whole search range which can
6905 * cause problems.
6906 */
6907 extent_end = start;
6908 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
6910
Chris Mason5f39d392007-10-15 16:14:19 -04006911 found_type = btrfs_file_extent_type(leaf, item);
6912 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006913 if (found_type == BTRFS_FILE_EXTENT_REG ||
6914 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006915 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006916 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006917 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6918 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006919 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006920 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006921 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006922 }
Josef Bacik25a50342013-10-14 12:08:38 -04006923next:
Yan Zheng9036c102008-10-30 14:19:41 -04006924 if (start >= extent_end) {
6925 path->slots[0]++;
6926 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6927 ret = btrfs_next_leaf(root, path);
6928 if (ret < 0) {
6929 err = ret;
6930 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006931 }
Yan Zheng9036c102008-10-30 14:19:41 -04006932 if (ret > 0)
6933 goto not_found;
6934 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006935 }
Yan Zheng9036c102008-10-30 14:19:41 -04006936 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6937 if (found_key.objectid != objectid ||
6938 found_key.type != BTRFS_EXTENT_DATA_KEY)
6939 goto not_found;
6940 if (start + len <= found_key.offset)
6941 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006942 if (start > found_key.offset)
6943 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006944 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006945 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006946 em->len = found_key.offset - start;
6947 goto not_found_em;
6948 }
6949
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006950 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006951 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006952
Yan Zhengd899e052008-10-30 14:25:28 -04006953 if (found_type == BTRFS_FILE_EXTENT_REG ||
6954 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006955 goto insert;
6956 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006957 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006958 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006959 size_t size;
6960 size_t extent_offset;
6961 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006962
Filipe Manana7ffbb592014-06-09 03:48:05 +01006963 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006964 goto out;
Yan689f9342007-10-29 11:41:07 -04006965
Chris Mason514ac8a2014-01-03 21:07:00 -08006966 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006967 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006968 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6969 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006970 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006971 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006972 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006973 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006974 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006975 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006976 if (btrfs_file_extent_compression(leaf, item) !=
6977 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006978 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006979 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006980 if (ret) {
6981 err = ret;
6982 goto out;
6983 }
Chris Masonc8b97812008-10-29 14:49:59 -04006984 } else {
6985 map = kmap(page);
6986 read_extent_buffer(leaf, map + pg_offset, ptr,
6987 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006988 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006989 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006990 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006991 copy_size);
6992 }
Chris Masonc8b97812008-10-29 14:49:59 -04006993 kunmap(page);
6994 }
Chris Mason179e29e2007-11-01 11:28:41 -04006995 flush_dcache_page(page);
6996 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006997 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006998 if (!trans) {
6999 kunmap(page);
7000 free_extent_map(em);
7001 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007002
David Sterbab3b4aa72011-04-21 01:20:15 +02007003 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007004 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007005
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007006 if (IS_ERR(trans))
7007 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007008 goto again;
7009 }
Chris Masonc8b97812008-10-29 14:49:59 -04007010 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007011 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007012 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007013 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007014 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007015 }
Chris Masond1310b22008-01-24 16:13:08 -05007016 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007017 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007018 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007019 }
7020not_found:
7021 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007022 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007023 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007024not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007025 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007026 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007027insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007028 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007029 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007030 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007031 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7032 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007033 err = -EIO;
7034 goto out;
7035 }
Chris Masond1310b22008-01-24 16:13:08 -05007036
7037 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007038 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007039 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007040 /* it is possible that someone inserted the extent into the tree
7041 * while we had the lock dropped. It is also possible that
7042 * an overlapping map exists in the tree
7043 */
Chris Masona52d9a82007-08-27 16:49:44 -04007044 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007045 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007046
7047 ret = 0;
7048
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007049 existing = search_extent_mapping(em_tree, start, len);
7050 /*
7051 * existing will always be non-NULL, since there must be
7052 * extent causing the -EEXIST.
7053 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007054 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007055 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007056 em->block_start == existing->block_start) {
7057 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007058 * The existing extent map already encompasses the
7059 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007060 */
7061 free_extent_map(em);
7062 em = existing;
7063 err = 0;
7064
7065 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007066 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007067 /*
7068 * The existing extent map is the one nearest to
7069 * the [start, start + len) range which overlaps
7070 */
7071 err = merge_extent_mapping(em_tree, existing,
7072 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007073 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007074 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007075 free_extent_map(em);
7076 em = NULL;
7077 }
7078 } else {
7079 free_extent_map(em);
7080 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007081 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007082 }
Chris Masona52d9a82007-08-27 16:49:44 -04007083 }
Chris Mason890871b2009-09-02 16:24:52 -04007084 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007085out:
liubo1abe9b82011-03-24 11:18:59 +00007086
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007087 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007088
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007089 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007090 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007091 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007092 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007093 err = ret;
7094 }
Chris Masona52d9a82007-08-27 16:49:44 -04007095 if (err) {
7096 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007097 return ERR_PTR(err);
7098 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007099 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007100 return em;
7101}
7102
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007103struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7104 struct page *page,
7105 size_t pg_offset, u64 start, u64 len,
7106 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007107{
7108 struct extent_map *em;
7109 struct extent_map *hole_em = NULL;
7110 u64 range_start = start;
7111 u64 end;
7112 u64 found;
7113 u64 found_end;
7114 int err = 0;
7115
7116 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7117 if (IS_ERR(em))
7118 return em;
7119 if (em) {
7120 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007121 * if our em maps to
7122 * - a hole or
7123 * - a pre-alloc extent,
7124 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007125 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007126 if (em->block_start != EXTENT_MAP_HOLE &&
7127 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007128 return em;
7129 else
7130 hole_em = em;
7131 }
7132
7133 /* check to see if we've wrapped (len == -1 or similar) */
7134 end = start + len;
7135 if (end < start)
7136 end = (u64)-1;
7137 else
7138 end -= 1;
7139
7140 em = NULL;
7141
7142 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007143 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007144 end, len, EXTENT_DELALLOC, 1);
7145 found_end = range_start + found;
7146 if (found_end < range_start)
7147 found_end = (u64)-1;
7148
7149 /*
7150 * we didn't find anything useful, return
7151 * the original results from get_extent()
7152 */
7153 if (range_start > end || found_end <= start) {
7154 em = hole_em;
7155 hole_em = NULL;
7156 goto out;
7157 }
7158
7159 /* adjust the range_start to make sure it doesn't
7160 * go backwards from the start they passed in
7161 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307162 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007163 found = found_end - range_start;
7164
7165 if (found > 0) {
7166 u64 hole_start = start;
7167 u64 hole_len = len;
7168
David Sterba172ddd62011-04-21 00:48:27 +02007169 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007170 if (!em) {
7171 err = -ENOMEM;
7172 goto out;
7173 }
7174 /*
7175 * when btrfs_get_extent can't find anything it
7176 * returns one huge hole
7177 *
7178 * make sure what it found really fits our range, and
7179 * adjust to make sure it is based on the start from
7180 * the caller
7181 */
7182 if (hole_em) {
7183 u64 calc_end = extent_map_end(hole_em);
7184
7185 if (calc_end <= start || (hole_em->start > end)) {
7186 free_extent_map(hole_em);
7187 hole_em = NULL;
7188 } else {
7189 hole_start = max(hole_em->start, start);
7190 hole_len = calc_end - hole_start;
7191 }
7192 }
7193 em->bdev = NULL;
7194 if (hole_em && range_start > hole_start) {
7195 /* our hole starts before our delalloc, so we
7196 * have to return just the parts of the hole
7197 * that go until the delalloc starts
7198 */
7199 em->len = min(hole_len,
7200 range_start - hole_start);
7201 em->start = hole_start;
7202 em->orig_start = hole_start;
7203 /*
7204 * don't adjust block start at all,
7205 * it is fixed at EXTENT_MAP_HOLE
7206 */
7207 em->block_start = hole_em->block_start;
7208 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007209 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7210 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007211 } else {
7212 em->start = range_start;
7213 em->len = found;
7214 em->orig_start = range_start;
7215 em->block_start = EXTENT_MAP_DELALLOC;
7216 em->block_len = found;
7217 }
7218 } else if (hole_em) {
7219 return hole_em;
7220 }
7221out:
7222
7223 free_extent_map(hole_em);
7224 if (err) {
7225 free_extent_map(em);
7226 return ERR_PTR(err);
7227 }
7228 return em;
7229}
7230
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007231static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7232 const u64 start,
7233 const u64 len,
7234 const u64 orig_start,
7235 const u64 block_start,
7236 const u64 block_len,
7237 const u64 orig_block_len,
7238 const u64 ram_bytes,
7239 const int type)
7240{
7241 struct extent_map *em = NULL;
7242 int ret;
7243
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007244 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007245 em = create_io_em(inode, start, len, orig_start,
7246 block_start, block_len, orig_block_len,
7247 ram_bytes,
7248 BTRFS_COMPRESS_NONE, /* compress_type */
7249 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007250 if (IS_ERR(em))
7251 goto out;
7252 }
7253 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7254 len, block_len, type);
7255 if (ret) {
7256 if (em) {
7257 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007258 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007259 start + len - 1, 0);
7260 }
7261 em = ERR_PTR(ret);
7262 }
7263 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007264
7265 return em;
7266}
7267
Josef Bacik4b46fce2010-05-23 11:00:55 -04007268static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7269 u64 start, u64 len)
7270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007273 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 struct btrfs_key ins;
7275 u64 alloc_hint;
7276 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007279 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007280 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007281 if (ret)
7282 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007284 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7285 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007286 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007287 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007288 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007289 btrfs_free_reserved_extent(fs_info, ins.objectid,
7290 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007291
Josef Bacik4b46fce2010-05-23 11:00:55 -04007292 return em;
7293}
7294
Chris Mason46bfbb52010-05-26 11:04:10 -04007295/*
7296 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7297 * block must be cow'd
7298 */
Josef Bacik00361582013-08-14 14:02:47 -04007299noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007300 u64 *orig_start, u64 *orig_block_len,
7301 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007302{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007304 struct btrfs_path *path;
7305 int ret;
7306 struct extent_buffer *leaf;
7307 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007308 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007309 struct btrfs_file_extent_item *fi;
7310 struct btrfs_key key;
7311 u64 disk_bytenr;
7312 u64 backref_offset;
7313 u64 extent_end;
7314 u64 num_bytes;
7315 int slot;
7316 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007317 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007318
Chris Mason46bfbb52010-05-26 11:04:10 -04007319 path = btrfs_alloc_path();
7320 if (!path)
7321 return -ENOMEM;
7322
David Sterbaf85b7372017-01-20 14:54:07 +01007323 ret = btrfs_lookup_file_extent(NULL, root, path,
7324 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007325 if (ret < 0)
7326 goto out;
7327
7328 slot = path->slots[0];
7329 if (ret == 1) {
7330 if (slot == 0) {
7331 /* can't find the item, must cow */
7332 ret = 0;
7333 goto out;
7334 }
7335 slot--;
7336 }
7337 ret = 0;
7338 leaf = path->nodes[0];
7339 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007340 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 key.type != BTRFS_EXTENT_DATA_KEY) {
7342 /* not our file or wrong item type, must cow */
7343 goto out;
7344 }
7345
7346 if (key.offset > offset) {
7347 /* Wrong offset, must cow */
7348 goto out;
7349 }
7350
7351 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7352 found_type = btrfs_file_extent_type(leaf, fi);
7353 if (found_type != BTRFS_FILE_EXTENT_REG &&
7354 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7355 /* not a regular extent, must cow */
7356 goto out;
7357 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007358
7359 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7360 goto out;
7361
Miao Xiee77751a2013-12-27 21:11:50 +08007362 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7363 if (extent_end <= offset)
7364 goto out;
7365
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367 if (disk_bytenr == 0)
7368 goto out;
7369
7370 if (btrfs_file_extent_compression(leaf, fi) ||
7371 btrfs_file_extent_encryption(leaf, fi) ||
7372 btrfs_file_extent_other_encoding(leaf, fi))
7373 goto out;
7374
Chris Mason46bfbb52010-05-26 11:04:10 -04007375 backref_offset = btrfs_file_extent_offset(leaf, fi);
7376
Josef Bacik7ee9e442013-06-21 16:37:03 -04007377 if (orig_start) {
7378 *orig_start = key.offset - backref_offset;
7379 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7380 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7381 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007382
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007384 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007385
7386 num_bytes = min(offset + *len, extent_end) - offset;
7387 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7388 u64 range_end;
7389
Jeff Mahoneyda170662016-06-15 09:22:56 -04007390 range_end = round_up(offset + num_bytes,
7391 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007392 ret = test_range_bit(io_tree, offset, range_end,
7393 EXTENT_DELALLOC, 0, NULL);
7394 if (ret) {
7395 ret = -EAGAIN;
7396 goto out;
7397 }
7398 }
7399
Josef Bacik1bda19e2013-10-18 12:10:36 -04007400 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007401
7402 /*
7403 * look for other files referencing this extent, if we
7404 * find any we must cow
7405 */
Josef Bacik00361582013-08-14 14:02:47 -04007406
Liu Boe4c3b2d2017-01-30 12:25:28 -08007407 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007408 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007409 if (ret) {
7410 ret = 0;
7411 goto out;
7412 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007413
7414 /*
7415 * adjust disk_bytenr and num_bytes to cover just the bytes
7416 * in this extent we are about to write. If there
7417 * are any csums in that range we have to cow in order
7418 * to keep the csums correct
7419 */
7420 disk_bytenr += backref_offset;
7421 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007422 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7423 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007424 /*
7425 * all of the above have passed, it is safe to overwrite this extent
7426 * without cow
7427 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007428 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007429 ret = 1;
7430out:
7431 btrfs_free_path(path);
7432 return ret;
7433}
7434
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007435bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7436{
7437 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7438 int found = false;
7439 void **pagep = NULL;
7440 struct page *page = NULL;
7441 int start_idx;
7442 int end_idx;
7443
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007444 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007445
7446 /*
7447 * end is the last byte in the last page. end == start is legal
7448 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007449 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007450
7451 rcu_read_lock();
7452
7453 /* Most of the code in this while loop is lifted from
7454 * find_get_page. It's been modified to begin searching from a
7455 * page and return just the first page found in that range. If the
7456 * found idx is less than or equal to the end idx then we know that
7457 * a page exists. If no pages are found or if those pages are
7458 * outside of the range then we're fine (yay!) */
7459 while (page == NULL &&
7460 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7461 page = radix_tree_deref_slot(pagep);
7462 if (unlikely(!page))
7463 break;
7464
7465 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007466 if (radix_tree_deref_retry(page)) {
7467 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007468 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007469 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470 /*
7471 * Otherwise, shmem/tmpfs must be storing a swap entry
7472 * here as an exceptional entry: so return it without
7473 * attempting to raise page count.
7474 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007475 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007476 break; /* TODO: Is this relevant for this use case? */
7477 }
7478
Filipe Manana91405152014-06-05 13:22:24 +01007479 if (!page_cache_get_speculative(page)) {
7480 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007481 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007482 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007483
7484 /*
7485 * Has the page moved?
7486 * This is part of the lockless pagecache protocol. See
7487 * include/linux/pagemap.h for details.
7488 */
7489 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007490 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007491 page = NULL;
7492 }
7493 }
7494
7495 if (page) {
7496 if (page->index <= end_idx)
7497 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007498 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007499 }
7500
7501 rcu_read_unlock();
7502 return found;
7503}
7504
Josef Bacikeb838e72012-07-31 16:28:48 -04007505static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7506 struct extent_state **cached_state, int writing)
7507{
7508 struct btrfs_ordered_extent *ordered;
7509 int ret = 0;
7510
7511 while (1) {
7512 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007513 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007514 /*
7515 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007516 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007517 * extents in this range.
7518 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007519 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007520 lockend - lockstart + 1);
7521
7522 /*
7523 * We need to make sure there are no buffered pages in this
7524 * range either, we could have raced between the invalidate in
7525 * generic_file_direct_write and locking the extent. The
7526 * invalidate needs to happen so that reads after a write do not
7527 * get stale data.
7528 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007529 if (!ordered &&
7530 (!writing ||
7531 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007532 break;
7533
7534 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7535 cached_state, GFP_NOFS);
7536
7537 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007538 /*
7539 * If we are doing a DIO read and the ordered extent we
7540 * found is for a buffered write, we can not wait for it
7541 * to complete and retry, because if we do so we can
7542 * deadlock with concurrent buffered writes on page
7543 * locks. This happens only if our DIO read covers more
7544 * than one extent map, if at this point has already
7545 * created an ordered extent for a previous extent map
7546 * and locked its range in the inode's io tree, and a
7547 * concurrent write against that previous extent map's
7548 * range and this range started (we unlock the ranges
7549 * in the io tree only when the bios complete and
7550 * buffered writes always lock pages before attempting
7551 * to lock range in the io tree).
7552 */
7553 if (writing ||
7554 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7555 btrfs_start_ordered_extent(inode, ordered, 1);
7556 else
7557 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007558 btrfs_put_ordered_extent(ordered);
7559 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007560 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007561 * We could trigger writeback for this range (and wait
7562 * for it to complete) and then invalidate the pages for
7563 * this range (through invalidate_inode_pages2_range()),
7564 * but that can lead us to a deadlock with a concurrent
7565 * call to readpages() (a buffered read or a defrag call
7566 * triggered a readahead) on a page lock due to an
7567 * ordered dio extent we created before but did not have
7568 * yet a corresponding bio submitted (whence it can not
7569 * complete), which makes readpages() wait for that
7570 * ordered extent to complete while holding a lock on
7571 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007572 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007573 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007574 }
7575
Filipe Mananaade77022016-02-18 14:28:55 +00007576 if (ret)
7577 break;
7578
Josef Bacikeb838e72012-07-31 16:28:48 -04007579 cond_resched();
7580 }
7581
7582 return ret;
7583}
7584
Liu Bo6f9994d2017-01-31 07:50:22 -08007585/* The callers of this must take lock_extent() */
7586static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7587 u64 orig_start, u64 block_start,
7588 u64 block_len, u64 orig_block_len,
7589 u64 ram_bytes, int compress_type,
7590 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007591{
7592 struct extent_map_tree *em_tree;
7593 struct extent_map *em;
7594 struct btrfs_root *root = BTRFS_I(inode)->root;
7595 int ret;
7596
Liu Bo6f9994d2017-01-31 07:50:22 -08007597 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7598 type == BTRFS_ORDERED_COMPRESSED ||
7599 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007600 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007601
Josef Bacik69ffb542012-09-11 15:40:07 -04007602 em_tree = &BTRFS_I(inode)->extent_tree;
7603 em = alloc_extent_map();
7604 if (!em)
7605 return ERR_PTR(-ENOMEM);
7606
7607 em->start = start;
7608 em->orig_start = orig_start;
7609 em->len = len;
7610 em->block_len = block_len;
7611 em->block_start = block_start;
7612 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007613 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007614 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007615 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007616 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007617 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007618 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007619 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007620 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7621 em->compress_type = compress_type;
7622 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007623
7624 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007625 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007626 em->start + em->len - 1, 0);
7627 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007628 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007629 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007630 /*
7631 * The caller has taken lock_extent(), who could race with us
7632 * to add em?
7633 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007634 } while (ret == -EEXIST);
7635
7636 if (ret) {
7637 free_extent_map(em);
7638 return ERR_PTR(ret);
7639 }
7640
Liu Bo6f9994d2017-01-31 07:50:22 -08007641 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007642 return em;
7643}
7644
Filipe Manana9c9464c2015-11-04 09:52:04 +00007645static void adjust_dio_outstanding_extents(struct inode *inode,
7646 struct btrfs_dio_data *dio_data,
7647 const u64 len)
7648{
David Sterba823bb202017-01-04 11:09:51 +01007649 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007650
Filipe Manana9c9464c2015-11-04 09:52:04 +00007651 /*
7652 * If we have an outstanding_extents count still set then we're
7653 * within our reservation, otherwise we need to adjust our inode
7654 * counter appropriately.
7655 */
Liu Boc2931662016-12-22 17:13:54 -08007656 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007657 dio_data->outstanding_extents -= num_extents;
7658 } else {
Liu Boc2931662016-12-22 17:13:54 -08007659 /*
7660 * If dio write length has been split due to no large enough
7661 * contiguous space, we need to compensate our inode counter
7662 * appropriately.
7663 */
7664 u64 num_needed = num_extents - dio_data->outstanding_extents;
7665
Filipe Manana9c9464c2015-11-04 09:52:04 +00007666 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007667 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007668 spin_unlock(&BTRFS_I(inode)->lock);
7669 }
7670}
7671
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7673 struct buffer_head *bh_result, int create)
7674{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007675 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307678 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007679 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007683 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007684
Miao Xie172a5042013-02-21 02:48:22 -07007685 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007686 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007687 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007688 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007689
Josef Bacikc3298612012-08-03 16:49:19 -04007690 lockstart = start;
7691 lockend = start + len - 1;
7692
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007693 if (current->journal_info) {
7694 /*
7695 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007696 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007697 * confused.
7698 */
chandan50745b02015-08-28 21:10:13 +05307699 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007700 current->journal_info = NULL;
7701 }
7702
Josef Bacikeb838e72012-07-31 16:28:48 -04007703 /*
7704 * If this errors out it's because we couldn't invalidate pagecache for
7705 * this range and we need to fallback to buffered.
7706 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007707 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7708 create)) {
7709 ret = -ENOTBLK;
7710 goto err;
7711 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007712
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007713 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007714 if (IS_ERR(em)) {
7715 ret = PTR_ERR(em);
7716 goto unlock_err;
7717 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718
7719 /*
7720 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7721 * io. INLINE is special, and we could probably kludge it in here, but
7722 * it's still buffered so for safety lets just fall back to the generic
7723 * buffered path.
7724 *
7725 * For COMPRESSED we _have_ to read the entire extent in so we can
7726 * decompress it, so there will be buffering required no matter what we
7727 * do, so go ahead and fallback to buffered.
7728 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007729 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 * to buffered IO. Don't blame me, this is the price we pay for using
7731 * the generic code.
7732 */
7733 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7734 em->block_start == EXTENT_MAP_INLINE) {
7735 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007736 ret = -ENOTBLK;
7737 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738 }
7739
7740 /* Just a good old fashioned hole, return */
7741 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7742 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7743 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007744 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 }
7746
7747 /*
7748 * We don't allocate a new extent in the following cases
7749 *
7750 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7751 * existing extent.
7752 * 2) The extent is marked as PREALLOC. We're good to go here and can
7753 * just use the extent.
7754 *
7755 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007756 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 len = min(len, em->len - (start - em->start));
7758 lockstart = start + len;
7759 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007760 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007761
7762 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7763 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7764 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007766 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767
7768 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7769 type = BTRFS_ORDERED_PREALLOC;
7770 else
7771 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007772 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007774
Josef Bacik00361582013-08-14 14:02:47 -04007775 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007776 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007777 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007778 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007779
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007780 em2 = btrfs_create_dio_extent(inode, start, len,
7781 orig_start, block_start,
7782 len, orig_block_len,
7783 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007784 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007785 if (type == BTRFS_ORDERED_PREALLOC) {
7786 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007787 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007788 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007789 if (em2 && IS_ERR(em2)) {
7790 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007791 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007792 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007793 /*
7794 * For inode marked NODATACOW or extent marked PREALLOC,
7795 * use the existing or preallocated extent, so does not
7796 * need to adjust btrfs_space_info's bytes_may_use.
7797 */
7798 btrfs_free_reserved_data_space_noquota(inode,
7799 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007800 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802 }
Josef Bacik00361582013-08-14 14:02:47 -04007803
Chris Mason46bfbb52010-05-26 11:04:10 -04007804 /*
7805 * this will cow the extent, reset the len in case we changed
7806 * it above
7807 */
7808 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007809 free_extent_map(em);
7810 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007811 if (IS_ERR(em)) {
7812 ret = PTR_ERR(em);
7813 goto unlock_err;
7814 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007815 len = min(len, em->len - (start - em->start));
7816unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7818 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007819 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007820 bh_result->b_bdev = em->bdev;
7821 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007822 if (create) {
7823 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7824 set_buffer_new(bh_result);
7825
7826 /*
7827 * Need to update the i_size under the extent lock so buffered
7828 * readers will get the updated i_size when we unlock.
7829 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007830 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007831 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007832
Filipe Manana9c9464c2015-11-04 09:52:04 +00007833 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307834 WARN_ON(dio_data->reserve < len);
7835 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007836 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307837 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007838 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839
Josef Bacikeb838e72012-07-31 16:28:48 -04007840 /*
7841 * In the case of write we need to clear and unlock the entire range,
7842 * in the case of read we need to unlock only the end area that we
7843 * aren't using if there is any left over space.
7844 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007845 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007846 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7847 lockend, unlock_bits, 1, 0,
7848 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007849 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007850 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007851 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007852
Josef Bacik4b46fce2010-05-23 11:00:55 -04007853 free_extent_map(em);
7854
7855 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007856
7857unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007858 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7859 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007860err:
chandan50745b02015-08-28 21:10:13 +05307861 if (dio_data)
7862 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007863 /*
7864 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7865 * write less data then expected, so that we don't underflow our inode's
7866 * outstanding extents counter.
7867 */
7868 if (create && dio_data)
7869 adjust_dio_outstanding_extents(inode, dio_data, len);
7870
Josef Bacikeb838e72012-07-31 16:28:48 -04007871 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007872}
7873
Miao Xie8b110e32014-09-12 18:44:03 +08007874static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007875 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007876{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007878 int ret;
7879
Mike Christie37226b22016-06-05 14:31:52 -05007880 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007881
7882 bio_get(bio);
7883
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007884 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007885 if (ret)
7886 goto err;
7887
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007888 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007889err:
7890 bio_put(bio);
7891 return ret;
7892}
7893
7894static int btrfs_check_dio_repairable(struct inode *inode,
7895 struct bio *failed_bio,
7896 struct io_failure_record *failrec,
7897 int failed_mirror)
7898{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007899 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007900 int num_copies;
7901
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007902 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007903 if (num_copies == 1) {
7904 /*
7905 * we only have a single copy of the data, so don't bother with
7906 * all the retry and error correction code that follows. no
7907 * matter what the error is, it is very likely to persist.
7908 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007909 btrfs_debug(fs_info,
7910 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7911 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007912 return 0;
7913 }
7914
7915 failrec->failed_mirror = failed_mirror;
7916 failrec->this_mirror++;
7917 if (failrec->this_mirror == failed_mirror)
7918 failrec->this_mirror++;
7919
7920 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007921 btrfs_debug(fs_info,
7922 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7923 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007924 return 0;
7925 }
7926
7927 return 1;
7928}
7929
7930static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 struct page *page, unsigned int pgoff,
7932 u64 start, u64 end, int failed_mirror,
7933 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007934{
7935 struct io_failure_record *failrec;
7936 struct bio *bio;
7937 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007938 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 int ret;
7940
Mike Christie37226b22016-06-05 14:31:52 -05007941 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007942
7943 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7944 if (ret)
7945 return ret;
7946
7947 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7948 failed_mirror);
7949 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007950 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 return -EIO;
7952 }
7953
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307954 if ((failed_bio->bi_vcnt > 1)
7955 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007956 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007957 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007958
7959 isector = start - btrfs_io_bio(failed_bio)->logical;
7960 isector >>= inode->i_sb->s_blocksize_bits;
7961 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007963 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007964 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007965 return -EIO;
7966 }
Mike Christie37226b22016-06-05 14:31:52 -05007967 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007968
7969 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7970 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7971 read_mode, failrec->this_mirror, failrec->in_validation);
7972
Mike Christie81a75f672016-06-05 14:31:54 -05007973 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007974 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007975 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007976 bio_put(bio);
7977 }
7978
7979 return ret;
7980}
7981
7982struct btrfs_retry_complete {
7983 struct completion done;
7984 struct inode *inode;
7985 u64 start;
7986 int uptodate;
7987};
7988
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007989static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007990{
7991 struct btrfs_retry_complete *done = bio->bi_private;
7992 struct bio_vec *bvec;
7993 int i;
7994
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007995 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007996 goto end;
7997
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07007999 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000
Miao Xie8b110e32014-09-12 18:44:03 +08008001 done->uptodate = 1;
8002 bio_for_each_segment_all(bvec, bio, i)
Liu Bo2e949b02017-04-05 14:04:19 -07008003 clean_io_failure(BTRFS_I(done->inode), done->start,
8004 bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008005end:
8006 complete(&done->done);
8007 bio_put(bio);
8008}
8009
8010static int __btrfs_correct_data_nocsum(struct inode *inode,
8011 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008012{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008014 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008015 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008016 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 unsigned int pgoff;
8018 u32 sectorsize;
8019 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008020 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008021 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008022
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008024 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025
Miao Xiec1dc0892014-09-12 18:43:56 +08008026 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008027 done.inode = inode;
8028
8029 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8031 pgoff = bvec->bv_offset;
8032
8033next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008034 done.uptodate = 0;
8035 done.start = start;
8036 init_completion(&done.done);
8037
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8039 pgoff, start, start + sectorsize - 1,
8040 io_bio->mirror_num,
8041 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008042 if (ret)
8043 return ret;
8044
8045 wait_for_completion(&done.done);
8046
8047 if (!done.uptodate) {
8048 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008050 }
8051
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052 start += sectorsize;
8053
Liu Bo97bf5a52017-04-07 13:11:10 -07008054 nr_sectors--;
8055 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008057 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 goto next_block_or_try_again;
8059 }
Miao Xie8b110e32014-09-12 18:44:03 +08008060 }
8061
8062 return 0;
8063}
8064
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008065static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008066{
8067 struct btrfs_retry_complete *done = bio->bi_private;
8068 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8069 struct bio_vec *bvec;
8070 int uptodate;
8071 int ret;
8072 int i;
8073
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008074 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008075 goto end;
8076
8077 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008080 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308081
Miao Xie8b110e32014-09-12 18:44:03 +08008082 bio_for_each_segment_all(bvec, bio, i) {
8083 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084 bvec->bv_page, bvec->bv_offset,
8085 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008086 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02008087 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008089 else
8090 uptodate = 0;
8091 }
8092
8093 done->uptodate = uptodate;
8094end:
8095 complete(&done->done);
8096 bio_put(bio);
8097}
8098
8099static int __btrfs_subio_endio_read(struct inode *inode,
8100 struct btrfs_io_bio *io_bio, int err)
8101{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008103 struct bio_vec *bvec;
8104 struct btrfs_retry_complete done;
8105 u64 start;
8106 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107 u32 sectorsize;
8108 int nr_sectors;
8109 unsigned int pgoff;
8110 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008111 int i;
8112 int ret;
8113
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008115 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116
Miao Xie8b110e32014-09-12 18:44:03 +08008117 err = 0;
8118 start = io_bio->logical;
8119 done.inode = inode;
8120
Miao Xiec1dc0892014-09-12 18:43:56 +08008121 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8123
8124 pgoff = bvec->bv_offset;
8125next_block:
8126 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8127 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8128 bvec->bv_page, pgoff, start,
8129 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008130 if (likely(!ret))
8131 goto next;
8132try_again:
8133 done.uptodate = 0;
8134 done.start = start;
8135 init_completion(&done.done);
8136
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8138 pgoff, start, start + sectorsize - 1,
8139 io_bio->mirror_num,
8140 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008141 if (ret) {
8142 err = ret;
8143 goto next;
8144 }
8145
8146 wait_for_completion(&done.done);
8147
8148 if (!done.uptodate) {
8149 /* We might have another mirror, so try again */
8150 goto try_again;
8151 }
8152next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153 offset += sectorsize;
8154 start += sectorsize;
8155
8156 ASSERT(nr_sectors);
8157
Liu Bo97bf5a52017-04-07 13:11:10 -07008158 nr_sectors--;
8159 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308160 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008161 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308162 goto next_block;
8163 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008164 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008165
8166 return err;
8167}
8168
Miao Xie8b110e32014-09-12 18:44:03 +08008169static int btrfs_subio_endio_read(struct inode *inode,
8170 struct btrfs_io_bio *io_bio, int err)
8171{
8172 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8173
8174 if (skip_csum) {
8175 if (unlikely(err))
8176 return __btrfs_correct_data_nocsum(inode, io_bio);
8177 else
8178 return 0;
8179 } else {
8180 return __btrfs_subio_endio_read(inode, io_bio, err);
8181 }
8182}
8183
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008184static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008185{
8186 struct btrfs_dio_private *dip = bio->bi_private;
8187 struct inode *inode = dip->inode;
8188 struct bio *dio_bio;
8189 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008190 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008191
Miao Xie8b110e32014-09-12 18:44:03 +08008192 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8193 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008194
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008196 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008197 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198
Josef Bacik4b46fce2010-05-23 11:00:55 -04008199 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008200
Filipe Manana1636d1d2016-02-15 16:20:26 +00008201 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008202 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008203
8204 if (io_bio->end_io)
8205 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008206 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207}
8208
Qu Wenruo524272602017-03-08 10:25:52 +08008209static void __endio_write_update_ordered(struct inode *inode,
8210 const u64 offset, const u64 bytes,
8211 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008212{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008213 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008214 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008215 struct btrfs_workqueue *wq;
8216 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008217 u64 ordered_offset = offset;
8218 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008219 int ret;
8220
Qu Wenruo524272602017-03-08 10:25:52 +08008221 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8222 wq = fs_info->endio_freespace_worker;
8223 func = btrfs_freespace_write_helper;
8224 } else {
8225 wq = fs_info->endio_write_workers;
8226 func = btrfs_endio_write_helper;
8227 }
8228
Chris Mason163cf092010-11-28 19:56:33 -05008229again:
8230 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8231 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008232 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008233 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008234 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008235 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008236
Qu Wenruo524272602017-03-08 10:25:52 +08008237 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8238 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008239out_test:
8240 /*
8241 * our bio might span multiple ordered extents. If we haven't
8242 * completed the accounting for the whole dio, go back and try again
8243 */
Filipe Manana14543772015-11-24 16:23:54 +00008244 if (ordered_offset < offset + bytes) {
8245 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008246 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008247 goto again;
8248 }
Filipe Manana14543772015-11-24 16:23:54 +00008249}
8250
8251static void btrfs_endio_direct_write(struct bio *bio)
8252{
8253 struct btrfs_dio_private *dip = bio->bi_private;
8254 struct bio *dio_bio = dip->dio_bio;
8255
Qu Wenruo524272602017-03-08 10:25:52 +08008256 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8257 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008258
Josef Bacik4b46fce2010-05-23 11:00:55 -04008259 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008260
Filipe Manana1636d1d2016-02-15 16:20:26 +00008261 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008262 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008263 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008264}
8265
Mike Christie81a75f672016-06-05 14:31:54 -05008266static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008267 struct bio *bio, int mirror_num,
8268 unsigned long bio_flags, u64 offset)
8269{
8270 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008271 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008272 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008273 return 0;
8274}
8275
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008276static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008277{
8278 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008279 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008280
Miao Xie8b110e32014-09-12 18:44:03 +08008281 if (err)
8282 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008283 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008284 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8285 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008286 (unsigned long long)bio->bi_iter.bi_sector,
8287 bio->bi_iter.bi_size, err);
8288
8289 if (dip->subio_endio)
8290 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008291
8292 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008293 dip->errors = 1;
8294
8295 /*
8296 * before atomic variable goto zero, we must make sure
8297 * dip->errors is perceived to be set.
8298 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008299 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008300 }
8301
8302 /* if there are more bios still pending for this dio, just exit */
8303 if (!atomic_dec_and_test(&dip->pending_bios))
8304 goto out;
8305
Chris Mason9be33952013-05-17 18:30:14 -04008306 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008307 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008308 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008309 dip->dio_bio->bi_error = 0;
8310 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008311 }
8312out:
8313 bio_put(bio);
8314}
8315
8316static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8317 u64 first_sector, gfp_t gfp_flags)
8318{
Chris Masonda2f0f72015-07-02 13:57:22 -07008319 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008320 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008321 if (bio)
8322 bio_associate_current(bio);
8323 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008324}
8325
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008326static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008327 struct btrfs_dio_private *dip,
8328 struct bio *bio,
8329 u64 file_offset)
8330{
8331 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8332 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8333 int ret;
8334
8335 /*
8336 * We load all the csum data we need when we submit
8337 * the first bio to reduce the csum tree search and
8338 * contention.
8339 */
8340 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008341 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008342 file_offset);
8343 if (ret)
8344 return ret;
8345 }
8346
8347 if (bio == dip->orig_bio)
8348 return 0;
8349
8350 file_offset -= dip->logical_offset;
8351 file_offset >>= inode->i_sb->s_blocksize_bits;
8352 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8353
8354 return 0;
8355}
8356
Miao Xiee65e1532010-11-22 03:04:43 +00008357static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008358 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008359 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008360{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008361 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008362 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008363 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008364 int ret;
8365
Josef Bacikb812ce22012-11-16 13:56:32 -05008366 if (async_submit)
8367 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8368
Miao Xiee65e1532010-11-22 03:04:43 +00008369 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008370
8371 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008372 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008373 if (ret)
8374 goto err;
8375 }
Miao Xiee65e1532010-11-22 03:04:43 +00008376
Josef Bacik1ae39932011-04-06 14:41:34 -04008377 if (skip_sum)
8378 goto map;
8379
8380 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008381 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8382 file_offset,
8383 __btrfs_submit_bio_start_direct_io,
8384 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008385 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008386 } else if (write) {
8387 /*
8388 * If we aren't doing async submit, calculate the csum of the
8389 * bio now.
8390 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008391 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008392 if (ret)
8393 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008394 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008395 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008396 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008397 if (ret)
8398 goto err;
8399 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008400map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008401 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008402err:
8403 bio_put(bio);
8404 return ret;
8405}
8406
Mike Christie81a75f672016-06-05 14:31:54 -05008407static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008408 int skip_sum)
8409{
8410 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008411 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008412 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008413 struct bio *bio;
8414 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008415 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008416 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008417 u64 file_offset = dip->logical_offset;
8418 u64 submit_len = 0;
8419 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008420 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008421 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308422 int nr_sectors;
8423 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008424 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008425
Kent Overstreet4f024f32013-10-11 15:44:27 -07008426 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008427 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8428 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008429 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008430 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008431
Kent Overstreet4f024f32013-10-11 15:44:27 -07008432 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008433 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008434 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008435 goto submit;
8436 }
8437
David Woodhouse53b381b2013-01-29 18:40:14 -05008438 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008439 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008440 async_submit = 0;
8441 else
8442 async_submit = 1;
8443
Josef Bacik02f57c72011-04-06 14:25:44 -04008444 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8445 if (!bio)
8446 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008447
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008448 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008449 bio->bi_private = dip;
8450 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008451 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008452 atomic_inc(&dip->pending_bios);
8453
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008454 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008455 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308456 i = 0;
8457next_block:
8458 if (unlikely(map_length < submit_len + blocksize ||
8459 bio_add_page(bio, bvec->bv_page, blocksize,
8460 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008461 /*
8462 * inc the count before we submit the bio so
8463 * we know the end IO handler won't happen before
8464 * we inc the count. Otherwise, the dip might get freed
8465 * before we're done setting it up
8466 */
8467 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008468 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008469 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008470 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008471 if (ret) {
8472 bio_put(bio);
8473 atomic_dec(&dip->pending_bios);
8474 goto out_err;
8475 }
8476
Miao Xiee65e1532010-11-22 03:04:43 +00008477 start_sector += submit_len >> 9;
8478 file_offset += submit_len;
8479
8480 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008481
8482 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8483 start_sector, GFP_NOFS);
8484 if (!bio)
8485 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008486 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008487 bio->bi_private = dip;
8488 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008489 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008490
Kent Overstreet4f024f32013-10-11 15:44:27 -07008491 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008492 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008493 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008494 &map_length, NULL, 0);
8495 if (ret) {
8496 bio_put(bio);
8497 goto out_err;
8498 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308499
8500 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008501 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308502 submit_len += blocksize;
8503 if (--nr_sectors) {
8504 i++;
8505 goto next_block;
8506 }
Miao Xiee65e1532010-11-22 03:04:43 +00008507 }
8508 }
8509
Josef Bacik02f57c72011-04-06 14:25:44 -04008510submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008511 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008512 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008513 if (!ret)
8514 return 0;
8515
8516 bio_put(bio);
8517out_err:
8518 dip->errors = 1;
8519 /*
8520 * before atomic variable goto zero, we must
8521 * make sure dip->errors is perceived to be set.
8522 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008523 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008524 if (atomic_dec_and_test(&dip->pending_bios))
8525 bio_io_error(dip->orig_bio);
8526
8527 /* bio_end_io() will handle error, so we needn't return it */
8528 return 0;
8529}
8530
Mike Christie8a4c1e42016-06-05 14:31:50 -05008531static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8532 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008533{
Filipe Manana61de7182015-07-01 12:13:10 +01008534 struct btrfs_dio_private *dip = NULL;
8535 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008536 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008537 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008538 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008539 int ret = 0;
8540
8541 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8542
Chris Mason9be33952013-05-17 18:30:14 -04008543 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008544 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008545 ret = -ENOMEM;
8546 goto free_ordered;
8547 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008548
Miao Xiec1dc0892014-09-12 18:43:56 +08008549 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008550 if (!dip) {
8551 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008552 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008553 }
8554
8555 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008556 dip->inode = inode;
8557 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008558 dip->bytes = dio_bio->bi_iter.bi_size;
8559 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008560 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008561 dip->orig_bio = io_bio;
8562 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008563 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008564 btrfs_bio = btrfs_io_bio(io_bio);
8565 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008566
Miao Xiec1dc0892014-09-12 18:43:56 +08008567 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008568 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008569 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008570 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008571 dip->subio_endio = btrfs_subio_endio_read;
8572 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008573
Filipe Mananaf28a4922015-12-08 19:23:20 +00008574 /*
8575 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8576 * even if we fail to submit a bio, because in such case we do the
8577 * corresponding error handling below and it must not be done a second
8578 * time by btrfs_direct_IO().
8579 */
8580 if (write) {
8581 struct btrfs_dio_data *dio_data = current->journal_info;
8582
8583 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8584 dip->bytes;
8585 dio_data->unsubmitted_oe_range_start =
8586 dio_data->unsubmitted_oe_range_end;
8587 }
8588
Mike Christie81a75f672016-06-05 14:31:54 -05008589 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008590 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008591 return;
Chris Mason9be33952013-05-17 18:30:14 -04008592
Miao Xie23ea8e52014-09-12 18:43:54 +08008593 if (btrfs_bio->end_io)
8594 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008595
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596free_ordered:
8597 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008598 * If we arrived here it means either we failed to submit the dip
8599 * or we either failed to clone the dio_bio or failed to allocate the
8600 * dip. If we cloned the dio_bio and allocated the dip, we can just
8601 * call bio_endio against our io_bio so that we get proper resource
8602 * cleanup if we fail to submit the dip, otherwise, we must do the
8603 * same as btrfs_endio_direct_[write|read] because we can't call these
8604 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008605 */
Filipe Manana61de7182015-07-01 12:13:10 +01008606 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008607 io_bio->bi_error = -EIO;
8608 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008609 /*
8610 * The end io callbacks free our dip, do the final put on io_bio
8611 * and all the cleanup and final put for dio_bio (through
8612 * dio_end_io()).
8613 */
8614 dip = NULL;
8615 io_bio = NULL;
8616 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008617 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008618 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008619 file_offset,
8620 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008621 false);
Filipe Manana14543772015-11-24 16:23:54 +00008622 else
Filipe Manana61de7182015-07-01 12:13:10 +01008623 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8624 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008625
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008626 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008627 /*
8628 * Releases and cleans up our dio_bio, no need to bio_put()
8629 * nor bio_endio()/bio_io_error() against dio_bio.
8630 */
8631 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008632 }
Filipe Manana61de7182015-07-01 12:13:10 +01008633 if (io_bio)
8634 bio_put(io_bio);
8635 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008636}
8637
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008638static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8639 struct kiocb *iocb,
8640 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008641{
8642 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008643 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008644 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008645 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008646
8647 if (offset & blocksize_mask)
8648 goto out;
8649
Al Viro28060d52014-03-22 05:15:17 -04008650 if (iov_iter_alignment(iter) & blocksize_mask)
8651 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008652
Al Viro28060d52014-03-22 05:15:17 -04008653 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008654 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008655 return 0;
8656 /*
8657 * Check to make sure we don't have duplicate iov_base's in this
8658 * iovec, if so return EINVAL, otherwise we'll get csum errors
8659 * when reading back.
8660 */
8661 for (seg = 0; seg < iter->nr_segs; seg++) {
8662 for (i = seg + 1; i < iter->nr_segs; i++) {
8663 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008664 goto out;
8665 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008666 }
8667 retval = 0;
8668out:
8669 return retval;
8670}
Josef Bacikeb838e72012-07-31 16:28:48 -04008671
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008672static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008673{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008674 struct file *file = iocb->ki_filp;
8675 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008676 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308677 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008678 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008679 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008680 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008681 bool wakeup = true;
8682 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008683 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008684
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008685 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008686 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008687
Jens Axboefe0f07d2015-04-15 17:05:48 -06008688 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008689 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008690
Josef Bacik0e267c42013-07-02 10:38:02 -04008691 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008692 * The generic stuff only does filemap_write_and_wait_range, which
8693 * isn't enough if we've written compressed pages to this area, so
8694 * we need to flush the dirty pages again to make absolutely sure
8695 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008696 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008697 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008698 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8699 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008700 filemap_fdatawrite_range(inode->i_mapping, offset,
8701 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008702
Omar Sandoval6f673762015-03-16 04:33:52 -07008703 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008704 /*
8705 * If the write DIO is beyond the EOF, we need update
8706 * the isize, but it is protected by i_mutex. So we can
8707 * not unlock the i_mutex at this case.
8708 */
8709 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008710 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008711 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008712 relock = true;
8713 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008714 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008715 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008716 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008717 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008718
8719 /*
8720 * We need to know how many extents we reserved so that we can
8721 * do the accounting properly if we go over the number we
8722 * originally calculated. Abuse current->journal_info for this.
8723 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008724 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008725 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008726 dio_data.unsubmitted_oe_range_start = (u64)offset;
8727 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308728 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308729 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008730 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8731 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008732 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008733 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8734 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008735 }
8736
Omar Sandoval17f8c842015-03-16 04:33:50 -07008737 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008738 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008739 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008740 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008741 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308742 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008743 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008744 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308745 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008746 btrfs_delalloc_release_space(inode, offset,
8747 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008748 /*
8749 * On error we might have left some ordered extents
8750 * without submitting corresponding bios for them, so
8751 * cleanup them up to avoid other tasks getting them
8752 * and waiting for them to complete forever.
8753 */
8754 if (dio_data.unsubmitted_oe_range_start <
8755 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008756 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008757 dio_data.unsubmitted_oe_range_start,
8758 dio_data.unsubmitted_oe_range_end -
8759 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008760 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008761 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008762 btrfs_delalloc_release_space(inode, offset,
8763 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008764 }
Miao Xie38851cc2013-02-08 07:04:11 +00008765out:
Miao Xie2e60a512013-02-08 07:01:08 +00008766 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008767 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008768 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008769 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008770
8771 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008772}
8773
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008774#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8775
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008776static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8777 __u64 start, __u64 len)
8778{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008779 int ret;
8780
8781 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8782 if (ret)
8783 return ret;
8784
Chris Masonec29ed52011-02-23 16:23:20 -05008785 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008786}
8787
Chris Mason9ebefb182007-06-15 13:50:00 -04008788int btrfs_readpage(struct file *file, struct page *page)
8789{
Chris Masond1310b22008-01-24 16:13:08 -05008790 struct extent_io_tree *tree;
8791 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008792 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008793}
Chris Mason1832a6d2007-12-21 16:27:21 -05008794
Chris Mason39279cc2007-06-12 06:35:45 -04008795static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8796{
Chris Masond1310b22008-01-24 16:13:08 -05008797 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008798 struct inode *inode = page->mapping->host;
8799 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008800
8801 if (current->flags & PF_MEMALLOC) {
8802 redirty_page_for_writepage(wbc, page);
8803 unlock_page(page);
8804 return 0;
8805 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008806
8807 /*
8808 * If we are under memory pressure we will call this directly from the
8809 * VM, we need to make sure we have the inode referenced for the ordered
8810 * extent. If not just return like we didn't do anything.
8811 */
8812 if (!igrab(inode)) {
8813 redirty_page_for_writepage(wbc, page);
8814 return AOP_WRITEPAGE_ACTIVATE;
8815 }
Chris Masond1310b22008-01-24 16:13:08 -05008816 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008817 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8818 btrfs_add_delayed_iput(inode);
8819 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008820}
Chris Mason39279cc2007-06-12 06:35:45 -04008821
Eric Sandeen48a3b632013-04-25 20:41:01 +00008822static int btrfs_writepages(struct address_space *mapping,
8823 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008824{
Chris Masond1310b22008-01-24 16:13:08 -05008825 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008826
Chris Masond1310b22008-01-24 16:13:08 -05008827 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008828 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8829}
8830
Chris Mason3ab2fb52007-11-08 10:59:22 -05008831static int
8832btrfs_readpages(struct file *file, struct address_space *mapping,
8833 struct list_head *pages, unsigned nr_pages)
8834{
Chris Masond1310b22008-01-24 16:13:08 -05008835 struct extent_io_tree *tree;
8836 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008837 return extent_readpages(tree, mapping, pages, nr_pages,
8838 btrfs_get_extent);
8839}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008840static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008841{
Chris Masond1310b22008-01-24 16:13:08 -05008842 struct extent_io_tree *tree;
8843 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008844 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008845
Chris Masond1310b22008-01-24 16:13:08 -05008846 tree = &BTRFS_I(page->mapping->host)->io_tree;
8847 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008848 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008849 if (ret == 1) {
8850 ClearPagePrivate(page);
8851 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008852 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008853 }
8854 return ret;
8855}
Chris Mason39279cc2007-06-12 06:35:45 -04008856
Chris Masone6dcd2d2008-07-17 12:53:50 -04008857static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8858{
Chris Mason98509cf2008-09-11 15:51:43 -04008859 if (PageWriteback(page) || PageDirty(page))
8860 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008861 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862}
8863
Lukas Czernerd47992f2013-05-21 23:17:23 -04008864static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8865 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008866{
Josef Bacik5fd02042012-05-02 14:00:54 -04008867 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008868 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008869 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008870 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008871 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008872 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308873 u64 start;
8874 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008875 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008876
Chris Mason8b62b722009-09-02 16:53:46 -04008877 /*
8878 * we have the page locked, so new writeback can't start,
8879 * and the dirty bit won't be cleared while we are here.
8880 *
8881 * Wait for IO on this page so that we can safely clear
8882 * the PagePrivate2 bit and do ordered accounting
8883 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008885
Josef Bacik5fd02042012-05-02 14:00:54 -04008886 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008887 if (offset) {
8888 btrfs_releasepage(page, GFP_NOFS);
8889 return;
8890 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008891
8892 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008893 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308894again:
8895 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008896 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008898 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308899 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008900 /*
8901 * IO on this page will never be started, so we need
8902 * to account for any ordered extents now
8903 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008904 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308905 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906 EXTENT_DIRTY | EXTENT_DELALLOC |
8907 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8908 EXTENT_DEFRAG, 1, 0, &cached_state,
8909 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008910 /*
8911 * whoever cleared the private bit is responsible
8912 * for the finish_ordered_io
8913 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008914 if (TestClearPagePrivate2(page)) {
8915 struct btrfs_ordered_inode_tree *tree;
8916 u64 new_len;
8917
8918 tree = &BTRFS_I(inode)->ordered_tree;
8919
8920 spin_lock_irq(&tree->lock);
8921 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308922 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008923 if (new_len < ordered->truncated_len)
8924 ordered->truncated_len = new_len;
8925 spin_unlock_irq(&tree->lock);
8926
8927 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308928 start,
8929 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008930 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008931 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008932 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008933 if (!inode_evicting) {
8934 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308935 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008936 &cached_state);
8937 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308938
8939 start = end + 1;
8940 if (start < page_end)
8941 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008943
Qu Wenruob9d0b382015-09-29 10:35:16 +08008944 /*
8945 * Qgroup reserved space handler
8946 * Page here will be either
8947 * 1) Already written to disk
8948 * In this case, its reserved space is released from data rsv map
8949 * and will be freed by delayed_ref handler finally.
8950 * So even we call qgroup_free_data(), it won't decrease reserved
8951 * space.
8952 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008953 * This means the reserved space should be freed here. However,
8954 * if a truncate invalidates the page (by clearing PageDirty)
8955 * and the page is accounted for while allocating extent
8956 * in btrfs_check_data_free_space() we let delayed_ref to
8957 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008958 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008959 if (PageDirty(page))
8960 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008961 if (!inode_evicting) {
8962 clear_extent_bit(tree, page_start, page_end,
8963 EXTENT_LOCKED | EXTENT_DIRTY |
8964 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8965 EXTENT_DEFRAG, 1, 1,
8966 &cached_state, GFP_NOFS);
8967
8968 __btrfs_releasepage(page, GFP_NOFS);
8969 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970
Chris Mason4a096752008-07-21 10:29:44 -04008971 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008972 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008973 ClearPagePrivate(page);
8974 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008975 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008976 }
Chris Mason39279cc2007-06-12 06:35:45 -04008977}
8978
Chris Mason9ebefb182007-06-15 13:50:00 -04008979/*
8980 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8981 * called from a page fault handler when a page is first dirtied. Hence we must
8982 * be careful to check for EOF conditions here. We set the page up correctly
8983 * for a written page which means we get ENOSPC checking when writing into
8984 * holes and correct delalloc and unwritten extent mapping on filesystems that
8985 * support these features.
8986 *
8987 * We are not allowed to take the i_mutex here so we have to play games to
8988 * protect against truncate races as the page could now be beyond EOF. Because
8989 * vmtruncate() writes the inode size before removing pages, once we have the
8990 * page lock we can determine safely if the page is beyond EOF. If it is not
8991 * beyond EOF, then the page is guaranteed safe against truncation until we
8992 * unlock the page.
8993 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008994int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008995{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008996 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008997 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008998 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008999 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9000 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009001 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009002 char *kaddr;
9003 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009004 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009005 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009006 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309007 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009008 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309010 u64 end;
9011
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009012 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009013
Jan Karab2b5ef52012-06-12 16:20:45 +02009014 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009015 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009016 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309017 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009018
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309019 /*
9020 * Reserving delalloc space after obtaining the page lock can lead to
9021 * deadlock. For example, if a dirty page is locked by this function
9022 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9023 * dirty page write out, then the btrfs_writepage() function could
9024 * end up waiting indefinitely to get a lock on the page currently
9025 * being processed by btrfs_page_mkwrite() function.
9026 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009027 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309028 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009029 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009030 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009031 reserved = 1;
9032 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009033 if (ret) {
9034 if (ret == -ENOMEM)
9035 ret = VM_FAULT_OOM;
9036 else /* -ENOSPC, -EIO, etc */
9037 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009038 if (reserved)
9039 goto out;
9040 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009041 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009042
Nick Piggin56a76f82009-03-31 15:23:23 -07009043 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009044again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009045 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009046 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009047
Chris Mason9ebefb182007-06-15 13:50:00 -04009048 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009049 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009050 /* page got truncated out from underneath us */
9051 goto out_unlock;
9052 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009053 wait_on_page_writeback(page);
9054
David Sterbaff13db42015-12-03 14:30:40 +01009055 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009056 set_page_extent_mapped(page);
9057
Chris Masoneb84ae02008-07-17 13:53:27 -04009058 /*
9059 * we can't set the delalloc bits if there are pending ordered
9060 * extents. Drop our locks and wait for them to finish
9061 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009062 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9063 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009064 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009065 unlock_extent_cached(io_tree, page_start, page_end,
9066 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009068 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009069 btrfs_put_ordered_extent(ordered);
9070 goto again;
9071 }
9072
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009074 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009075 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009076 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309077 end = page_start + reserved_space - 1;
9078 spin_lock(&BTRFS_I(inode)->lock);
9079 BTRFS_I(inode)->outstanding_extents++;
9080 spin_unlock(&BTRFS_I(inode)->lock);
9081 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309083 }
9084 }
9085
Josef Bacikfbf19082009-10-01 17:10:23 -04009086 /*
Liu Bo54160342016-12-13 12:15:19 -08009087 * page_mkwrite gets called when the page is firstly dirtied after it's
9088 * faulted in, but write(2) could also dirty a page and set delalloc
9089 * bits, thus in this case for space account reason, we still need to
9090 * clear any delalloc bits within this page range since we have to
9091 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009092 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309093 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009094 EXTENT_DIRTY | EXTENT_DELALLOC |
9095 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009096 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009097
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309098 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009099 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009100 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009101 unlock_extent_cached(io_tree, page_start, page_end,
9102 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009103 ret = VM_FAULT_SIGBUS;
9104 goto out_unlock;
9105 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009106 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009107
9108 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009109 if (page_start + PAGE_SIZE > size)
9110 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009111 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009112 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009113
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009114 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009115 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009116 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009117 flush_dcache_page(page);
9118 kunmap(page);
9119 }
Chris Mason247e7432008-07-17 12:53:51 -04009120 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009121 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009122 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009123
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009124 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009125 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009126 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009127
Josef Bacik2ac55d42010-02-03 19:33:23 +00009128 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009129
9130out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009131 if (!ret) {
9132 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009133 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009134 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009135 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009136out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309137 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009138out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009139 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009140 return ret;
9141}
9142
Josef Bacika41ad392011-01-31 15:30:16 -05009143static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009145 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009146 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009147 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009148 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009149 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009150 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009151 u64 mask = fs_info->sectorsize - 1;
9152 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009153
Josef Bacik0ef8b722013-10-25 16:13:35 -04009154 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9155 (u64)-1);
9156 if (ret)
9157 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009158
Josef Bacikfcb80c22011-05-03 10:40:22 -04009159 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009160 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 * 3 things going on here
9162 *
9163 * 1) We need to reserve space for our orphan item and the space to
9164 * delete our orphan item. Lord knows we don't want to have a dangling
9165 * orphan item because we didn't reserve space to remove it.
9166 *
9167 * 2) We need to reserve space to update our inode.
9168 *
9169 * 3) We need to have something to cache all the space that is going to
9170 * be free'd up by the truncate operation, but also have some slack
9171 * space reserved in case it uses space during the truncate (thank you
9172 * very much snapshotting).
9173 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009174 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009175 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009176 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009177 * doesn't end up using space reserved for updating the inode or
9178 * removing the orphan item. We also need to be able to stop the
9179 * transaction and start a new one, which means we need to be able to
9180 * update the inode several times, and we have no idea of knowing how
9181 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009182 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009183 * Then there is the orphan item, which does indeed need to be held on
9184 * to for the whole operation, and we need nobody to touch this reserved
9185 * space except the orphan code.
9186 *
9187 * So that leaves us with
9188 *
9189 * 1) root->orphan_block_rsv - for the orphan deletion.
9190 * 2) rsv - for the truncate reservation, which we will steal from the
9191 * transaction reservation.
9192 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9193 * updating the inode.
9194 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009195 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009196 if (!rsv)
9197 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009198 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009199 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009200
Josef Bacik907cbce2011-08-08 13:46:15 -04009201 /*
Josef Bacik07127182011-08-19 10:29:59 -04009202 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009203 * 1 for updating the inode.
9204 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009205 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009206 if (IS_ERR(trans)) {
9207 err = PTR_ERR(trans);
9208 goto out;
9209 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009210
Josef Bacik907cbce2011-08-08 13:46:15 -04009211 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009212 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009213 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009214 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009215
Chris Mason5a3f23d2009-03-31 13:27:11 -04009216 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009217 * So if we truncate and then write and fsync we normally would just
9218 * write the extents that changed, which is a problem if we need to
9219 * first truncate that entire inode. So set this flag so we write out
9220 * all of the extents in the inode to the sync log so we're completely
9221 * safe.
9222 */
9223 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009224 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009225
Yan, Zheng80825102009-11-12 09:35:36 +00009226 while (1) {
9227 ret = btrfs_truncate_inode_items(trans, root, inode,
9228 inode->i_size,
9229 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009230 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009231 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009232 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009233 }
Chris Mason39279cc2007-06-12 06:35:45 -04009234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009235 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009236 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009237 if (ret) {
9238 err = ret;
9239 break;
9240 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009241
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009242 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009243 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009244
9245 trans = btrfs_start_transaction(root, 2);
9246 if (IS_ERR(trans)) {
9247 ret = err = PTR_ERR(trans);
9248 trans = NULL;
9249 break;
9250 }
9251
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009252 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009253 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009254 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009255 BUG_ON(ret); /* shouldn't happen */
9256 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009257 }
9258
9259 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009260 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009261 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009262 if (ret)
9263 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009264 }
9265
Chris Mason917c16b2011-11-08 14:49:59 -05009266 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009267 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009268 ret = btrfs_update_inode(trans, root, inode);
9269 if (ret && !err)
9270 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009271
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009272 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009273 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009274 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009275out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009276 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009277
Josef Bacik3893e332011-01-31 16:03:11 -05009278 if (ret && !err)
9279 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009280
Josef Bacik3893e332011-01-31 16:03:11 -05009281 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009282}
9283
Sven Wegener3b963622008-06-09 21:57:42 -04009284/*
Chris Masond352ac62008-09-29 15:18:18 -04009285 * create a new subvolume directory/inode (helper for the ioctl).
9286 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009287int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009288 struct btrfs_root *new_root,
9289 struct btrfs_root *parent_root,
9290 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009291{
Chris Mason39279cc2007-06-12 06:35:45 -04009292 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009293 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009294 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009295
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009296 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9297 new_dirid, new_dirid,
9298 S_IFDIR | (~current_umask() & S_IRWXUGO),
9299 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009300 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009301 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009302 inode->i_op = &btrfs_dir_inode_operations;
9303 inode->i_fop = &btrfs_dir_file_operations;
9304
Miklos Szeredibfe86842011-10-28 14:13:29 +02009305 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009306 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009307 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009308
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009309 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9310 if (err)
9311 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009312 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009313 new_root->root_key.objectid, err);
9314
Yan, Zheng76dda932009-09-21 16:00:26 -04009315 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009316
Yan, Zheng76dda932009-09-21 16:00:26 -04009317 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009318 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009319}
9320
Chris Mason39279cc2007-06-12 06:35:45 -04009321struct inode *btrfs_alloc_inode(struct super_block *sb)
9322{
9323 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009325
9326 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9327 if (!ei)
9328 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009329
9330 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009331 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009332 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009333 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009334 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009335 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009336 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009337 ei->disk_i_size = 0;
9338 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009339 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009341 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009343 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009344 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009345
Josef Bacik9e0baf62011-07-15 15:16:44 +00009346 spin_lock_init(&ei->lock);
9347 ei->outstanding_extents = 0;
9348 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009349
Josef Bacik72ac3c02012-05-23 14:13:11 -04009350 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009351 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009352
Miao Xie16cdcec2011-04-22 18:12:22 +08009353 ei->delayed_node = NULL;
9354
chandan r9cc97d62012-07-04 12:48:07 +05309355 ei->i_otime.tv_sec = 0;
9356 ei->i_otime.tv_nsec = 0;
9357
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009358 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009359 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009360 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9361 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009362 ei->io_tree.track_uptodate = 1;
9363 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009364 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009365 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009366 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009367 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009368 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009369 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009370 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009371 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009372
9373 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009374}
9375
Josef Bacikaaedb552013-10-11 14:44:09 -04009376#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9377void btrfs_test_destroy_inode(struct inode *inode)
9378{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009379 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009380 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9381}
9382#endif
9383
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009384static void btrfs_i_callback(struct rcu_head *head)
9385{
9386 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009387 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9388}
9389
Chris Mason39279cc2007-06-12 06:35:45 -04009390void btrfs_destroy_inode(struct inode *inode)
9391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009392 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009393 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009394 struct btrfs_root *root = BTRFS_I(inode)->root;
9395
Al Virob3d9b7a2012-06-09 13:51:19 -04009396 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009397 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009398 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9399 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009400 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9401 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009402 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009403
Chris Mason5a3f23d2009-03-31 13:27:11 -04009404 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009405 * This can happen where we create an inode, but somebody else also
9406 * created the same inode and we need to destroy the one we already
9407 * created.
9408 */
9409 if (!root)
9410 goto free;
9411
Josef Bacik8a35d952012-05-23 14:26:42 -04009412 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9413 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009414 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009415 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009416 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009417 }
Josef Bacik7b128762008-07-24 12:17:14 -04009418
Chris Masond3977122009-01-05 21:25:51 -05009419 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009420 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9421 if (!ordered)
9422 break;
9423 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009424 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009425 "found ordered extent %llu %llu on inode cleanup",
9426 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009427 btrfs_remove_ordered_extent(inode, ordered);
9428 btrfs_put_ordered_extent(ordered);
9429 btrfs_put_ordered_extent(ordered);
9430 }
9431 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009432 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009433 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009434 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009435free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009436 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009437}
9438
Al Viro45321ac2010-06-07 13:43:19 -04009439int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009440{
9441 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009442
Naohiro Aota6379ef92013-06-06 09:56:34 +00009443 if (root == NULL)
9444 return 1;
9445
Liu Bofa6ac872013-02-20 14:10:23 +00009446 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009447 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009448 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009449 else
Al Viro45321ac2010-06-07 13:43:19 -04009450 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009451}
9452
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009453static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009454{
9455 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9456
9457 inode_init_once(&ei->vfs_inode);
9458}
9459
9460void btrfs_destroy_cachep(void)
9461{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009462 /*
9463 * Make sure all delayed rcu free inodes are flushed before we
9464 * destroy cache.
9465 */
9466 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009467 kmem_cache_destroy(btrfs_inode_cachep);
9468 kmem_cache_destroy(btrfs_trans_handle_cachep);
9469 kmem_cache_destroy(btrfs_transaction_cachep);
9470 kmem_cache_destroy(btrfs_path_cachep);
9471 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009472}
9473
9474int btrfs_init_cachep(void)
9475{
David Sterba837e1972012-09-07 03:00:48 -06009476 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009477 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009478 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9479 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009480 if (!btrfs_inode_cachep)
9481 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009482
David Sterba837e1972012-09-07 03:00:48 -06009483 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009484 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009485 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009486 if (!btrfs_trans_handle_cachep)
9487 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009488
David Sterba837e1972012-09-07 03:00:48 -06009489 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009490 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009491 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009492 if (!btrfs_transaction_cachep)
9493 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009494
David Sterba837e1972012-09-07 03:00:48 -06009495 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009496 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009497 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009498 if (!btrfs_path_cachep)
9499 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009500
David Sterba837e1972012-09-07 03:00:48 -06009501 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009502 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009503 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009504 if (!btrfs_free_space_cachep)
9505 goto fail;
9506
Chris Mason39279cc2007-06-12 06:35:45 -04009507 return 0;
9508fail:
9509 btrfs_destroy_cachep();
9510 return -ENOMEM;
9511}
9512
9513static int btrfs_getattr(struct vfsmount *mnt,
9514 struct dentry *dentry, struct kstat *stat)
9515{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009516 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009517 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009518 u32 blocksize = inode->i_sb->s_blocksize;
9519
Chris Mason39279cc2007-06-12 06:35:45 -04009520 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009521 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009522
9523 spin_lock(&BTRFS_I(inode)->lock);
9524 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9525 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009526 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009527 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009528 return 0;
9529}
9530
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009531static int btrfs_rename_exchange(struct inode *old_dir,
9532 struct dentry *old_dentry,
9533 struct inode *new_dir,
9534 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009535{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009536 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009537 struct btrfs_trans_handle *trans;
9538 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009539 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 struct inode *new_inode = new_dentry->d_inode;
9541 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009542 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009544 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9545 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546 u64 old_idx = 0;
9547 u64 new_idx = 0;
9548 u64 root_objectid;
9549 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009550 bool root_log_pinned = false;
9551 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009552
9553 /* we only allow rename subvolume link between subvolumes */
9554 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9555 return -EXDEV;
9556
9557 /* close the race window with snapshot create/destroy ioctl */
9558 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009559 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009561 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562
9563 /*
9564 * We want to reserve the absolute worst case amount of items. So if
9565 * both inodes are subvols and we need to unlink them then that would
9566 * require 4 item modifications, but if they are both normal inodes it
9567 * would require 5 item modifications, so we'll assume their normal
9568 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9569 * should cover the worst case number of items we'll modify.
9570 */
9571 trans = btrfs_start_transaction(root, 12);
9572 if (IS_ERR(trans)) {
9573 ret = PTR_ERR(trans);
9574 goto out_notrans;
9575 }
9576
9577 /*
9578 * We need to find a free sequence number both in the source and
9579 * in the destination directory for the exchange.
9580 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009581 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 if (ret)
9583 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009584 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 if (ret)
9586 goto out_fail;
9587
9588 BTRFS_I(old_inode)->dir_index = 0ULL;
9589 BTRFS_I(new_inode)->dir_index = 0ULL;
9590
9591 /* Reference for the source. */
9592 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9593 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009594 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009596 btrfs_pin_log_trans(root);
9597 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 ret = btrfs_insert_inode_ref(trans, dest,
9599 new_dentry->d_name.name,
9600 new_dentry->d_name.len,
9601 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009602 btrfs_ino(BTRFS_I(new_dir)),
9603 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 if (ret)
9605 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 }
9607
9608 /* And now for the dest. */
9609 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9610 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009611 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009613 btrfs_pin_log_trans(dest);
9614 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 ret = btrfs_insert_inode_ref(trans, root,
9616 old_dentry->d_name.name,
9617 old_dentry->d_name.len,
9618 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009619 btrfs_ino(BTRFS_I(old_dir)),
9620 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 if (ret)
9622 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 }
9624
9625 /* Update inode version and ctime/mtime. */
9626 inode_inc_iversion(old_dir);
9627 inode_inc_iversion(new_dir);
9628 inode_inc_iversion(old_inode);
9629 inode_inc_iversion(new_inode);
9630 old_dir->i_ctime = old_dir->i_mtime = ctime;
9631 new_dir->i_ctime = new_dir->i_mtime = ctime;
9632 old_inode->i_ctime = ctime;
9633 new_inode->i_ctime = ctime;
9634
9635 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009636 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9637 BTRFS_I(old_inode), 1);
9638 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9639 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 }
9641
9642 /* src is a subvolume */
9643 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9644 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9645 ret = btrfs_unlink_subvol(trans, root, old_dir,
9646 root_objectid,
9647 old_dentry->d_name.name,
9648 old_dentry->d_name.len);
9649 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009650 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9651 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 old_dentry->d_name.name,
9653 old_dentry->d_name.len);
9654 if (!ret)
9655 ret = btrfs_update_inode(trans, root, old_inode);
9656 }
9657 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009658 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 goto out_fail;
9660 }
9661
9662 /* dest is a subvolume */
9663 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9664 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9665 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9666 root_objectid,
9667 new_dentry->d_name.name,
9668 new_dentry->d_name.len);
9669 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009670 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9671 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 new_dentry->d_name.name,
9673 new_dentry->d_name.len);
9674 if (!ret)
9675 ret = btrfs_update_inode(trans, dest, new_inode);
9676 }
9677 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009678 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 goto out_fail;
9680 }
9681
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009682 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009683 new_dentry->d_name.name,
9684 new_dentry->d_name.len, 0, old_idx);
9685 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009686 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009687 goto out_fail;
9688 }
9689
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009690 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 old_dentry->d_name.name,
9692 old_dentry->d_name.len, 0, new_idx);
9693 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009694 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 goto out_fail;
9696 }
9697
9698 if (old_inode->i_nlink == 1)
9699 BTRFS_I(old_inode)->dir_index = old_idx;
9700 if (new_inode->i_nlink == 1)
9701 BTRFS_I(new_inode)->dir_index = new_idx;
9702
Filipe Manana86e8aa02016-05-05 02:02:27 +01009703 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009705 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9706 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009708 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009710 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009712 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9713 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009715 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 }
9717out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009718 /*
9719 * If we have pinned a log and an error happened, we unpin tasks
9720 * trying to sync the log and force them to fallback to a transaction
9721 * commit if the log currently contains any of the inodes involved in
9722 * this rename operation (to ensure we do not persist a log with an
9723 * inconsistent state for any of these inodes or leading to any
9724 * inconsistencies when replayed). If the transaction was aborted, the
9725 * abortion reason is propagated to userspace when attempting to commit
9726 * the transaction. If the log does not contain any of these inodes, we
9727 * allow the tasks to sync it.
9728 */
9729 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009730 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9731 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9732 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009733 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009734 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009735 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009736
9737 if (root_log_pinned) {
9738 btrfs_end_log_trans(root);
9739 root_log_pinned = false;
9740 }
9741 if (dest_log_pinned) {
9742 btrfs_end_log_trans(dest);
9743 dest_log_pinned = false;
9744 }
9745 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009746 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747out_notrans:
9748 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009749 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009751 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752
9753 return ret;
9754}
9755
9756static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9757 struct btrfs_root *root,
9758 struct inode *dir,
9759 struct dentry *dentry)
9760{
9761 int ret;
9762 struct inode *inode;
9763 u64 objectid;
9764 u64 index;
9765
9766 ret = btrfs_find_free_ino(root, &objectid);
9767 if (ret)
9768 return ret;
9769
9770 inode = btrfs_new_inode(trans, root, dir,
9771 dentry->d_name.name,
9772 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009773 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 objectid,
9775 S_IFCHR | WHITEOUT_MODE,
9776 &index);
9777
9778 if (IS_ERR(inode)) {
9779 ret = PTR_ERR(inode);
9780 return ret;
9781 }
9782
9783 inode->i_op = &btrfs_special_inode_operations;
9784 init_special_inode(inode, inode->i_mode,
9785 WHITEOUT_DEV);
9786
9787 ret = btrfs_init_inode_security(trans, inode, dir,
9788 &dentry->d_name);
9789 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009790 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009791
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009792 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9793 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009795 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009796
9797 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009798out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009800 if (ret)
9801 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 iput(inode);
9803
Filipe Mananac9901612016-05-05 01:41:57 +01009804 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805}
9806
Chris Mason39279cc2007-06-12 06:35:45 -04009807static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 struct inode *new_dir, struct dentry *new_dentry,
9809 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009810{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009811 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009812 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009813 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009814 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9815 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009816 struct inode *new_inode = d_inode(new_dentry);
9817 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009818 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009819 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009820 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009821 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009822 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009823
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009824 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009825 return -EPERM;
9826
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009828 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009829 return -EXDEV;
9830
Li Zefan33345d012011-04-20 10:31:50 +08009831 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009832 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009833 return -ENOTEMPTY;
9834
Chris Mason39279cc2007-06-12 06:35:45 -04009835 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009836 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009837 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009838
9839
9840 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009841 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009842 new_dentry->d_name.name,
9843 new_dentry->d_name.len);
9844
9845 if (ret) {
9846 if (ret == -EEXIST) {
9847 /* we shouldn't get
9848 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309849 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009850 return ret;
9851 }
9852 } else {
9853 /* maybe -EOVERFLOW */
9854 return ret;
9855 }
9856 }
9857 ret = 0;
9858
Chris Mason5a3f23d2009-03-31 13:27:11 -04009859 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009860 * we're using rename to replace one file with another. Start IO on it
9861 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009862 */
Chris Mason8d875f92014-08-12 10:47:42 -07009863 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009864 filemap_flush(old_inode->i_mapping);
9865
Yan, Zheng76dda932009-09-21 16:00:26 -04009866 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009867 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009868 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009869 /*
9870 * We want to reserve the absolute worst case amount of items. So if
9871 * both inodes are subvols and we need to unlink them then that would
9872 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009873 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009874 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9875 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009876 * If our rename has the whiteout flag, we need more 5 units for the
9877 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9878 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009879 */
Filipe Manana5062af32016-05-05 10:26:26 +01009880 trans_num_items = 11;
9881 if (flags & RENAME_WHITEOUT)
9882 trans_num_items += 5;
9883 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009884 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885 ret = PTR_ERR(trans);
9886 goto out_notrans;
9887 }
Chris Mason5f39d392007-10-15 16:14:19 -04009888
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 if (dest != root)
9890 btrfs_record_root_in_trans(trans, dest);
9891
Nikolay Borisov877574e2017-02-20 13:50:33 +02009892 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009893 if (ret)
9894 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009895
Miao Xie67de1172013-12-26 13:07:06 +08009896 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009897 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009898 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009899 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009901 btrfs_pin_log_trans(root);
9902 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009903 ret = btrfs_insert_inode_ref(trans, dest,
9904 new_dentry->d_name.name,
9905 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009906 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009907 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009908 if (ret)
9909 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009911
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009912 inode_inc_iversion(old_dir);
9913 inode_inc_iversion(new_dir);
9914 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009915 old_dir->i_ctime = old_dir->i_mtime =
9916 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009917 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009918
Chris Mason12fcfd22009-03-24 10:24:20 -04009919 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009920 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9921 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009922
Li Zefan33345d012011-04-20 10:31:50 +08009923 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009924 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9925 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9926 old_dentry->d_name.name,
9927 old_dentry->d_name.len);
9928 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009929 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9930 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009931 old_dentry->d_name.name,
9932 old_dentry->d_name.len);
9933 if (!ret)
9934 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009935 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009936 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009937 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009938 goto out_fail;
9939 }
Chris Mason39279cc2007-06-12 06:35:45 -04009940
9941 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009942 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009943 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009944 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9946 root_objectid = BTRFS_I(new_inode)->location.objectid;
9947 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9948 root_objectid,
9949 new_dentry->d_name.name,
9950 new_dentry->d_name.len);
9951 BUG_ON(new_inode->i_nlink == 0);
9952 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009953 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9954 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009955 new_dentry->d_name.name,
9956 new_dentry->d_name.len);
9957 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009958 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009959 ret = btrfs_orphan_add(trans,
9960 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009961 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009962 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009963 goto out_fail;
9964 }
Chris Mason39279cc2007-06-12 06:35:45 -04009965 }
Josef Bacikaec74772008-07-24 12:12:38 -04009966
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009967 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009968 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009969 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009971 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 goto out_fail;
9973 }
Chris Mason39279cc2007-06-12 06:35:45 -04009974
Miao Xie67de1172013-12-26 13:07:06 +08009975 if (old_inode->i_nlink == 1)
9976 BTRFS_I(old_inode)->dir_index = index;
9977
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009978 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009979 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009980
David Sterbaf85b7372017-01-20 14:54:07 +01009981 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9982 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009983 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009984 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009985 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009986
9987 if (flags & RENAME_WHITEOUT) {
9988 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9989 old_dentry);
9990
9991 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009992 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009993 goto out_fail;
9994 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009995 }
Chris Mason39279cc2007-06-12 06:35:45 -04009996out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009997 /*
9998 * If we have pinned the log and an error happened, we unpin tasks
9999 * trying to sync the log and force them to fallback to a transaction
10000 * commit if the log currently contains any of the inodes involved in
10001 * this rename operation (to ensure we do not persist a log with an
10002 * inconsistent state for any of these inodes or leading to any
10003 * inconsistencies when replayed). If the transaction was aborted, the
10004 * abortion reason is propagated to userspace when attempting to commit
10005 * the transaction. If the log does not contain any of these inodes, we
10006 * allow the tasks to sync it.
10007 */
10008 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010009 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10010 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10011 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010012 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010013 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010014 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010015
10016 btrfs_end_log_trans(root);
10017 log_pinned = false;
10018 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010019 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010020out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010021 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010022 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010023
Chris Mason39279cc2007-06-12 06:35:45 -040010024 return ret;
10025}
10026
Miklos Szeredi80ace852014-07-23 15:15:32 +020010027static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10028 struct inode *new_dir, struct dentry *new_dentry,
10029 unsigned int flags)
10030{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010031 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010032 return -EINVAL;
10033
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010034 if (flags & RENAME_EXCHANGE)
10035 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10036 new_dentry);
10037
10038 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010039}
10040
Miao Xie8ccf6f192012-10-25 09:28:04 +000010041static void btrfs_run_delalloc_work(struct btrfs_work *work)
10042{
10043 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010044 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045
10046 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10047 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010048 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010049 filemap_flush(inode->i_mapping);
10050 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10051 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010052 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010053
10054 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010055 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010056 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010057 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010058 complete(&delalloc_work->completion);
10059}
10060
10061struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010062 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010063{
10064 struct btrfs_delalloc_work *work;
10065
David Sterba100d5702015-12-08 14:39:32 +010010066 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010067 if (!work)
10068 return NULL;
10069
10070 init_completion(&work->completion);
10071 INIT_LIST_HEAD(&work->list);
10072 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010073 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010074 WARN_ON_ONCE(!inode);
10075 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10076 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010077
10078 return work;
10079}
10080
10081void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10082{
10083 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010084 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010085}
10086
Chris Masond352ac62008-09-29 15:18:18 -040010087/*
10088 * some fairly slow code that needs optimization. This walks the list
10089 * of all the inodes with pending delalloc and forces them to disk.
10090 */
Miao Xie6c255e62014-03-06 13:55:01 +080010091static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10092 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010093{
Chris Masonea8c2812008-08-04 23:17:27 -040010094 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010095 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010096 struct btrfs_delalloc_work *work, *next;
10097 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010098 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010100
Miao Xie8ccf6f192012-10-25 09:28:04 +000010101 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010102 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010103
Miao Xie573bfb72014-03-06 13:55:03 +080010104 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105 spin_lock(&root->delalloc_lock);
10106 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010107 while (!list_empty(&splice)) {
10108 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010109 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010110
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 list_move_tail(&binode->delalloc_inodes,
10112 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010113 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010114 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010116 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010117 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010119
David Sterba651d4942015-11-27 19:24:16 +010010120 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010121 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010122 if (delay_iput)
10123 btrfs_add_delayed_iput(inode);
10124 else
10125 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010126 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010127 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010128 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010129 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010130 btrfs_queue_work(root->fs_info->flush_workers,
10131 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010132 ret++;
10133 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010134 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010135 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010136 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010137 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010139
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010140out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010141 list_for_each_entry_safe(work, next, &works, list) {
10142 list_del_init(&work->list);
10143 btrfs_wait_and_free_delalloc_work(work);
10144 }
10145
Miao Xieeb73c1b2013-05-15 07:48:22 +000010146 if (!list_empty_careful(&splice)) {
10147 spin_lock(&root->delalloc_lock);
10148 list_splice_tail(&splice, &root->delalloc_inodes);
10149 spin_unlock(&root->delalloc_lock);
10150 }
Miao Xie573bfb72014-03-06 13:55:03 +080010151 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010152 return ret;
10153}
10154
10155int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10156{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010157 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010158 int ret;
10159
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 return -EROFS;
10162
Miao Xie6c255e62014-03-06 13:55:01 +080010163 ret = __start_delalloc_inodes(root, delay_iput, -1);
10164 if (ret > 0)
10165 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010166 /*
10167 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010168 * we have to make sure the IO is actually started and that
10169 * ordered extents get created before we return
10170 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010171 atomic_inc(&fs_info->async_submit_draining);
10172 while (atomic_read(&fs_info->nr_async_submits) ||
10173 atomic_read(&fs_info->async_delalloc_pages)) {
10174 wait_event(fs_info->async_submit_wait,
10175 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10176 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010177 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010178 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 return ret;
10180}
10181
Miao Xie6c255e62014-03-06 13:55:01 +080010182int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10183 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184{
10185 struct btrfs_root *root;
10186 struct list_head splice;
10187 int ret;
10188
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010189 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 return -EROFS;
10191
10192 INIT_LIST_HEAD(&splice);
10193
Miao Xie573bfb72014-03-06 13:55:03 +080010194 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010195 spin_lock(&fs_info->delalloc_root_lock);
10196 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010197 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 root = list_first_entry(&splice, struct btrfs_root,
10199 delalloc_root);
10200 root = btrfs_grab_fs_root(root);
10201 BUG_ON(!root);
10202 list_move_tail(&root->delalloc_root,
10203 &fs_info->delalloc_roots);
10204 spin_unlock(&fs_info->delalloc_root_lock);
10205
Miao Xie6c255e62014-03-06 13:55:01 +080010206 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010208 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 goto out;
10210
Miao Xie6c255e62014-03-06 13:55:01 +080010211 if (nr != -1) {
10212 nr -= ret;
10213 WARN_ON(nr < 0);
10214 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010215 spin_lock(&fs_info->delalloc_root_lock);
10216 }
10217 spin_unlock(&fs_info->delalloc_root_lock);
10218
Miao Xie6c255e62014-03-06 13:55:01 +080010219 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010220 atomic_inc(&fs_info->async_submit_draining);
10221 while (atomic_read(&fs_info->nr_async_submits) ||
10222 atomic_read(&fs_info->async_delalloc_pages)) {
10223 wait_event(fs_info->async_submit_wait,
10224 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10225 atomic_read(&fs_info->async_delalloc_pages) == 0));
10226 }
10227 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010228out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010229 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010230 spin_lock(&fs_info->delalloc_root_lock);
10231 list_splice_tail(&splice, &fs_info->delalloc_roots);
10232 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010233 }
Miao Xie573bfb72014-03-06 13:55:03 +080010234 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010235 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010236}
10237
Chris Mason39279cc2007-06-12 06:35:45 -040010238static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10239 const char *symname)
10240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010241 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010242 struct btrfs_trans_handle *trans;
10243 struct btrfs_root *root = BTRFS_I(dir)->root;
10244 struct btrfs_path *path;
10245 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010246 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010247 int err;
10248 int drop_inode = 0;
10249 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010250 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010251 int name_len;
10252 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010253 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010254 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010255 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010256
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010257 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010258 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010259 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010260
Josef Bacik9ed74f22009-09-11 16:12:44 -040010261 /*
10262 * 2 items for inode item and ref
10263 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010264 * 1 item for updating parent inode item
10265 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010266 * 1 item for xattr if selinux is on
10267 */
Filipe Manana9269d122015-12-31 18:16:29 +000010268 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010269 if (IS_ERR(trans))
10270 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010271
Li Zefan581bb052011-04-20 10:06:11 +080010272 err = btrfs_find_free_ino(root, &objectid);
10273 if (err)
10274 goto out_unlock;
10275
Josef Bacikaec74772008-07-24 12:12:38 -040010276 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010277 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10278 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010279 if (IS_ERR(inode)) {
10280 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010281 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010282 }
Chris Mason39279cc2007-06-12 06:35:45 -040010283
Casey Schauflerad19db72011-12-15 10:09:07 -050010284 /*
10285 * If the active LSM wants to access the inode during
10286 * d_instantiate it needs these. Smack checks to see
10287 * if the filesystem supports xattrs by looking at the
10288 * ops vector.
10289 */
10290 inode->i_fop = &btrfs_file_operations;
10291 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010292 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010293 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10294
10295 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10296 if (err)
10297 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010298
Chris Mason39279cc2007-06-12 06:35:45 -040010299 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010300 if (!path) {
10301 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010302 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010303 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010304 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010305 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010306 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010307 datasize = btrfs_file_extent_calc_inline_size(name_len);
10308 err = btrfs_insert_empty_item(trans, root, path, &key,
10309 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010310 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010311 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010312 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010313 }
Chris Mason5f39d392007-10-15 16:14:19 -040010314 leaf = path->nodes[0];
10315 ei = btrfs_item_ptr(leaf, path->slots[0],
10316 struct btrfs_file_extent_item);
10317 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10318 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010319 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010320 btrfs_set_file_extent_encryption(leaf, ei, 0);
10321 btrfs_set_file_extent_compression(leaf, ei, 0);
10322 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10323 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10324
Chris Mason39279cc2007-06-12 06:35:45 -040010325 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010326 write_extent_buffer(leaf, symname, ptr, name_len);
10327 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010328 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010329
Chris Mason39279cc2007-06-12 06:35:45 -040010330 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010331 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010332 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010333 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010334 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010335 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010336 /*
10337 * Last step, add directory indexes for our symlink inode. This is the
10338 * last step to avoid extra cleanup of these indexes if an error happens
10339 * elsewhere above.
10340 */
10341 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010342 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10343 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010344 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010345 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010346 goto out_unlock_inode;
10347 }
10348
10349 unlock_new_inode(inode);
10350 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010351
10352out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010353 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010354 if (drop_inode) {
10355 inode_dec_link_count(inode);
10356 iput(inode);
10357 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010358 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010359 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010360
10361out_unlock_inode:
10362 drop_inode = 1;
10363 unlock_new_inode(inode);
10364 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010365}
Chris Mason16432982008-04-10 10:23:21 -040010366
Josef Bacik0af3d002010-06-21 14:48:16 -040010367static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10368 u64 start, u64 num_bytes, u64 min_size,
10369 loff_t actual_len, u64 *alloc_hint,
10370 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010371{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010373 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10374 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010375 struct btrfs_root *root = BTRFS_I(inode)->root;
10376 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010377 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010378 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010379 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010380 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010381 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010382 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010383 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010384
Josef Bacik0af3d002010-06-21 14:48:16 -040010385 if (trans)
10386 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010387 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010388 if (own_trans) {
10389 trans = btrfs_start_transaction(root, 3);
10390 if (IS_ERR(trans)) {
10391 ret = PTR_ERR(trans);
10392 break;
10393 }
Yan Zhengd899e052008-10-30 14:25:28 -040010394 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395
Byongho Leeee221842015-12-15 01:42:10 +090010396 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010397 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010398 /*
10399 * If we are severely fragmented we could end up with really
10400 * small allocations, so if the allocator is returning small
10401 * chunks lets make its job easier by only searching for those
10402 * sized chunks.
10403 */
10404 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010405 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10406 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010407 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010408 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010409 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010410 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010411 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010412 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010413
Josef Bacik0b670dc2015-09-23 17:11:16 -040010414 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010415 ret = insert_reserved_file_extent(trans, inode,
10416 cur_offset, ins.objectid,
10417 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010418 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010419 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010420 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010421 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010422 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010423 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010424 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010425 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010426 break;
10427 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010428
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010429 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010430 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010431
Josef Bacik5dc562c2012-08-17 13:14:17 -040010432 em = alloc_extent_map();
10433 if (!em) {
10434 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10435 &BTRFS_I(inode)->runtime_flags);
10436 goto next;
10437 }
10438
10439 em->start = cur_offset;
10440 em->orig_start = cur_offset;
10441 em->len = ins.offset;
10442 em->block_start = ins.objectid;
10443 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010444 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010445 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010446 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010447 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10448 em->generation = trans->transid;
10449
10450 while (1) {
10451 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010452 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010453 write_unlock(&em_tree->lock);
10454 if (ret != -EEXIST)
10455 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010456 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010457 cur_offset + ins.offset - 1,
10458 0);
10459 }
10460 free_extent_map(em);
10461next:
Yan Zhengd899e052008-10-30 14:25:28 -040010462 num_bytes -= ins.offset;
10463 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010464 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010465
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010466 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010467 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010468 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010469 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010470 (actual_len > inode->i_size) &&
10471 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010472 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010473 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010474 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010475 i_size = cur_offset;
10476 i_size_write(inode, i_size);
10477 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010478 }
10479
Yan Zhengd899e052008-10-30 14:25:28 -040010480 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010481
10482 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010483 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010484 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010485 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010486 break;
10487 }
Yan Zhengd899e052008-10-30 14:25:28 -040010488
Josef Bacik0af3d002010-06-21 14:48:16 -040010489 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010490 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010491 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010492 if (cur_offset < end)
10493 btrfs_free_reserved_data_space(inode, cur_offset,
10494 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010495 return ret;
10496}
10497
Josef Bacik0af3d002010-06-21 14:48:16 -040010498int btrfs_prealloc_file_range(struct inode *inode, int mode,
10499 u64 start, u64 num_bytes, u64 min_size,
10500 loff_t actual_len, u64 *alloc_hint)
10501{
10502 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10503 min_size, actual_len, alloc_hint,
10504 NULL);
10505}
10506
10507int btrfs_prealloc_file_range_trans(struct inode *inode,
10508 struct btrfs_trans_handle *trans, int mode,
10509 u64 start, u64 num_bytes, u64 min_size,
10510 loff_t actual_len, u64 *alloc_hint)
10511{
10512 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10513 min_size, actual_len, alloc_hint, trans);
10514}
10515
Chris Masone6dcd2d2008-07-17 12:53:50 -040010516static int btrfs_set_page_dirty(struct page *page)
10517{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010518 return __set_page_dirty_nobuffers(page);
10519}
10520
Al Viro10556cb2011-06-20 19:28:19 -040010521static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010522{
Li Zefanb83cc962010-12-20 16:04:08 +080010523 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010524 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010525
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010526 if (mask & MAY_WRITE &&
10527 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10528 if (btrfs_root_readonly(root))
10529 return -EROFS;
10530 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10531 return -EACCES;
10532 }
Al Viro2830ba72011-06-20 19:16:29 -040010533 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010534}
Chris Mason39279cc2007-06-12 06:35:45 -040010535
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010536static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10537{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010538 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010539 struct btrfs_trans_handle *trans;
10540 struct btrfs_root *root = BTRFS_I(dir)->root;
10541 struct inode *inode = NULL;
10542 u64 objectid;
10543 u64 index;
10544 int ret = 0;
10545
10546 /*
10547 * 5 units required for adding orphan entry
10548 */
10549 trans = btrfs_start_transaction(root, 5);
10550 if (IS_ERR(trans))
10551 return PTR_ERR(trans);
10552
10553 ret = btrfs_find_free_ino(root, &objectid);
10554 if (ret)
10555 goto out;
10556
10557 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010558 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010559 if (IS_ERR(inode)) {
10560 ret = PTR_ERR(inode);
10561 inode = NULL;
10562 goto out;
10563 }
10564
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010565 inode->i_fop = &btrfs_file_operations;
10566 inode->i_op = &btrfs_file_inode_operations;
10567
10568 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010569 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10570
Chris Masonb0d5d102014-09-08 13:08:51 -070010571 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10572 if (ret)
10573 goto out_inode;
10574
10575 ret = btrfs_update_inode(trans, root, inode);
10576 if (ret)
10577 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010578 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010579 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010580 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010581
Filipe Manana5762b5c2014-08-01 00:10:32 +010010582 /*
10583 * We set number of links to 0 in btrfs_new_inode(), and here we set
10584 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10585 * through:
10586 *
10587 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10588 */
10589 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010590 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010591 d_tmpfile(dentry, inode);
10592 mark_inode_dirty(inode);
10593
10594out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010595 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010596 if (ret)
10597 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010598 btrfs_balance_delayed_items(fs_info);
10599 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010600 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010601
10602out_inode:
10603 unlock_new_inode(inode);
10604 goto out;
10605
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010606}
10607
David Sterba20a7db82017-02-17 16:24:29 +010010608__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010609static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010610{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010611 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010612}
10613
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010614static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010615 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010616 .lookup = btrfs_lookup,
10617 .create = btrfs_create,
10618 .unlink = btrfs_unlink,
10619 .link = btrfs_link,
10620 .mkdir = btrfs_mkdir,
10621 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010622 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010623 .symlink = btrfs_symlink,
10624 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010625 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010626 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010627 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010628 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010629 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010630 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010631 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010632};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010633static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010634 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010635 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010636 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010637};
Yan, Zheng76dda932009-09-21 16:00:26 -040010638
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010639static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010640 .llseek = generic_file_llseek,
10641 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010642 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010643 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010644#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010645 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010646#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010647 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010648 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
10650
David Sterba20e55062015-11-19 11:42:28 +010010651static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010652 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010653 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010654 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010655 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010656 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010657
10658 /* optional callbacks */
10659 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010660 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010661 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010662 .set_bit_hook = btrfs_set_bit_hook,
10663 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010664 .merge_extent_hook = btrfs_merge_extent_hook,
10665 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010666};
10667
Chris Mason35054392009-01-21 13:11:13 -050010668/*
10669 * btrfs doesn't support the bmap operation because swapfiles
10670 * use bmap to make a mapping of extents in the file. They assume
10671 * these extents won't change over the life of the file and they
10672 * use the bmap result to do IO directly to the drive.
10673 *
10674 * the btrfs bmap call would return logical addresses that aren't
10675 * suitable for IO and they also will change frequently as COW
10676 * operations happen. So, swapfile + btrfs == corruption.
10677 *
10678 * For now we're avoiding this by dropping bmap.
10679 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010680static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010681 .readpage = btrfs_readpage,
10682 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010683 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010684 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010685 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010686 .invalidatepage = btrfs_invalidatepage,
10687 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010688 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010689 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010690};
10691
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010692static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010693 .readpage = btrfs_readpage,
10694 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010695 .invalidatepage = btrfs_invalidatepage,
10696 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010697};
10698
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010699static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010700 .getattr = btrfs_getattr,
10701 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010702 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010703 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010704 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010705 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010706 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010707 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010708};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010709static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010710 .getattr = btrfs_getattr,
10711 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010712 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010713 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010714 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010715 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010716 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010717};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010718static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010719 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010720 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010721 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010722 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010723 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010725};
Yan, Zheng76dda932009-09-21 16:00:26 -040010726
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010727const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010728 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010729 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010730};