blob: b8b2a3cbdbe174a95e0ad1d8751dafae5908a39e [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;
946 u64 cur_alloc_size;
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;
Chris Mason771ed682008-11-06 22:02:51 -0500950 int ret = 0;
951
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200952 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400953 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500954 ret = -EINVAL;
955 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957
Qu Wenruofda28322013-02-26 08:10:22 +0000958 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500959 num_bytes = max(blocksize, num_bytes);
960 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500961
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200962 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400963
Chris Mason771ed682008-11-06 22:02:51 -0500964 if (start == 0) {
965 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800966 ret = cow_file_range_inline(root, inode, start, end, 0,
967 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800969 extent_clear_unlock_delalloc(inode, start, end,
970 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400971 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400972 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400973 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
974 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800975 btrfs_free_reserved_data_space_noquota(inode, start,
976 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500977 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300978 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500979 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500980 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100981 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100982 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500983 }
984 }
Chris Masonc8b97812008-10-29 14:49:59 -0400985
986 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400987 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400988
Josef Bacik4b46fce2010-05-23 11:00:55 -0400989 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200990 btrfs_drop_extent_cache(BTRFS_I(inode), start,
991 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400992
Chris Masond3977122009-01-05 21:25:51 -0500993 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400994 unsigned long op;
995
Josef Bacik287a0ab2010-03-19 18:07:23 +0000996 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800997 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400998 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800999 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001000 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001002
Chris Mason771ed682008-11-06 22:02:51 -05001003 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001004 em = create_io_em(inode, start, ins.offset, /* len */
1005 start, /* orig_start */
1006 ins.objectid, /* block_start */
1007 ins.offset, /* block_len */
1008 ins.offset, /* orig_block_len */
1009 ram_size, /* ram_bytes */
1010 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001011 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001012 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001013 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001014 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001015
Chris Mason98d20f62008-04-14 09:46:10 -04001016 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001017 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001019 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001020 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001021
Yan Zheng17d217f2008-12-12 10:03:38 -05001022 if (root->root_key.objectid ==
1023 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1024 ret = btrfs_reloc_clone_csums(inode, start,
1025 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001026 /*
1027 * Only drop cache here, and process as normal.
1028 *
1029 * We must not allow extent_clear_unlock_delalloc()
1030 * at out_unlock label to free meta of this ordered
1031 * extent, as its meta should be freed by
1032 * btrfs_finish_ordered_io().
1033 *
1034 * So we must continue until @start is increased to
1035 * skip current ordered extent.
1036 */
Josef Bacik00361582013-08-14 14:02:47 -04001037 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001038 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1039 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001040 }
1041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001042 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001043
Chris Masonc8b97812008-10-29 14:49:59 -04001044 /* we're not doing compressed IO, don't unlock the first
1045 * page (which the caller expects to stay locked), don't
1046 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001047 *
1048 * Do set the Private2 bit so we know this page was properly
1049 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001050 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001051 op = unlock ? PAGE_UNLOCK : 0;
1052 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001053
Josef Bacikc2790a22013-07-29 11:20:47 -04001054 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001055 start + ram_size - 1,
1056 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001057 EXTENT_LOCKED | EXTENT_DELALLOC,
1058 op);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 if (disk_num_bytes < cur_alloc_size)
1060 disk_num_bytes = 0;
1061 else
1062 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001063 num_bytes -= cur_alloc_size;
1064 alloc_hint = ins.objectid + ins.offset;
1065 start += cur_alloc_size;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001066
1067 /*
1068 * btrfs_reloc_clone_csums() error, since start is increased
1069 * extent_clear_unlock_delalloc() at out_unlock label won't
1070 * free metadata of current ordered extent, we're OK to exit.
1071 */
1072 if (ret)
1073 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001074 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001075out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001076 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001077
Filipe Mananad9f85962014-08-25 10:43:00 +01001078out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001079 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001080out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001081 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001082 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001083out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001084 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1085 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001086 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1087 EXTENT_DELALLOC | EXTENT_DEFRAG,
1088 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1089 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001090 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001091}
Chris Masonc8b97812008-10-29 14:49:59 -04001092
Chris Mason771ed682008-11-06 22:02:51 -05001093/*
1094 * work queue call back to started compression on a file and pages
1095 */
1096static noinline void async_cow_start(struct btrfs_work *work)
1097{
1098 struct async_cow *async_cow;
1099 int num_added = 0;
1100 async_cow = container_of(work, struct async_cow, work);
1101
1102 compress_file_range(async_cow->inode, async_cow->locked_page,
1103 async_cow->start, async_cow->end, async_cow,
1104 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001106 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001108 }
Chris Mason771ed682008-11-06 22:02:51 -05001109}
1110
1111/*
1112 * work queue call back to submit previously compressed pages
1113 */
1114static noinline void async_cow_submit(struct btrfs_work *work)
1115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001116 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001117 struct async_cow *async_cow;
1118 struct btrfs_root *root;
1119 unsigned long nr_pages;
1120
1121 async_cow = container_of(work, struct async_cow, work);
1122
1123 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001124 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001125 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1126 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001127
David Sterbaee863952015-02-16 19:41:40 +01001128 /*
1129 * atomic_sub_return implies a barrier for waitqueue_active
1130 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001131 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001132 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001133 waitqueue_active(&fs_info->async_submit_wait))
1134 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001135
Chris Masond3977122009-01-05 21:25:51 -05001136 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001137 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001138}
Chris Masonc8b97812008-10-29 14:49:59 -04001139
Chris Mason771ed682008-11-06 22:02:51 -05001140static noinline void async_cow_free(struct btrfs_work *work)
1141{
1142 struct async_cow *async_cow;
1143 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001144 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001145 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001146 kfree(async_cow);
1147}
1148
1149static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1150 u64 start, u64 end, int *page_started,
1151 unsigned long *nr_written)
1152{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001154 struct async_cow *async_cow;
1155 struct btrfs_root *root = BTRFS_I(inode)->root;
1156 unsigned long nr_pages;
1157 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001158
Chris Masona3429ab2009-10-08 12:30:20 -04001159 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1160 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001161 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001162 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001163 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001164 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001165 async_cow->root = root;
1166 async_cow->locked_page = locked_page;
1167 async_cow->start = start;
1168
Wang Shilongf79707b2014-07-17 11:44:09 +08001169 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001170 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001171 cur_end = end;
1172 else
Byongho Leeee221842015-12-15 01:42:10 +09001173 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001174
1175 async_cow->end = cur_end;
1176 INIT_LIST_HEAD(&async_cow->extents);
1177
Liu Bo9e0af232014-08-15 23:36:53 +08001178 btrfs_init_work(&async_cow->work,
1179 btrfs_delalloc_helper,
1180 async_cow_start, async_cow_submit,
1181 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001182
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001183 nr_pages = (cur_end - start + PAGE_SIZE) >>
1184 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001185 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001186
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001187 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001189 while (atomic_read(&fs_info->async_submit_draining) &&
1190 atomic_read(&fs_info->async_delalloc_pages)) {
1191 wait_event(fs_info->async_submit_wait,
1192 (atomic_read(&fs_info->async_delalloc_pages) ==
1193 0));
Chris Mason771ed682008-11-06 22:02:51 -05001194 }
1195
1196 *nr_written += nr_pages;
1197 start = cur_end + 1;
1198 }
1199 *page_started = 1;
1200 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201}
1202
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001203static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001204 u64 bytenr, u64 num_bytes)
1205{
1206 int ret;
1207 struct btrfs_ordered_sum *sums;
1208 LIST_HEAD(list);
1209
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001210 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001211 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001212 if (ret == 0 && list_empty(&list))
1213 return 0;
1214
1215 while (!list_empty(&list)) {
1216 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1217 list_del(&sums->list);
1218 kfree(sums);
1219 }
1220 return 1;
1221}
1222
Chris Masond352ac62008-09-29 15:18:18 -04001223/*
1224 * when nowcow writeback call back. This checks for snapshots or COW copies
1225 * of the extents that exist in the file, and COWs the file as required.
1226 *
1227 * If no cow copies or snapshots exist, we write directly to the existing
1228 * blocks on disk
1229 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001230static noinline int run_delalloc_nocow(struct inode *inode,
1231 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001232 u64 start, u64 end, int *page_started, int force,
1233 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001235 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001236 struct btrfs_root *root = BTRFS_I(inode)->root;
1237 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001238 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001241 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 u64 cow_start;
1243 u64 cur_offset;
1244 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 u64 disk_bytenr;
1247 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001248 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001249 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001252 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 int nocow;
1254 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001255 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001256 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
1258 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001259 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001260 extent_clear_unlock_delalloc(inode, start, end, end,
1261 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001262 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001263 EXTENT_DO_ACCOUNTING |
1264 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001265 PAGE_CLEAR_DIRTY |
1266 PAGE_SET_WRITEBACK |
1267 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001268 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001269 }
Li Zefan82d59022011-04-20 10:33:24 +08001270
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001271 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001272
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 cow_start = (u64)-1;
1274 cur_offset = start;
1275 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001276 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001278 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1281 leaf = path->nodes[0];
1282 btrfs_item_key_to_cpu(leaf, &found_key,
1283 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001284 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 found_key.type == BTRFS_EXTENT_DATA_KEY)
1286 path->slots[0]--;
1287 }
1288 check_prev = 0;
1289next_slot:
1290 leaf = path->nodes[0];
1291 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1292 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001293 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (ret > 0)
1296 break;
1297 leaf = path->nodes[0];
1298 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 nocow = 0;
1301 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001302 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Filipe Manana1d512cb2015-11-09 00:33:58 +00001305 if (found_key.objectid > ino)
1306 break;
1307 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1308 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1309 path->slots[0]++;
1310 goto next_slot;
1311 }
1312 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.offset > end)
1314 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001315
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (found_key.offset > cur_offset) {
1317 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001318 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 goto out_check;
1320 }
Chris Masonc31f8832008-01-08 15:46:31 -05001321
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 fi = btrfs_item_ptr(leaf, path->slots[0],
1323 struct btrfs_file_extent_item);
1324 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001325
Josef Bacikcc95bef2013-04-04 14:31:27 -04001326 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001327 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1328 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 extent_end = found_key.offset +
1332 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001333 disk_num_bytes =
1334 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (extent_end <= start) {
1336 path->slots[0]++;
1337 goto next_slot;
1338 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 if (disk_bytenr == 0)
1340 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 if (btrfs_file_extent_compression(leaf, fi) ||
1342 btrfs_file_extent_encryption(leaf, fi) ||
1343 btrfs_file_extent_other_encoding(leaf, fi))
1344 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001345 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1346 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001347 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001349 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001350 found_key.offset -
1351 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001354 disk_bytenr += cur_offset - found_key.offset;
1355 num_bytes = min(end + 1, extent_end) - cur_offset;
1356 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001357 * if there are pending snapshots for this root,
1358 * we fall into common COW way.
1359 */
1360 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001361 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001362 if (!err)
1363 goto out_check;
1364 }
1365 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001366 * force cow if csum exists in the range.
1367 * this ensure that csum for a given extent are
1368 * either valid or do not exist.
1369 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001370 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001371 num_bytes)) {
1372 if (!nolock)
1373 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001374 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001375 }
1376 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1377 if (!nolock)
1378 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001379 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001380 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 nocow = 1;
1382 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1383 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001384 btrfs_file_extent_inline_len(leaf,
1385 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001386 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001387 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 } else {
1389 BUG_ON(1);
1390 }
1391out_check:
1392 if (extent_end <= start) {
1393 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001395 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001396 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001397 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001398 goto next_slot;
1399 }
1400 if (!nocow) {
1401 if (cow_start == (u64)-1)
1402 cow_start = cur_offset;
1403 cur_offset = extent_end;
1404 if (cur_offset > end)
1405 break;
1406 path->slots[0]++;
1407 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001408 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001409
David Sterbab3b4aa72011-04-21 01:20:15 +02001410 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001412 ret = cow_file_range(inode, locked_page,
1413 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001414 end, page_started, nr_written, 1,
1415 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001416 if (ret) {
1417 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001418 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001419 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001420 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001421 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001422 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001425 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001426
Yan Zhengd899e052008-10-30 14:25:28 -04001427 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001428 u64 orig_start = found_key.offset - extent_offset;
1429
1430 em = create_io_em(inode, cur_offset, num_bytes,
1431 orig_start,
1432 disk_bytenr, /* block_start */
1433 num_bytes, /* block_len */
1434 disk_num_bytes, /* orig_block_len */
1435 ram_bytes, BTRFS_COMPRESS_NONE,
1436 BTRFS_ORDERED_PREALLOC);
1437 if (IS_ERR(em)) {
1438 if (!nolock && nocow)
1439 btrfs_end_write_no_snapshoting(root);
1440 if (nocow)
1441 btrfs_dec_nocow_writers(fs_info,
1442 disk_bytenr);
1443 ret = PTR_ERR(em);
1444 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001445 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001446 free_extent_map(em);
1447 }
1448
1449 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001450 type = BTRFS_ORDERED_PREALLOC;
1451 } else {
1452 type = BTRFS_ORDERED_NOCOW;
1453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454
1455 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001456 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001457 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001458 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001460
Yan, Zhengefa56462010-05-16 10:49:59 -04001461 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001462 BTRFS_DATA_RELOC_TREE_OBJECTID)
1463 /*
1464 * Error handled later, as we must prevent
1465 * extent_clear_unlock_delalloc() in error handler
1466 * from freeing metadata of created ordered extent.
1467 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001468 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1469 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001470
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001472 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001473 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001474 EXTENT_DELALLOC |
1475 EXTENT_CLEAR_DATA_RESV,
1476 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1477
Wang Shilonge9894fd2014-03-27 11:12:25 +08001478 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001479 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001481
1482 /*
1483 * btrfs_reloc_clone_csums() error, now we're OK to call error
1484 * handler, as metadata for created ordered extent will only
1485 * be freed by btrfs_finish_ordered_io().
1486 */
1487 if (ret)
1488 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001489 if (cur_offset > end)
1490 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001491 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001492 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001493
Josef Bacik17ca04a2012-05-31 15:58:55 -04001494 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001495 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001496 cur_offset = end;
1497 }
1498
Yan Zheng80ff3852008-10-30 14:20:02 -04001499 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001500 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1501 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001502 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001504 }
1505
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001506error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001507 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001508 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001509 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001510 EXTENT_DELALLOC | EXTENT_DEFRAG |
1511 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1512 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 PAGE_SET_WRITEBACK |
1514 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001515 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001516 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001517}
1518
Wang Shilong47059d92014-07-03 18:22:07 +08001519static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1520{
1521
1522 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1523 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1524 return 0;
1525
1526 /*
1527 * @defrag_bytes is a hint value, no spinlock held here,
1528 * if is not zero, it means the file is defragging.
1529 * Force cow if given extent needs to be defragged.
1530 */
1531 if (BTRFS_I(inode)->defrag_bytes &&
1532 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1533 EXTENT_DEFRAG, 0, NULL))
1534 return 1;
1535
1536 return 0;
1537}
1538
Chris Masond352ac62008-09-29 15:18:18 -04001539/*
1540 * extent_io.c call back to do delayed allocation processing
1541 */
Chris Masonc8b97812008-10-29 14:49:59 -04001542static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001543 u64 start, u64 end, int *page_started,
1544 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001545{
Chris Masonbe20aa92007-12-17 20:14:01 -05001546 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001547 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001548
Wang Shilong47059d92014-07-03 18:22:07 +08001549 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001550 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001551 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001552 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001553 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001554 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001555 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001556 ret = cow_file_range(inode, locked_page, start, end, end,
1557 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001558 } else {
1559 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1560 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001561 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001562 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001563 }
Qu Wenruo524272602017-03-08 10:25:52 +08001564 if (ret)
1565 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001566 return ret;
1567}
1568
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001569static void btrfs_split_extent_hook(struct inode *inode,
1570 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001571{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001572 u64 size;
1573
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001574 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001575 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001576 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001577
Josef Bacikdcab6a32015-02-11 15:08:59 -05001578 size = orig->end - orig->start + 1;
1579 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001580 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001581 u64 new_size;
1582
1583 /*
Josef Bacikba117212015-03-13 15:01:24 -04001584 * See the explanation in btrfs_merge_extent_hook, the same
1585 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001586 */
1587 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001588 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001589 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001590 num_extents += count_max_extents(new_size);
1591 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001592 return;
1593 }
1594
Josef Bacik9e0baf62011-07-15 15:16:44 +00001595 spin_lock(&BTRFS_I(inode)->lock);
1596 BTRFS_I(inode)->outstanding_extents++;
1597 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001598}
1599
1600/*
1601 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1602 * extents so we can keep track of new extents that are just merged onto old
1603 * extents, such as when we are doing sequential writes, so we can properly
1604 * account for the metadata space we'll need.
1605 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001606static void btrfs_merge_extent_hook(struct inode *inode,
1607 struct extent_state *new,
1608 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001611 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612
Josef Bacik9ed74f22009-09-11 16:12:44 -04001613 /* not delalloc, ignore it */
1614 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001615 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001616
Josef Bacik8461a3d2015-03-13 15:12:08 -04001617 if (new->start > other->start)
1618 new_size = new->end - other->start + 1;
1619 else
1620 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621
1622 /* we're not bigger than the max, unreserve the space and go */
1623 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1624 spin_lock(&BTRFS_I(inode)->lock);
1625 BTRFS_I(inode)->outstanding_extents--;
1626 spin_unlock(&BTRFS_I(inode)->lock);
1627 return;
1628 }
1629
1630 /*
Josef Bacikba117212015-03-13 15:01:24 -04001631 * We have to add up either side to figure out how many extents were
1632 * accounted for before we merged into one big extent. If the number of
1633 * extents we accounted for is <= the amount we need for the new range
1634 * then we can return, otherwise drop. Think of it like this
1635 *
1636 * [ 4k][MAX_SIZE]
1637 *
1638 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1639 * need 2 outstanding extents, on one side we have 1 and the other side
1640 * we have 1 so they are == and we can return. But in this case
1641 *
1642 * [MAX_SIZE+4k][MAX_SIZE+4k]
1643 *
1644 * Each range on their own accounts for 2 extents, but merged together
1645 * they are only 3 extents worth of accounting, so we need to drop in
1646 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647 */
Josef Bacikba117212015-03-13 15:01:24 -04001648 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001649 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001650 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001651 num_extents += count_max_extents(old_size);
1652 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653 return;
1654
Josef Bacik9e0baf62011-07-15 15:16:44 +00001655 spin_lock(&BTRFS_I(inode)->lock);
1656 BTRFS_I(inode)->outstanding_extents--;
1657 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658}
1659
Miao Xieeb73c1b2013-05-15 07:48:22 +00001660static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1661 struct inode *inode)
1662{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001663 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1664
Miao Xieeb73c1b2013-05-15 07:48:22 +00001665 spin_lock(&root->delalloc_lock);
1666 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1667 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1668 &root->delalloc_inodes);
1669 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1670 &BTRFS_I(inode)->runtime_flags);
1671 root->nr_delalloc_inodes++;
1672 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001673 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001674 BUG_ON(!list_empty(&root->delalloc_root));
1675 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001676 &fs_info->delalloc_roots);
1677 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001678 }
1679 }
1680 spin_unlock(&root->delalloc_lock);
1681}
1682
1683static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001684 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001685{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001686 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687
Miao Xieeb73c1b2013-05-15 07:48:22 +00001688 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001689 if (!list_empty(&inode->delalloc_inodes)) {
1690 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001691 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001692 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001693 root->nr_delalloc_inodes--;
1694 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001695 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001696 BUG_ON(list_empty(&root->delalloc_root));
1697 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001698 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001699 }
1700 }
1701 spin_unlock(&root->delalloc_lock);
1702}
1703
Chris Masond352ac62008-09-29 15:18:18 -04001704/*
1705 * extent_io.c set_bit_hook, used to track delayed allocation
1706 * bytes in this file, and to maintain the list of inodes that
1707 * have pending delalloc work to be done.
1708 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001709static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001710 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001711{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001712
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714
Wang Shilong47059d92014-07-03 18:22:07 +08001715 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1716 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001717 /*
1718 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001719 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001720 * bit, which is only set or cleared with irqs on
1721 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001722 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001723 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001724 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001725 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001726
Josef Bacik9e0baf62011-07-15 15:16:44 +00001727 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001728 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001729 } else {
1730 spin_lock(&BTRFS_I(inode)->lock);
1731 BTRFS_I(inode)->outstanding_extents++;
1732 spin_unlock(&BTRFS_I(inode)->lock);
1733 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001734
Josef Bacik6a3891c2015-03-16 17:38:52 -04001735 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001736 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001737 return;
1738
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1740 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001741 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001742 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001743 if (*bits & EXTENT_DEFRAG)
1744 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001745 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 &BTRFS_I(inode)->runtime_flags))
1747 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001748 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001749 }
Chris Mason291d6732008-01-29 15:55:23 -05001750}
1751
Chris Masond352ac62008-09-29 15:18:18 -04001752/*
1753 * extent_io.c clear_bit_hook, see set_bit_hook for why
1754 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001755static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001756 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001757 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001758{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001760 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001761 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001762
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001763 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001764 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001765 inode->defrag_bytes -= len;
1766 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001767
Chris Mason75eff682008-12-15 15:54:40 -05001768 /*
1769 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001770 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001771 * bit, which is only set or cleared with irqs on
1772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001773 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001774 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001775 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001776
Josef Bacik9e0baf62011-07-15 15:16:44 +00001777 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001778 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001779 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001780 spin_lock(&inode->lock);
1781 inode->outstanding_extents -= num_extents;
1782 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001783 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001784
Josef Bacikb6d08f02013-09-27 14:57:43 -04001785 /*
1786 * We don't reserve metadata space for space cache inodes so we
1787 * don't need to call dellalloc_release_metadata if there is an
1788 * error.
1789 */
1790 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001791 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001792 btrfs_delalloc_release_metadata(inode, len);
1793
Josef Bacik6a3891c2015-03-16 17:38:52 -04001794 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001795 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001796 return;
1797
Josef Bacik0cb59c92010-07-02 12:14:14 -04001798 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001799 && do_list && !(state->state & EXTENT_NORESERVE)
1800 && (*bits & (EXTENT_DO_ACCOUNTING |
1801 EXTENT_CLEAR_DATA_RESV)))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001802 btrfs_free_reserved_data_space_noquota(
1803 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001804 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001805
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001806 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1807 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001808 spin_lock(&inode->lock);
1809 inode->delalloc_bytes -= len;
1810 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001811 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001812 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001813 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001814 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001815 }
Chris Mason291d6732008-01-29 15:55:23 -05001816}
1817
Chris Masond352ac62008-09-29 15:18:18 -04001818/*
1819 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1820 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001821 *
1822 * return 1 if page cannot be merged to bio
1823 * return 0 if page can be merged to bio
1824 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001825 */
Mike Christie81a75f672016-06-05 14:31:54 -05001826int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001827 size_t size, struct bio *bio,
1828 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001829{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001830 struct inode *inode = page->mapping->host;
1831 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001832 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001833 u64 length = 0;
1834 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001835 int ret;
1836
Chris Mason771ed682008-11-06 22:02:51 -05001837 if (bio_flags & EXTENT_BIO_COMPRESSED)
1838 return 0;
1839
Kent Overstreet4f024f32013-10-11 15:44:27 -07001840 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001841 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1843 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001844 if (ret < 0)
1845 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001846 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001847 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001848 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001849}
1850
Chris Masond352ac62008-09-29 15:18:18 -04001851/*
1852 * in order to insert checksums into the metadata in large chunks,
1853 * we wait until bio submission time. All the pages in the bio are
1854 * checksummed and sums are attached onto the ordered extent record.
1855 *
1856 * At IO completion time the cums attached on the ordered extent record
1857 * are inserted into the btree
1858 */
Mike Christie81a75f672016-06-05 14:31:54 -05001859static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1860 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001861 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001862{
Chris Mason065631f2008-02-20 12:07:25 -05001863 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001864
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001865 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001866 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001867 return 0;
1868}
Chris Masone0156402008-04-16 11:15:20 -04001869
Chris Mason4a69a412008-11-06 22:03:00 -05001870/*
1871 * in order to insert checksums into the metadata in large chunks,
1872 * we wait until bio submission time. All the pages in the bio are
1873 * checksummed and sums are attached onto the ordered extent record.
1874 *
1875 * At IO completion time the cums attached on the ordered extent record
1876 * are inserted into the btree
1877 */
Mike Christie81a75f672016-06-05 14:31:54 -05001878static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001879 int mirror_num, unsigned long bio_flags,
1880 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001881{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001882 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001883 int ret;
1884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001885 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001886 if (ret) {
1887 bio->bi_error = ret;
1888 bio_endio(bio);
1889 }
Stefan Behrens61891922012-11-05 18:51:52 +01001890 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001891}
1892
Chris Masond352ac62008-09-29 15:18:18 -04001893/*
Chris Masoncad321a2008-12-17 14:51:42 -05001894 * extent_io.c submission hook. This does the right thing for csum calculation
1895 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001896 */
Mike Christie81a75f672016-06-05 14:31:54 -05001897static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001898 int mirror_num, unsigned long bio_flags,
1899 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001900{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001902 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301903 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001904 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001905 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001906 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001907
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001908 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001909
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001910 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301911 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001912
Mike Christie37226b22016-06-05 14:31:52 -05001913 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001915 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001916 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001917
Chris Masond20f7042008-12-08 16:58:54 -05001918 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001919 ret = btrfs_submit_compressed_read(inode, bio,
1920 mirror_num,
1921 bio_flags);
1922 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001923 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001925 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001926 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001927 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001928 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001929 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001930 /* csum items have already been cloned */
1931 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1932 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001933 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001934 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1935 bio_flags, bio_offset,
1936 __btrfs_submit_bio_start,
1937 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001938 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001939 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001940 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001941 if (ret)
1942 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001943 }
1944
Chris Mason0b86a832008-03-24 15:01:56 -04001945mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001946 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001947
1948out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret < 0) {
1950 bio->bi_error = ret;
1951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001954}
Chris Mason6885f302008-02-20 16:11:05 -05001955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
1957 * given a list of ordered sums record them in the inode. This happens
1958 * at IO completion time based on sums calculated at bio submission time.
1959 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001960static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001961 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001962{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001963 struct btrfs_ordered_sum *sum;
1964
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001965 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001966 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001967 btrfs_csum_file_blocks(trans,
1968 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001969 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001970 }
1971 return 0;
1972}
1973
Josef Bacik2ac55d42010-02-03 19:33:23 +00001974int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001975 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001976{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001977 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001978 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001979 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001980}
1981
Chris Masond352ac62008-09-29 15:18:18 -04001982/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001983struct btrfs_writepage_fixup {
1984 struct page *page;
1985 struct btrfs_work work;
1986};
1987
Christoph Hellwigb2950862008-12-02 09:54:17 -05001988static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001989{
1990 struct btrfs_writepage_fixup *fixup;
1991 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001992 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001993 struct page *page;
1994 struct inode *inode;
1995 u64 page_start;
1996 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001997 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001998
1999 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2000 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002001again:
Chris Mason247e7432008-07-17 12:53:51 -04002002 lock_page(page);
2003 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2004 ClearPageChecked(page);
2005 goto out_page;
2006 }
2007
2008 inode = page->mapping->host;
2009 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002010 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002011
David Sterbaff13db42015-12-03 14:30:40 +01002012 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002013 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002014
2015 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002016 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002017 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002018
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002019 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002020 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002021 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002022 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2023 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002024 unlock_page(page);
2025 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002026 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002027 goto again;
2028 }
Chris Mason247e7432008-07-17 12:53:51 -04002029
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002030 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002031 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002032 if (ret) {
2033 mapping_set_error(page->mapping, ret);
2034 end_extent_writepage(page, ret, page_start, page_end);
2035 ClearPageChecked(page);
2036 goto out;
2037 }
2038
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002039 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2040 0);
Chris Mason247e7432008-07-17 12:53:51 -04002041 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002042 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002043out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002044 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2045 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002046out_page:
2047 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002048 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002049 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002050}
2051
2052/*
2053 * There are a few paths in the higher layers of the kernel that directly
2054 * set the page dirty bit without asking the filesystem if it is a
2055 * good idea. This causes problems because we want to make sure COW
2056 * properly happens and the data=ordered rules are followed.
2057 *
Chris Masonc8b97812008-10-29 14:49:59 -04002058 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002059 * hasn't been properly setup for IO. We kick off an async process
2060 * to fix it up. The async helper will wait for ordered extents, set
2061 * the delalloc bit and make it safe to write the page.
2062 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002064{
2065 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002066 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002067 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002068
Chris Mason8b62b722009-09-02 16:53:46 -04002069 /* this page is properly in the ordered list */
2070 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002071 return 0;
2072
2073 if (PageChecked(page))
2074 return -EAGAIN;
2075
2076 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2077 if (!fixup)
2078 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002079
Chris Mason247e7432008-07-17 12:53:51 -04002080 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002082 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2083 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002084 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002086 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002087}
2088
Yan Zhengd899e052008-10-30 14:25:28 -04002089static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2090 struct inode *inode, u64 file_pos,
2091 u64 disk_bytenr, u64 disk_num_bytes,
2092 u64 num_bytes, u64 ram_bytes,
2093 u8 compression, u8 encryption,
2094 u16 other_encoding, int extent_type)
2095{
2096 struct btrfs_root *root = BTRFS_I(inode)->root;
2097 struct btrfs_file_extent_item *fi;
2098 struct btrfs_path *path;
2099 struct extent_buffer *leaf;
2100 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002101 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002102 int ret;
2103
2104 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002105 if (!path)
2106 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002107
Chris Masona1ed8352009-09-11 12:27:37 -04002108 /*
2109 * we may be replacing one extent in the tree with another.
2110 * The new extent is pinned in the extent map, and we don't want
2111 * to drop it from the cache until it is completely in the btree.
2112 *
2113 * So, tell btrfs_drop_extents to leave this extent in the cache.
2114 * the caller is expected to unpin it and allow it to be merged
2115 * with the others.
2116 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002117 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2118 file_pos + num_bytes, NULL, 0,
2119 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002120 if (ret)
2121 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002122
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002123 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002124 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002125 ins.offset = file_pos;
2126 ins.type = BTRFS_EXTENT_DATA_KEY;
2127
2128 path->leave_spinning = 1;
2129 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2130 sizeof(*fi));
2131 if (ret)
2132 goto out;
2133 }
Yan Zhengd899e052008-10-30 14:25:28 -04002134 leaf = path->nodes[0];
2135 fi = btrfs_item_ptr(leaf, path->slots[0],
2136 struct btrfs_file_extent_item);
2137 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2138 btrfs_set_file_extent_type(leaf, fi, extent_type);
2139 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2140 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2141 btrfs_set_file_extent_offset(leaf, fi, 0);
2142 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2143 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2144 btrfs_set_file_extent_compression(leaf, fi, compression);
2145 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2146 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002147
Yan Zhengd899e052008-10-30 14:25:28 -04002148 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002149 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002150
2151 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002152
2153 ins.objectid = disk_bytenr;
2154 ins.offset = disk_num_bytes;
2155 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002156 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002157 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002158 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002159 * Release the reserved range from inode dirty range map, as it is
2160 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002161 */
2162 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002163out:
Yan Zhengd899e052008-10-30 14:25:28 -04002164 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002165
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002166 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002167}
2168
Liu Bo38c227d2013-01-29 03:18:40 +00002169/* snapshot-aware defrag */
2170struct sa_defrag_extent_backref {
2171 struct rb_node node;
2172 struct old_sa_defrag_extent *old;
2173 u64 root_id;
2174 u64 inum;
2175 u64 file_pos;
2176 u64 extent_offset;
2177 u64 num_bytes;
2178 u64 generation;
2179};
2180
2181struct old_sa_defrag_extent {
2182 struct list_head list;
2183 struct new_sa_defrag_extent *new;
2184
2185 u64 extent_offset;
2186 u64 bytenr;
2187 u64 offset;
2188 u64 len;
2189 int count;
2190};
2191
2192struct new_sa_defrag_extent {
2193 struct rb_root root;
2194 struct list_head head;
2195 struct btrfs_path *path;
2196 struct inode *inode;
2197 u64 file_pos;
2198 u64 len;
2199 u64 bytenr;
2200 u64 disk_len;
2201 u8 compress_type;
2202};
2203
2204static int backref_comp(struct sa_defrag_extent_backref *b1,
2205 struct sa_defrag_extent_backref *b2)
2206{
2207 if (b1->root_id < b2->root_id)
2208 return -1;
2209 else if (b1->root_id > b2->root_id)
2210 return 1;
2211
2212 if (b1->inum < b2->inum)
2213 return -1;
2214 else if (b1->inum > b2->inum)
2215 return 1;
2216
2217 if (b1->file_pos < b2->file_pos)
2218 return -1;
2219 else if (b1->file_pos > b2->file_pos)
2220 return 1;
2221
2222 /*
2223 * [------------------------------] ===> (a range of space)
2224 * |<--->| |<---->| =============> (fs/file tree A)
2225 * |<---------------------------->| ===> (fs/file tree B)
2226 *
2227 * A range of space can refer to two file extents in one tree while
2228 * refer to only one file extent in another tree.
2229 *
2230 * So we may process a disk offset more than one time(two extents in A)
2231 * and locate at the same extent(one extent in B), then insert two same
2232 * backrefs(both refer to the extent in B).
2233 */
2234 return 0;
2235}
2236
2237static void backref_insert(struct rb_root *root,
2238 struct sa_defrag_extent_backref *backref)
2239{
2240 struct rb_node **p = &root->rb_node;
2241 struct rb_node *parent = NULL;
2242 struct sa_defrag_extent_backref *entry;
2243 int ret;
2244
2245 while (*p) {
2246 parent = *p;
2247 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2248
2249 ret = backref_comp(backref, entry);
2250 if (ret < 0)
2251 p = &(*p)->rb_left;
2252 else
2253 p = &(*p)->rb_right;
2254 }
2255
2256 rb_link_node(&backref->node, parent, p);
2257 rb_insert_color(&backref->node, root);
2258}
2259
2260/*
2261 * Note the backref might has changed, and in this case we just return 0.
2262 */
2263static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2264 void *ctx)
2265{
2266 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002267 struct old_sa_defrag_extent *old = ctx;
2268 struct new_sa_defrag_extent *new = old->new;
2269 struct btrfs_path *path = new->path;
2270 struct btrfs_key key;
2271 struct btrfs_root *root;
2272 struct sa_defrag_extent_backref *backref;
2273 struct extent_buffer *leaf;
2274 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002276 int slot;
2277 int ret;
2278 u64 extent_offset;
2279 u64 num_bytes;
2280
2281 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002282 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002283 return 0;
2284
2285 key.objectid = root_id;
2286 key.type = BTRFS_ROOT_ITEM_KEY;
2287 key.offset = (u64)-1;
2288
Liu Bo38c227d2013-01-29 03:18:40 +00002289 root = btrfs_read_fs_root_no_name(fs_info, &key);
2290 if (IS_ERR(root)) {
2291 if (PTR_ERR(root) == -ENOENT)
2292 return 0;
2293 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002294 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002295 inum, offset, root_id);
2296 return PTR_ERR(root);
2297 }
2298
2299 key.objectid = inum;
2300 key.type = BTRFS_EXTENT_DATA_KEY;
2301 if (offset > (u64)-1 << 32)
2302 key.offset = 0;
2303 else
2304 key.offset = offset;
2305
2306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302307 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002308 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002309 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002310
2311 while (1) {
2312 cond_resched();
2313
2314 leaf = path->nodes[0];
2315 slot = path->slots[0];
2316
2317 if (slot >= btrfs_header_nritems(leaf)) {
2318 ret = btrfs_next_leaf(root, path);
2319 if (ret < 0) {
2320 goto out;
2321 } else if (ret > 0) {
2322 ret = 0;
2323 goto out;
2324 }
2325 continue;
2326 }
2327
2328 path->slots[0]++;
2329
2330 btrfs_item_key_to_cpu(leaf, &key, slot);
2331
2332 if (key.objectid > inum)
2333 goto out;
2334
2335 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2336 continue;
2337
2338 extent = btrfs_item_ptr(leaf, slot,
2339 struct btrfs_file_extent_item);
2340
2341 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2342 continue;
2343
Liu Boe68afa42013-07-01 22:13:26 +08002344 /*
2345 * 'offset' refers to the exact key.offset,
2346 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2347 * (key.offset - extent_offset).
2348 */
2349 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002350 continue;
2351
Liu Boe68afa42013-07-01 22:13:26 +08002352 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002353 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002354
Liu Bo38c227d2013-01-29 03:18:40 +00002355 if (extent_offset >= old->extent_offset + old->offset +
2356 old->len || extent_offset + num_bytes <=
2357 old->extent_offset + old->offset)
2358 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002359 break;
2360 }
2361
2362 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2363 if (!backref) {
2364 ret = -ENOENT;
2365 goto out;
2366 }
2367
2368 backref->root_id = root_id;
2369 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002370 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002371 backref->num_bytes = num_bytes;
2372 backref->extent_offset = extent_offset;
2373 backref->generation = btrfs_file_extent_generation(leaf, extent);
2374 backref->old = old;
2375 backref_insert(&new->root, backref);
2376 old->count++;
2377out:
2378 btrfs_release_path(path);
2379 WARN_ON(ret);
2380 return ret;
2381}
2382
2383static noinline bool record_extent_backrefs(struct btrfs_path *path,
2384 struct new_sa_defrag_extent *new)
2385{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002386 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002387 struct old_sa_defrag_extent *old, *tmp;
2388 int ret;
2389
2390 new->path = path;
2391
2392 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002393 ret = iterate_inodes_from_logical(old->bytenr +
2394 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002395 path, record_one_backref,
2396 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002397 if (ret < 0 && ret != -ENOENT)
2398 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002399
2400 /* no backref to be processed for this extent */
2401 if (!old->count) {
2402 list_del(&old->list);
2403 kfree(old);
2404 }
2405 }
2406
2407 if (list_empty(&new->head))
2408 return false;
2409
2410 return true;
2411}
2412
2413static int relink_is_mergable(struct extent_buffer *leaf,
2414 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002415 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002416{
Liu Bo116e0022013-08-02 16:30:40 +08002417 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002418 return 0;
2419
2420 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2421 return 0;
2422
Liu Bo116e0022013-08-02 16:30:40 +08002423 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2424 return 0;
2425
2426 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002427 btrfs_file_extent_other_encoding(leaf, fi))
2428 return 0;
2429
2430 return 1;
2431}
2432
2433/*
2434 * Note the backref might has changed, and in this case we just return 0.
2435 */
2436static noinline int relink_extent_backref(struct btrfs_path *path,
2437 struct sa_defrag_extent_backref *prev,
2438 struct sa_defrag_extent_backref *backref)
2439{
2440 struct btrfs_file_extent_item *extent;
2441 struct btrfs_file_extent_item *item;
2442 struct btrfs_ordered_extent *ordered;
2443 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002444 struct btrfs_root *root;
2445 struct btrfs_key key;
2446 struct extent_buffer *leaf;
2447 struct old_sa_defrag_extent *old = backref->old;
2448 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002449 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002450 struct inode *inode;
2451 struct extent_state *cached = NULL;
2452 int ret = 0;
2453 u64 start;
2454 u64 len;
2455 u64 lock_start;
2456 u64 lock_end;
2457 bool merge = false;
2458 int index;
2459
2460 if (prev && prev->root_id == backref->root_id &&
2461 prev->inum == backref->inum &&
2462 prev->file_pos + prev->num_bytes == backref->file_pos)
2463 merge = true;
2464
2465 /* step 1: get root */
2466 key.objectid = backref->root_id;
2467 key.type = BTRFS_ROOT_ITEM_KEY;
2468 key.offset = (u64)-1;
2469
Liu Bo38c227d2013-01-29 03:18:40 +00002470 index = srcu_read_lock(&fs_info->subvol_srcu);
2471
2472 root = btrfs_read_fs_root_no_name(fs_info, &key);
2473 if (IS_ERR(root)) {
2474 srcu_read_unlock(&fs_info->subvol_srcu, index);
2475 if (PTR_ERR(root) == -ENOENT)
2476 return 0;
2477 return PTR_ERR(root);
2478 }
Liu Bo38c227d2013-01-29 03:18:40 +00002479
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002480 if (btrfs_root_readonly(root)) {
2481 srcu_read_unlock(&fs_info->subvol_srcu, index);
2482 return 0;
2483 }
2484
Liu Bo38c227d2013-01-29 03:18:40 +00002485 /* step 2: get inode */
2486 key.objectid = backref->inum;
2487 key.type = BTRFS_INODE_ITEM_KEY;
2488 key.offset = 0;
2489
2490 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2491 if (IS_ERR(inode)) {
2492 srcu_read_unlock(&fs_info->subvol_srcu, index);
2493 return 0;
2494 }
2495
2496 srcu_read_unlock(&fs_info->subvol_srcu, index);
2497
2498 /* step 3: relink backref */
2499 lock_start = backref->file_pos;
2500 lock_end = backref->file_pos + backref->num_bytes - 1;
2501 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002502 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002503
2504 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2505 if (ordered) {
2506 btrfs_put_ordered_extent(ordered);
2507 goto out_unlock;
2508 }
2509
2510 trans = btrfs_join_transaction(root);
2511 if (IS_ERR(trans)) {
2512 ret = PTR_ERR(trans);
2513 goto out_unlock;
2514 }
2515
2516 key.objectid = backref->inum;
2517 key.type = BTRFS_EXTENT_DATA_KEY;
2518 key.offset = backref->file_pos;
2519
2520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2521 if (ret < 0) {
2522 goto out_free_path;
2523 } else if (ret > 0) {
2524 ret = 0;
2525 goto out_free_path;
2526 }
2527
2528 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2529 struct btrfs_file_extent_item);
2530
2531 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2532 backref->generation)
2533 goto out_free_path;
2534
2535 btrfs_release_path(path);
2536
2537 start = backref->file_pos;
2538 if (backref->extent_offset < old->extent_offset + old->offset)
2539 start += old->extent_offset + old->offset -
2540 backref->extent_offset;
2541
2542 len = min(backref->extent_offset + backref->num_bytes,
2543 old->extent_offset + old->offset + old->len);
2544 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2545
2546 ret = btrfs_drop_extents(trans, root, inode, start,
2547 start + len, 1);
2548 if (ret)
2549 goto out_free_path;
2550again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002551 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002552 key.type = BTRFS_EXTENT_DATA_KEY;
2553 key.offset = start;
2554
Liu Boa09a0a72013-03-11 09:20:58 +00002555 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002556 if (merge) {
2557 struct btrfs_file_extent_item *fi;
2558 u64 extent_len;
2559 struct btrfs_key found_key;
2560
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002561 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002562 if (ret < 0)
2563 goto out_free_path;
2564
2565 path->slots[0]--;
2566 leaf = path->nodes[0];
2567 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2568
2569 fi = btrfs_item_ptr(leaf, path->slots[0],
2570 struct btrfs_file_extent_item);
2571 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2572
Liu Bo116e0022013-08-02 16:30:40 +08002573 if (extent_len + found_key.offset == start &&
2574 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002575 btrfs_set_file_extent_num_bytes(leaf, fi,
2576 extent_len + len);
2577 btrfs_mark_buffer_dirty(leaf);
2578 inode_add_bytes(inode, len);
2579
2580 ret = 1;
2581 goto out_free_path;
2582 } else {
2583 merge = false;
2584 btrfs_release_path(path);
2585 goto again;
2586 }
2587 }
2588
2589 ret = btrfs_insert_empty_item(trans, root, path, &key,
2590 sizeof(*extent));
2591 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002592 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002593 goto out_free_path;
2594 }
2595
2596 leaf = path->nodes[0];
2597 item = btrfs_item_ptr(leaf, path->slots[0],
2598 struct btrfs_file_extent_item);
2599 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2600 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2601 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2602 btrfs_set_file_extent_num_bytes(leaf, item, len);
2603 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2604 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2605 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2606 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2607 btrfs_set_file_extent_encryption(leaf, item, 0);
2608 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2609
2610 btrfs_mark_buffer_dirty(leaf);
2611 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002612 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002613
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002614 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002615 new->disk_len, 0,
2616 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002617 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002618 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002619 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002620 goto out_free_path;
2621 }
2622
2623 ret = 1;
2624out_free_path:
2625 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002626 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002627 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002628out_unlock:
2629 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2630 &cached, GFP_NOFS);
2631 iput(inode);
2632 return ret;
2633}
2634
Liu Bo6f519562013-10-29 10:45:05 +08002635static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2636{
2637 struct old_sa_defrag_extent *old, *tmp;
2638
2639 if (!new)
2640 return;
2641
2642 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002643 kfree(old);
2644 }
2645 kfree(new);
2646}
2647
Liu Bo38c227d2013-01-29 03:18:40 +00002648static void relink_file_extents(struct new_sa_defrag_extent *new)
2649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002651 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002652 struct sa_defrag_extent_backref *backref;
2653 struct sa_defrag_extent_backref *prev = NULL;
2654 struct inode *inode;
2655 struct btrfs_root *root;
2656 struct rb_node *node;
2657 int ret;
2658
2659 inode = new->inode;
2660 root = BTRFS_I(inode)->root;
2661
2662 path = btrfs_alloc_path();
2663 if (!path)
2664 return;
2665
2666 if (!record_extent_backrefs(path, new)) {
2667 btrfs_free_path(path);
2668 goto out;
2669 }
2670 btrfs_release_path(path);
2671
2672 while (1) {
2673 node = rb_first(&new->root);
2674 if (!node)
2675 break;
2676 rb_erase(node, &new->root);
2677
2678 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2679
2680 ret = relink_extent_backref(path, prev, backref);
2681 WARN_ON(ret < 0);
2682
2683 kfree(prev);
2684
2685 if (ret == 1)
2686 prev = backref;
2687 else
2688 prev = NULL;
2689 cond_resched();
2690 }
2691 kfree(prev);
2692
2693 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002694out:
Liu Bo6f519562013-10-29 10:45:05 +08002695 free_sa_defrag_extent(new);
2696
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002697 atomic_dec(&fs_info->defrag_running);
2698 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002699}
2700
2701static struct new_sa_defrag_extent *
2702record_old_file_extents(struct inode *inode,
2703 struct btrfs_ordered_extent *ordered)
2704{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002705 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002706 struct btrfs_root *root = BTRFS_I(inode)->root;
2707 struct btrfs_path *path;
2708 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002709 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct new_sa_defrag_extent *new;
2711 int ret;
2712
2713 new = kmalloc(sizeof(*new), GFP_NOFS);
2714 if (!new)
2715 return NULL;
2716
2717 new->inode = inode;
2718 new->file_pos = ordered->file_offset;
2719 new->len = ordered->len;
2720 new->bytenr = ordered->start;
2721 new->disk_len = ordered->disk_len;
2722 new->compress_type = ordered->compress_type;
2723 new->root = RB_ROOT;
2724 INIT_LIST_HEAD(&new->head);
2725
2726 path = btrfs_alloc_path();
2727 if (!path)
2728 goto out_kfree;
2729
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002730 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002731 key.type = BTRFS_EXTENT_DATA_KEY;
2732 key.offset = new->file_pos;
2733
2734 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2735 if (ret < 0)
2736 goto out_free_path;
2737 if (ret > 0 && path->slots[0] > 0)
2738 path->slots[0]--;
2739
2740 /* find out all the old extents for the file range */
2741 while (1) {
2742 struct btrfs_file_extent_item *extent;
2743 struct extent_buffer *l;
2744 int slot;
2745 u64 num_bytes;
2746 u64 offset;
2747 u64 end;
2748 u64 disk_bytenr;
2749 u64 extent_offset;
2750
2751 l = path->nodes[0];
2752 slot = path->slots[0];
2753
2754 if (slot >= btrfs_header_nritems(l)) {
2755 ret = btrfs_next_leaf(root, path);
2756 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002757 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002758 else if (ret > 0)
2759 break;
2760 continue;
2761 }
2762
2763 btrfs_item_key_to_cpu(l, &key, slot);
2764
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002765 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002766 break;
2767 if (key.type != BTRFS_EXTENT_DATA_KEY)
2768 break;
2769 if (key.offset >= new->file_pos + new->len)
2770 break;
2771
2772 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2773
2774 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2775 if (key.offset + num_bytes < new->file_pos)
2776 goto next;
2777
2778 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2779 if (!disk_bytenr)
2780 goto next;
2781
2782 extent_offset = btrfs_file_extent_offset(l, extent);
2783
2784 old = kmalloc(sizeof(*old), GFP_NOFS);
2785 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002786 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002787
2788 offset = max(new->file_pos, key.offset);
2789 end = min(new->file_pos + new->len, key.offset + num_bytes);
2790
2791 old->bytenr = disk_bytenr;
2792 old->extent_offset = extent_offset;
2793 old->offset = offset - key.offset;
2794 old->len = end - offset;
2795 old->new = new;
2796 old->count = 0;
2797 list_add_tail(&old->list, &new->head);
2798next:
2799 path->slots[0]++;
2800 cond_resched();
2801 }
2802
2803 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002804 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002805
2806 return new;
2807
Liu Bo38c227d2013-01-29 03:18:40 +00002808out_free_path:
2809 btrfs_free_path(path);
2810out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002811 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002812 return NULL;
2813}
2814
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002815static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002816 u64 start, u64 len)
2817{
2818 struct btrfs_block_group_cache *cache;
2819
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002821 ASSERT(cache);
2822
2823 spin_lock(&cache->lock);
2824 cache->delalloc_bytes -= len;
2825 spin_unlock(&cache->lock);
2826
2827 btrfs_put_block_group(cache);
2828}
2829
Chris Masond352ac62008-09-29 15:18:18 -04002830/* as ordered data IO finishes, this gets called so we can finish
2831 * an ordered extent if the range of bytes in the file it covers are
2832 * fully written.
2833 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002834static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002835{
Josef Bacik5fd02042012-05-02 14:00:54 -04002836 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002837 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002838 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002839 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002841 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002842 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002843 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002844 int ret = 0;
2845 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002846 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002847 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002848
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002849 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002850
Josef Bacik5fd02042012-05-02 14:00:54 -04002851 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2852 ret = -EIO;
2853 goto out;
2854 }
2855
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002856 btrfs_free_io_failure_record(BTRFS_I(inode),
2857 ordered_extent->file_offset,
2858 ordered_extent->file_offset +
2859 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002860
Josef Bacik77cef2e2013-08-29 13:57:21 -04002861 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2862 truncated = true;
2863 logical_len = ordered_extent->truncated_len;
2864 /* Truncated the entire extent, don't bother adding */
2865 if (!logical_len)
2866 goto out;
2867 }
2868
Yan, Zhengc2167752009-11-12 09:34:21 +00002869 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002870 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002871
2872 /*
2873 * For mwrite(mmap + memset to write) case, we still reserve
2874 * space for NOCOW range.
2875 * As NOCOW won't cause a new delayed ref, just free the space
2876 */
2877 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2878 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002879 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2880 if (nolock)
2881 trans = btrfs_join_transaction_nolock(root);
2882 else
2883 trans = btrfs_join_transaction(root);
2884 if (IS_ERR(trans)) {
2885 ret = PTR_ERR(trans);
2886 trans = NULL;
2887 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002888 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002890 ret = btrfs_update_inode_fallback(trans, root, inode);
2891 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002892 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002893 goto out;
2894 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002895
Josef Bacik2ac55d42010-02-03 19:33:23 +00002896 lock_extent_bits(io_tree, ordered_extent->file_offset,
2897 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002898 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002899
Liu Bo38c227d2013-01-29 03:18:40 +00002900 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2901 ordered_extent->file_offset + ordered_extent->len - 1,
2902 EXTENT_DEFRAG, 1, cached_state);
2903 if (ret) {
2904 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002905 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002906 /* the inode is shared */
2907 new = record_old_file_extents(inode, ordered_extent);
2908
2909 clear_extent_bit(io_tree, ordered_extent->file_offset,
2910 ordered_extent->file_offset + ordered_extent->len - 1,
2911 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2912 }
2913
Josef Bacik0cb59c92010-07-02 12:14:14 -04002914 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002915 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002916 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002917 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918 if (IS_ERR(trans)) {
2919 ret = PTR_ERR(trans);
2920 trans = NULL;
2921 goto out_unlock;
2922 }
Chris Masona79b7d42014-05-22 16:18:52 -07002923
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002924 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002925
Chris Masonc8b97812008-10-29 14:49:59 -04002926 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002927 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002928 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002929 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002930 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002931 ordered_extent->file_offset,
2932 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002933 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002934 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002936 ret = insert_reserved_file_extent(trans, inode,
2937 ordered_extent->file_offset,
2938 ordered_extent->start,
2939 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002941 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002942 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002943 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002944 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002945 ordered_extent->start,
2946 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002947 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002948 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2949 ordered_extent->file_offset, ordered_extent->len,
2950 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002951 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002952 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002955
David Sterbadf9f6282017-02-10 19:35:37 +01002956 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002957
Josef Bacik6c760c02012-11-09 10:53:21 -05002958 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2959 ret = btrfs_update_inode_fallback(trans, root, inode);
2960 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002961 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002962 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002963 }
2964 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002965out_unlock:
2966 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2967 ordered_extent->file_offset +
2968 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002969out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002970 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002971 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2972 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002973 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002974 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975
Josef Bacik77cef2e2013-08-29 13:57:21 -04002976 if (ret || truncated) {
2977 u64 start, end;
2978
2979 if (truncated)
2980 start = ordered_extent->file_offset + logical_len;
2981 else
2982 start = ordered_extent->file_offset;
2983 end = ordered_extent->file_offset + ordered_extent->len - 1;
2984 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2985
2986 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002987 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002988
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002989 /*
2990 * If the ordered extent had an IOERR or something else went
2991 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002992 * back to the allocator. We only free the extent in the
2993 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002994 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 if ((ret || !logical_len) &&
2996 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002997 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002998 btrfs_free_reserved_extent(fs_info,
2999 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003000 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003001 }
3002
3003
Josef Bacik5fd02042012-05-02 14:00:54 -04003004 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003005 * This needs to be done to make sure anybody waiting knows we are done
3006 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003007 */
3008 btrfs_remove_ordered_extent(inode, ordered_extent);
3009
Liu Bo38c227d2013-01-29 03:18:40 +00003010 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003011 if (new) {
3012 if (ret) {
3013 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003015 } else {
3016 relink_file_extents(new);
3017 }
3018 }
Liu Bo38c227d2013-01-29 03:18:40 +00003019
Chris Masone6dcd2d2008-07-17 12:53:50 -04003020 /* once for us */
3021 btrfs_put_ordered_extent(ordered_extent);
3022 /* once for the tree */
3023 btrfs_put_ordered_extent(ordered_extent);
3024
Josef Bacik5fd02042012-05-02 14:00:54 -04003025 return ret;
3026}
3027
3028static void finish_ordered_fn(struct btrfs_work *work)
3029{
3030 struct btrfs_ordered_extent *ordered_extent;
3031 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3032 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003033}
3034
David Sterbac3988d62017-02-17 15:18:32 +01003035static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003036 struct extent_state *state, int uptodate)
3037{
Josef Bacik5fd02042012-05-02 14:00:54 -04003038 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003040 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003041 struct btrfs_workqueue *wq;
3042 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003043
liubo1abe9b82011-03-24 11:18:59 +00003044 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3045
Chris Mason8b62b722009-09-02 16:53:46 -04003046 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003047 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3048 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003049 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003050
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003051 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003053 func = btrfs_freespace_write_helper;
3054 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003056 func = btrfs_endio_write_helper;
3057 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003058
Liu Bo9e0af232014-08-15 23:36:53 +08003059 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3060 NULL);
3061 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003062}
3063
Miao Xiedc380ae2014-09-12 18:43:55 +08003064static int __readpage_endio_check(struct inode *inode,
3065 struct btrfs_io_bio *io_bio,
3066 int icsum, struct page *page,
3067 int pgoff, u64 start, size_t len)
3068{
3069 char *kaddr;
3070 u32 csum_expected;
3071 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003072
3073 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3074
3075 kaddr = kmap_atomic(page);
3076 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003077 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003078 if (csum != csum_expected)
3079 goto zeroit;
3080
3081 kunmap_atomic(kaddr);
3082 return 0;
3083zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003084 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003085 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003086 memset(kaddr + pgoff, 1, len);
3087 flush_dcache_page(page);
3088 kunmap_atomic(kaddr);
3089 if (csum_expected == 0)
3090 return 0;
3091 return -EIO;
3092}
3093
Chris Masond352ac62008-09-29 15:18:18 -04003094/*
Chris Masond352ac62008-09-29 15:18:18 -04003095 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003096 * if there's a match, we allow the bio to finish. If not, the code in
3097 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003098 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003099static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3100 u64 phy_offset, struct page *page,
3101 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003102{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003103 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003104 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003105 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003106 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003107
Chris Masond20f7042008-12-08 16:58:54 -05003108 if (PageChecked(page)) {
3109 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003110 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003111 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003112
3113 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003114 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003115
Yan Zheng17d217f2008-12-12 10:03:38 -05003116 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003117 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003118 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003119 return 0;
3120 }
3121
Miao Xiefacc8a222013-07-25 19:22:34 +08003122 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003123 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3124 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003125}
Chris Masonb888db22007-08-27 16:49:44 -04003126
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003127void btrfs_add_delayed_iput(struct inode *inode)
3128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003130 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003131
3132 if (atomic_add_unless(&inode->i_count, -1, 1))
3133 return;
3134
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003135 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003136 if (binode->delayed_iput_count == 0) {
3137 ASSERT(list_empty(&binode->delayed_iput));
3138 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3139 } else {
3140 binode->delayed_iput_count++;
3141 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003142 spin_unlock(&fs_info->delayed_iput_lock);
3143}
3144
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003145void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003146{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003147
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003148 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003149 while (!list_empty(&fs_info->delayed_iputs)) {
3150 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003151
David Sterba8089fe62015-11-19 14:15:51 +01003152 inode = list_first_entry(&fs_info->delayed_iputs,
3153 struct btrfs_inode, delayed_iput);
3154 if (inode->delayed_iput_count) {
3155 inode->delayed_iput_count--;
3156 list_move_tail(&inode->delayed_iput,
3157 &fs_info->delayed_iputs);
3158 } else {
3159 list_del_init(&inode->delayed_iput);
3160 }
3161 spin_unlock(&fs_info->delayed_iput_lock);
3162 iput(&inode->vfs_inode);
3163 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003164 }
David Sterba8089fe62015-11-19 14:15:51 +01003165 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003166}
3167
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003169 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 * files in the subvolume, it removes orphan item and frees block_rsv
3171 * structure.
3172 */
3173void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root)
3175{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003177 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003178 int ret;
3179
Josef Bacik8a35d952012-05-23 14:26:42 -04003180 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3182 return;
3183
Josef Bacik90290e12011-12-02 15:44:12 -05003184 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003185 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003186 spin_unlock(&root->orphan_lock);
3187 return;
3188 }
3189
3190 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3191 spin_unlock(&root->orphan_lock);
3192 return;
3193 }
3194
3195 block_rsv = root->orphan_block_rsv;
3196 root->orphan_block_rsv = NULL;
3197 spin_unlock(&root->orphan_lock);
3198
Miao Xie27cdeb72014-04-02 19:51:05 +08003199 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003200 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003202 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003203 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003204 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003205 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003206 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3207 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 }
3209
Josef Bacik90290e12011-12-02 15:44:12 -05003210 if (block_rsv) {
3211 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003212 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 }
3214}
3215
3216/*
Josef Bacik7b128762008-07-24 12:17:14 -04003217 * This creates an orphan entry for the given inode in case something goes
3218 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 *
3220 * NOTE: caller of this function should reserve 5 units of metadata for
3221 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003222 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003223int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3224 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003225{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3227 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 struct btrfs_block_rsv *block_rsv = NULL;
3229 int reserve = 0;
3230 int insert = 0;
3231 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003232
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003234 block_rsv = btrfs_alloc_block_rsv(fs_info,
3235 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003236 if (!block_rsv)
3237 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003238 }
3239
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 spin_lock(&root->orphan_lock);
3241 if (!root->orphan_block_rsv) {
3242 root->orphan_block_rsv = block_rsv;
3243 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003244 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 block_rsv = NULL;
3246 }
Josef Bacik7b128762008-07-24 12:17:14 -04003247
Josef Bacik8a35d952012-05-23 14:26:42 -04003248 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003249 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250#if 0
3251 /*
3252 * For proper ENOSPC handling, we should do orphan
3253 * cleanup when mounting. But this introduces backward
3254 * compatibility issue.
3255 */
3256 if (!xchg(&root->orphan_item_inserted, 1))
3257 insert = 2;
3258 else
3259 insert = 1;
3260#endif
3261 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003262 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003263 }
Josef Bacik7b128762008-07-24 12:17:14 -04003264
Josef Bacik72ac3c02012-05-23 14:13:11 -04003265 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003266 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003269
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 /* grab metadata reservation from transaction handle */
3271 if (reserve) {
3272 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003273 ASSERT(!ret);
3274 if (ret) {
3275 atomic_dec(&root->orphan_inodes);
3276 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003277 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003278 if (insert)
3279 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003280 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003281 return ret;
3282 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 }
3284
3285 /* insert an orphan item to track this unlinked/truncated file */
3286 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003287 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003288 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003289 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003290 if (reserve) {
3291 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003292 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003293 btrfs_orphan_release_metadata(inode);
3294 }
3295 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003296 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003297 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003298 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003299 return ret;
3300 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003301 }
3302 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 }
3304
3305 /* insert an orphan item to track subvolume contains orphan files */
3306 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003309 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003310 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003311 return ret;
3312 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 }
3314 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003315}
3316
3317/*
3318 * We have done the truncate/delete so we can go ahead and remove the orphan
3319 * item for this particular inode.
3320 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003321static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003322 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003323{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003324 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 int delete_item = 0;
3326 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 int ret = 0;
3328
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003330 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003331 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003332 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
Josef Bacik72ac3c02012-05-23 14:13:11 -04003334 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003335 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 spin_unlock(&root->orphan_lock);
3338
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003339 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003340 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003341 if (trans)
3342 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003343 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003344 }
Josef Bacik7b128762008-07-24 12:17:14 -04003345
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003346 if (release_rsv)
3347 btrfs_orphan_release_metadata(inode);
3348
Josef Bacik4ef31a42013-08-13 14:10:08 -04003349 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003350}
3351
3352/*
3353 * this cleans up any orphans that may be left on the list from the last use
3354 * of this root.
3355 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003356int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003358 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003359 struct btrfs_path *path;
3360 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003361 struct btrfs_key key, found_key;
3362 struct btrfs_trans_handle *trans;
3363 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3366
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003368 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003369
3370 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003371 if (!path) {
3372 ret = -ENOMEM;
3373 goto out;
3374 }
David Sterbae4058b52015-11-27 16:31:35 +01003375 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003376
3377 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003378 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003379 key.offset = (u64)-1;
3380
Josef Bacik7b128762008-07-24 12:17:14 -04003381 while (1) {
3382 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383 if (ret < 0)
3384 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003385
3386 /*
3387 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003388 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003389 * find the key and see if we have stuff that matches
3390 */
3391 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003392 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003393 if (path->slots[0] == 0)
3394 break;
3395 path->slots[0]--;
3396 }
3397
3398 /* pull out the item */
3399 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003400 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3401
3402 /* make sure the item matches what we want */
3403 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3404 break;
David Sterba962a2982014-06-04 18:41:45 +02003405 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003406 break;
3407
3408 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003409 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003410
3411 /*
3412 * this is where we are basically btrfs_lookup, without the
3413 * crossing root thing. we store the inode number in the
3414 * offset of the orphan item.
3415 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003416
3417 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003418 btrfs_err(fs_info,
3419 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003420 ret = -EINVAL;
3421 goto out;
3422 }
3423
3424 last_objectid = found_key.offset;
3425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003426 found_key.objectid = found_key.offset;
3427 found_key.type = BTRFS_INODE_ITEM_KEY;
3428 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003429 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303430 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003431 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003432 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003435 struct btrfs_root *dead_root;
3436 struct btrfs_fs_info *fs_info = root->fs_info;
3437 int is_dead_root = 0;
3438
3439 /*
3440 * this is an orphan in the tree root. Currently these
3441 * could come from 2 sources:
3442 * a) a snapshot deletion in progress
3443 * b) a free space cache inode
3444 * We need to distinguish those two, as the snapshot
3445 * orphan must not get deleted.
3446 * find_dead_roots already ran before us, so if this
3447 * is a snapshot deletion, we should find the root
3448 * in the dead_roots list
3449 */
3450 spin_lock(&fs_info->trans_lock);
3451 list_for_each_entry(dead_root, &fs_info->dead_roots,
3452 root_list) {
3453 if (dead_root->root_key.objectid ==
3454 found_key.objectid) {
3455 is_dead_root = 1;
3456 break;
3457 }
3458 }
3459 spin_unlock(&fs_info->trans_lock);
3460 if (is_dead_root) {
3461 /* prevent this orphan from being found again */
3462 key.offset = found_key.objectid - 1;
3463 continue;
3464 }
3465 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003466 /*
3467 * Inode is already gone but the orphan item is still there,
3468 * kill the orphan item.
3469 */
Filipe Manana67710892016-06-06 11:51:25 +01003470 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003471 trans = btrfs_start_transaction(root, 1);
3472 if (IS_ERR(trans)) {
3473 ret = PTR_ERR(trans);
3474 goto out;
3475 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 btrfs_debug(fs_info, "auto deleting %Lu",
3477 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003478 ret = btrfs_del_orphan_item(trans, root,
3479 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003480 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003481 if (ret)
3482 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003483 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 }
Josef Bacik7b128762008-07-24 12:17:14 -04003485
Josef Bacik7b128762008-07-24 12:17:14 -04003486 /*
3487 * add this inode to the orphan list so btrfs_orphan_del does
3488 * the proper thing when we hit it
3489 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003490 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3491 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003492 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003493
Josef Bacik7b128762008-07-24 12:17:14 -04003494 /* if we have links, this was a truncate, lets do that */
3495 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303496 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003497 iput(inode);
3498 continue;
3499 }
Josef Bacik7b128762008-07-24 12:17:14 -04003500 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003501
3502 /* 1 for the orphan item deletion. */
3503 trans = btrfs_start_transaction(root, 1);
3504 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003505 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003506 ret = PTR_ERR(trans);
3507 goto out;
3508 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003509 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003510 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003511 if (ret) {
3512 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003513 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003514 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003515
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003516 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003517 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003518 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003519 } else {
3520 nr_unlink++;
3521 }
3522
3523 /* this will do delete_inode and everything for us */
3524 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003525 if (ret)
3526 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003527 }
Miao Xie3254c872011-11-10 20:45:05 -05003528 /* release the path since we're done with it */
3529 btrfs_release_path(path);
3530
Yan, Zhengd68fc572010-05-16 10:49:58 -04003531 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3532
3533 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003534 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003535 (u64)-1);
3536
Miao Xie27cdeb72014-04-02 19:51:05 +08003537 if (root->orphan_block_rsv ||
3538 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003539 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003540 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003541 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003542 }
Josef Bacik7b128762008-07-24 12:17:14 -04003543
3544 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003545 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003546 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003548
3549out:
3550 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003551 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003552 btrfs_free_path(path);
3553 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003554}
3555
Chris Masond352ac62008-09-29 15:18:18 -04003556/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003557 * very simple check to peek ahead in the leaf looking for xattrs. If we
3558 * don't find any xattrs, we know there can't be any acls.
3559 *
3560 * slot is the slot the inode is in, objectid is the objectid of the inode
3561 */
3562static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003563 int slot, u64 objectid,
3564 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003565{
3566 u32 nritems = btrfs_header_nritems(leaf);
3567 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003568 static u64 xattr_access = 0;
3569 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003570 int scanned = 0;
3571
Josef Bacikf23b5a52013-06-19 10:16:26 -04003572 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003573 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3574 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3575 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3576 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003577 }
3578
Chris Mason46a53cc2009-04-27 11:47:50 -04003579 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003580 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003581 while (slot < nritems) {
3582 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3583
3584 /* we found a different objectid, there must not be acls */
3585 if (found_key.objectid != objectid)
3586 return 0;
3587
3588 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003589 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003590 if (*first_xattr_slot == -1)
3591 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003592 if (found_key.offset == xattr_access ||
3593 found_key.offset == xattr_default)
3594 return 1;
3595 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003596
3597 /*
3598 * we found a key greater than an xattr key, there can't
3599 * be any acls later on
3600 */
3601 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3602 return 0;
3603
3604 slot++;
3605 scanned++;
3606
3607 /*
3608 * it goes inode, inode backrefs, xattrs, extents,
3609 * so if there are a ton of hard links to an inode there can
3610 * be a lot of backrefs. Don't waste time searching too hard,
3611 * this is just an optimization
3612 */
3613 if (scanned >= 8)
3614 break;
3615 }
3616 /* we hit the end of the leaf before we found an xattr or
3617 * something larger than an xattr. We have to assume the inode
3618 * has acls
3619 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003620 if (*first_xattr_slot == -1)
3621 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003622 return 1;
3623}
3624
3625/*
Chris Masond352ac62008-09-29 15:18:18 -04003626 * read an inode from the btree into the in-memory inode
3627 */
Filipe Manana67710892016-06-06 11:51:25 +01003628static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003629{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003630 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003631 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003632 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003633 struct btrfs_inode_item *inode_item;
3634 struct btrfs_root *root = BTRFS_I(inode)->root;
3635 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003636 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003637 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003638 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003639 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003640 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003641 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003642
3643 ret = btrfs_fill_inode(inode, &rdev);
3644 if (!ret)
3645 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003646
3647 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003648 if (!path) {
3649 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003650 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003651 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003652
Chris Mason39279cc2007-06-12 06:35:45 -04003653 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003654
Chris Mason39279cc2007-06-12 06:35:45 -04003655 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003656 if (ret) {
3657 if (ret > 0)
3658 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003659 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003660 }
Chris Mason39279cc2007-06-12 06:35:45 -04003661
Chris Mason5f39d392007-10-15 16:14:19 -04003662 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003663
3664 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003665 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003666
Chris Mason5f39d392007-10-15 16:14:19 -04003667 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3668 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003669 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003670 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003671 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3672 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003673 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003674
David Sterbaa937b972014-12-12 17:39:12 +01003675 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3676 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003677
David Sterbaa937b972014-12-12 17:39:12 +01003678 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3679 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003680
David Sterbaa937b972014-12-12 17:39:12 +01003681 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3682 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003683
chandan r9cc97d62012-07-04 12:48:07 +05303684 BTRFS_I(inode)->i_otime.tv_sec =
3685 btrfs_timespec_sec(leaf, &inode_item->otime);
3686 BTRFS_I(inode)->i_otime.tv_nsec =
3687 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003688
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003689 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003690 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003691 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3692
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003693 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003694 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003695 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003696 rdev = btrfs_inode_rdev(leaf, inode_item);
3697
Josef Bacikaec74772008-07-24 12:12:38 -04003698 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003699 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003700
3701cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003702 /*
3703 * If we were modified in the current generation and evicted from memory
3704 * and then re-read we need to do a full sync since we don't have any
3705 * idea about which extents were modified before we were evicted from
3706 * cache.
3707 *
3708 * This is required for both inode re-read from disk and delayed inode
3709 * in delayed_nodes_tree.
3710 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003711 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003712 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3713 &BTRFS_I(inode)->runtime_flags);
3714
Filipe Mananabde6c242015-07-24 00:00:19 +01003715 /*
3716 * We don't persist the id of the transaction where an unlink operation
3717 * against the inode was last made. So here we assume the inode might
3718 * have been evicted, and therefore the exact value of last_unlink_trans
3719 * lost, and set it to last_trans to avoid metadata inconsistencies
3720 * between the inode and its parent if the inode is fsync'ed and the log
3721 * replayed. For example, in the scenario:
3722 *
3723 * touch mydir/foo
3724 * ln mydir/foo mydir/bar
3725 * sync
3726 * unlink mydir/bar
3727 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3728 * xfs_io -c fsync mydir/foo
3729 * <power failure>
3730 * mount fs, triggers fsync log replay
3731 *
3732 * We must make sure that when we fsync our inode foo we also log its
3733 * parent inode, otherwise after log replay the parent still has the
3734 * dentry with the "bar" name but our inode foo has a link count of 1
3735 * and doesn't have an inode ref with the name "bar" anymore.
3736 *
3737 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003738 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003739 * transaction commits on fsync if our inode is a directory, or if our
3740 * inode is not a directory, logging its parent unnecessarily.
3741 */
3742 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3743
Miao Xie67de1172013-12-26 13:07:06 +08003744 path->slots[0]++;
3745 if (inode->i_nlink != 1 ||
3746 path->slots[0] >= btrfs_header_nritems(leaf))
3747 goto cache_acl;
3748
3749 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003750 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003751 goto cache_acl;
3752
3753 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3754 if (location.type == BTRFS_INODE_REF_KEY) {
3755 struct btrfs_inode_ref *ref;
3756
3757 ref = (struct btrfs_inode_ref *)ptr;
3758 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3759 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3760 struct btrfs_inode_extref *extref;
3761
3762 extref = (struct btrfs_inode_extref *)ptr;
3763 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3764 extref);
3765 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003766cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003767 /*
3768 * try to precache a NULL acl entry for files that don't have
3769 * any xattrs or acls
3770 */
Li Zefan33345d012011-04-20 10:31:50 +08003771 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003772 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003773 if (first_xattr_slot != -1) {
3774 path->slots[0] = first_xattr_slot;
3775 ret = btrfs_load_inode_props(inode, path);
3776 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003777 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003778 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003779 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003780 root->root_key.objectid, ret);
3781 }
3782 btrfs_free_path(path);
3783
Al Viro72c04902009-06-24 16:58:48 -04003784 if (!maybe_acls)
3785 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003786
Chris Mason39279cc2007-06-12 06:35:45 -04003787 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003788 case S_IFREG:
3789 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003790 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003791 inode->i_fop = &btrfs_file_operations;
3792 inode->i_op = &btrfs_file_inode_operations;
3793 break;
3794 case S_IFDIR:
3795 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003796 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003797 break;
3798 case S_IFLNK:
3799 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003800 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003801 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3802 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003803 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003804 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003805 init_special_inode(inode, inode->i_mode, rdev);
3806 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003807 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003808
3809 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003810 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003811
3812make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003813 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003814 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003815 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003816}
3817
Chris Masond352ac62008-09-29 15:18:18 -04003818/*
3819 * given a leaf and an inode, copy the inode fields into the leaf
3820 */
Chris Masone02119d2008-09-05 16:13:11 -04003821static void fill_inode_item(struct btrfs_trans_handle *trans,
3822 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003824 struct inode *inode)
3825{
Liu Bo51fab692012-12-27 09:01:21 +00003826 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003827
Liu Bo51fab692012-12-27 09:01:21 +00003828 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003829
Liu Bo51fab692012-12-27 09:01:21 +00003830 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3831 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3832 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3833 &token);
3834 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3835 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003836
David Sterbaa937b972014-12-12 17:39:12 +01003837 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003838 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003839 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003840 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003841
David Sterbaa937b972014-12-12 17:39:12 +01003842 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003843 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003844 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003845 inode->i_mtime.tv_nsec, &token);
3846
David Sterbaa937b972014-12-12 17:39:12 +01003847 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003848 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003849 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003850 inode->i_ctime.tv_nsec, &token);
3851
chandan r9cc97d62012-07-04 12:48:07 +05303852 btrfs_set_token_timespec_sec(leaf, &item->otime,
3853 BTRFS_I(inode)->i_otime.tv_sec, &token);
3854 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3855 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3856
Liu Bo51fab692012-12-27 09:01:21 +00003857 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3858 &token);
3859 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3860 &token);
3861 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3862 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3863 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3864 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3865 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003866}
3867
Chris Masond352ac62008-09-29 15:18:18 -04003868/*
3869 * copy everything in the in-memory inode into the btree.
3870 */
Chris Mason21151332011-11-10 20:39:08 -05003871static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003872 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003873{
3874 struct btrfs_inode_item *inode_item;
3875 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003876 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 int ret;
3878
3879 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003880 if (!path)
3881 return -ENOMEM;
3882
Chris Masonb9473432009-03-13 11:00:37 -04003883 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003884 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3885 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003886 if (ret) {
3887 if (ret > 0)
3888 ret = -ENOENT;
3889 goto failed;
3890 }
3891
Chris Mason5f39d392007-10-15 16:14:19 -04003892 leaf = path->nodes[0];
3893 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003894 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003895
Chris Masone02119d2008-09-05 16:13:11 -04003896 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003897 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003898 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003899 ret = 0;
3900failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003901 btrfs_free_path(path);
3902 return ret;
3903}
3904
Chris Masond352ac62008-09-29 15:18:18 -04003905/*
Chris Mason21151332011-11-10 20:39:08 -05003906 * copy everything in the in-memory inode into the btree.
3907 */
3908noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3909 struct btrfs_root *root, struct inode *inode)
3910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003912 int ret;
3913
3914 /*
3915 * If the inode is a free space inode, we can deadlock during commit
3916 * if we put it into the delayed code.
3917 *
3918 * The data relocation inode should also be directly updated
3919 * without delay
3920 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003921 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003922 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003923 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003924 btrfs_update_root_times(trans, root);
3925
Chris Mason21151332011-11-10 20:39:08 -05003926 ret = btrfs_delayed_update_inode(trans, root, inode);
3927 if (!ret)
3928 btrfs_set_inode_last_trans(trans, inode);
3929 return ret;
3930 }
3931
3932 return btrfs_update_inode_item(trans, root, inode);
3933}
3934
Josef Bacikbe6aef62012-10-22 15:43:12 -04003935noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3936 struct btrfs_root *root,
3937 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003938{
3939 int ret;
3940
3941 ret = btrfs_update_inode(trans, root, inode);
3942 if (ret == -ENOSPC)
3943 return btrfs_update_inode_item(trans, root, inode);
3944 return ret;
3945}
3946
3947/*
Chris Masond352ac62008-09-29 15:18:18 -04003948 * unlink helper that gets used here in inode.c and in the tree logging
3949 * recovery code. It remove a link in a directory with a given name, and
3950 * also drops the back refs in the inode to the directory
3951 */
Al Viro92986792011-03-04 17:14:37 +00003952static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3953 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003954 struct btrfs_inode *dir,
3955 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003956 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003958 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003959 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003960 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003961 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003962 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003964 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003965 u64 ino = btrfs_ino(inode);
3966 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003967
3968 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003969 if (!path) {
3970 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003971 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003972 }
3973
Chris Masonb9473432009-03-13 11:00:37 -04003974 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003975 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003976 name, name_len, -1);
3977 if (IS_ERR(di)) {
3978 ret = PTR_ERR(di);
3979 goto err;
3980 }
3981 if (!di) {
3982 ret = -ENOENT;
3983 goto err;
3984 }
Chris Mason5f39d392007-10-15 16:14:19 -04003985 leaf = path->nodes[0];
3986 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003987 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003988 if (ret)
3989 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003990 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003991
Miao Xie67de1172013-12-26 13:07:06 +08003992 /*
3993 * If we don't have dir index, we have to get it by looking up
3994 * the inode ref, since we get the inode ref, remove it directly,
3995 * it is unnecessary to do delayed deletion.
3996 *
3997 * But if we have dir index, needn't search inode ref to get it.
3998 * Since the inode ref is close to the inode item, it is better
3999 * that we delay to delete it, and just do this deletion when
4000 * we update the inode item.
4001 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004002 if (inode->dir_index) {
4003 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004004 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004005 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004006 goto skip_backref;
4007 }
4008 }
4009
Li Zefan33345d012011-04-20 10:31:50 +08004010 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4011 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004012 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004013 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004014 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004015 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004016 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004017 goto err;
4018 }
Miao Xie67de1172013-12-26 13:07:06 +08004019skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004020 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004021 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004022 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004023 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004024 }
Chris Mason39279cc2007-06-12 06:35:45 -04004025
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004026 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4027 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004028 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004029 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004030 goto err;
4031 }
Chris Masone02119d2008-09-05 16:13:11 -04004032
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004033 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4034 index);
Chris Mason6418c962010-10-30 07:34:24 -04004035 if (ret == -ENOENT)
4036 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004037 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004038 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004039err:
4040 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004041 if (ret)
4042 goto out;
4043
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004044 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004045 inode_inc_iversion(&inode->vfs_inode);
4046 inode_inc_iversion(&dir->vfs_inode);
4047 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4048 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4049 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004050out:
Chris Mason39279cc2007-06-12 06:35:45 -04004051 return ret;
4052}
4053
Al Viro92986792011-03-04 17:14:37 +00004054int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4055 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004056 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004057 const char *name, int name_len)
4058{
4059 int ret;
4060 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4061 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004062 drop_nlink(&inode->vfs_inode);
4063 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004064 }
4065 return ret;
4066}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067
4068/*
4069 * helper to start transaction for unlink and rmdir.
4070 *
Josef Bacikd52be812013-05-29 14:54:47 -04004071 * unlink and rmdir are special in btrfs, they do not always free space, so
4072 * if we cannot make our reservations the normal way try and see if there is
4073 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4074 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004075 */
Josef Bacikd52be812013-05-29 14:54:47 -04004076static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004077{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004078 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004079
Josef Bacike70bea52011-10-11 14:18:24 -04004080 /*
4081 * 1 for the possible orphan item
4082 * 1 for the dir item
4083 * 1 for the dir index
4084 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004085 * 1 for the inode
4086 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004087 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004088}
4089
Chris Mason39279cc2007-06-12 06:35:45 -04004090static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4091{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004092 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004094 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 int ret;
4096
Josef Bacikd52be812013-05-29 14:54:47 -04004097 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004098 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004099 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004100
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004101 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4102 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004103
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004104 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4105 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4106 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004107 if (ret)
4108 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004109
Yan, Zhenga22285a2010-05-16 10:48:46 -04004110 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004111 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004112 if (ret)
4113 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004114 }
Josef Bacik7b128762008-07-24 12:17:14 -04004115
Tsutomu Itohb5324022011-07-19 07:27:20 +00004116out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004117 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004118 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004119 return ret;
4120}
4121
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4123 struct btrfs_root *root,
4124 struct inode *dir, u64 objectid,
4125 const char *name, int name_len)
4126{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004127 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 struct btrfs_path *path;
4129 struct extent_buffer *leaf;
4130 struct btrfs_dir_item *di;
4131 struct btrfs_key key;
4132 u64 index;
4133 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004134 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135
4136 path = btrfs_alloc_path();
4137 if (!path)
4138 return -ENOMEM;
4139
Li Zefan33345d012011-04-20 10:31:50 +08004140 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004141 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 if (IS_ERR_OR_NULL(di)) {
4143 if (!di)
4144 ret = -ENOENT;
4145 else
4146 ret = PTR_ERR(di);
4147 goto out;
4148 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149
4150 leaf = path->nodes[0];
4151 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4152 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4153 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004155 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004156 goto out;
4157 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004158 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4161 root->root_key.objectid, dir_ino,
4162 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004165 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166 goto out;
4167 }
Li Zefan33345d012011-04-20 10:31:50 +08004168 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170 if (IS_ERR_OR_NULL(di)) {
4171 if (!di)
4172 ret = -ENOENT;
4173 else
4174 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004175 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 goto out;
4177 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178
4179 leaf = path->nodes[0];
4180 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004181 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 index = key.offset;
4183 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004184 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004185
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004186 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004187 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004188 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004189 goto out;
4190 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004191
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004192 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004193 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004194 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004195 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004196 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004198out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004199 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004200 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201}
4202
Chris Mason39279cc2007-06-12 06:35:45 -04004203static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4204{
David Howells2b0143b2015-03-17 22:25:59 +00004205 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004206 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004207 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004208 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004209 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004210
David Sterbab3ae2442012-09-13 16:04:34 -06004211 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004212 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004213 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004214 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004215
Josef Bacikd52be812013-05-29 14:54:47 -04004216 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004217 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004218 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004219
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004220 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221 err = btrfs_unlink_subvol(trans, root, dir,
4222 BTRFS_I(inode)->location.objectid,
4223 dentry->d_name.name,
4224 dentry->d_name.len);
4225 goto out;
4226 }
4227
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004228 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004229 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004230 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004231
Filipe Manana44f714d2016-06-06 16:11:13 +01004232 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4233
Chris Mason39279cc2007-06-12 06:35:45 -04004234 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004235 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4236 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4237 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004238 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004239 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004240 /*
4241 * Propagate the last_unlink_trans value of the deleted dir to
4242 * its parent directory. This is to prevent an unrecoverable
4243 * log tree in the case we do something like this:
4244 * 1) create dir foo
4245 * 2) create snapshot under dir foo
4246 * 3) delete the snapshot
4247 * 4) rmdir foo
4248 * 5) mkdir foo
4249 * 6) fsync foo or some file inside foo
4250 */
4251 if (last_unlink_trans >= trans->transid)
4252 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4253 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004254out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004255 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004256 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004257
Chris Mason39279cc2007-06-12 06:35:45 -04004258 return err;
4259}
4260
Chris Mason28f75a02015-02-04 06:59:29 -08004261static int truncate_space_check(struct btrfs_trans_handle *trans,
4262 struct btrfs_root *root,
4263 u64 bytes_deleted)
4264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004265 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004266 int ret;
4267
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004268 /*
4269 * This is only used to apply pressure to the enospc system, we don't
4270 * intend to use this reservation at all.
4271 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004272 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004273 bytes_deleted *= fs_info->nodesize;
4274 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004275 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004276 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004277 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004278 trans->transid,
4279 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004280 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004281 }
Chris Mason28f75a02015-02-04 06:59:29 -08004282 return ret;
4283
4284}
4285
Filipe Manana0305cd52015-10-16 12:34:25 +01004286static int truncate_inline_extent(struct inode *inode,
4287 struct btrfs_path *path,
4288 struct btrfs_key *found_key,
4289 const u64 item_end,
4290 const u64 new_size)
4291{
4292 struct extent_buffer *leaf = path->nodes[0];
4293 int slot = path->slots[0];
4294 struct btrfs_file_extent_item *fi;
4295 u32 size = (u32)(new_size - found_key->offset);
4296 struct btrfs_root *root = BTRFS_I(inode)->root;
4297
4298 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4299
4300 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4301 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004302 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004303
4304 /*
4305 * Zero out the remaining of the last page of our inline extent,
4306 * instead of directly truncating our inline extent here - that
4307 * would be much more complex (decompressing all the data, then
4308 * compressing the truncated data, which might be bigger than
4309 * the size of the inline extent, resize the extent, etc).
4310 * We release the path because to get the page we might need to
4311 * read the extent item from disk (data not in the page cache).
4312 */
4313 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304314 return btrfs_truncate_block(inode, offset, page_end - offset,
4315 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004316 }
4317
4318 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4319 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004320 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004321
4322 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4323 inode_sub_bytes(inode, item_end + 1 - new_size);
4324
4325 return 0;
4326}
4327
Chris Mason323ac952008-10-01 19:05:46 -04004328/*
Chris Mason39279cc2007-06-12 06:35:45 -04004329 * this can truncate away extent items, csum items and directory items.
4330 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004331 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004332 *
4333 * csum items that cross the new i_size are truncated to the new size
4334 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004335 *
4336 * min_type is the minimum key type to truncate down to. If set to 0, this
4337 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004338 */
Yan, Zheng80825102009-11-12 09:35:36 +00004339int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4340 struct btrfs_root *root,
4341 struct inode *inode,
4342 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004345 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004346 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004347 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004348 struct btrfs_key key;
4349 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004350 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004351 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004352 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004353 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004354 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004355 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004356 int found_extent;
4357 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004358 int pending_del_nr = 0;
4359 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004360 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004361 int ret;
4362 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004363 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004364 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004365 bool be_nice = 0;
4366 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004367 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004368
4369 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004370
Chris Mason28ed1342014-12-17 09:41:04 -08004371 /*
4372 * for non-free space inodes and ref cows, we want to back off from
4373 * time to time
4374 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004375 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004376 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4377 be_nice = 1;
4378
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004379 path = btrfs_alloc_path();
4380 if (!path)
4381 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004382 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004383
Josef Bacik5dc562c2012-08-17 13:14:17 -04004384 /*
4385 * We want to drop from the next block forward in case this new size is
4386 * not block aligned since we will be keeping the last block of the
4387 * extent just the way it is.
4388 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004389 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004390 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004391 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004392 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004393 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004394
Miao Xie16cdcec2011-04-22 18:12:22 +08004395 /*
4396 * This function is also used to drop the items in the log tree before
4397 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4398 * it is used to drop the loged items. So we shouldn't kill the delayed
4399 * items.
4400 */
4401 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004402 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004403
Li Zefan33345d012011-04-20 10:31:50 +08004404 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004406 key.type = (u8)-1;
4407
Chris Mason85e21ba2008-01-29 15:11:36 -05004408search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004409 /*
4410 * with a 16K leaf size and 128MB extents, you can actually queue
4411 * up a huge file in a single leaf. Most of the time that
4412 * bytes_deleted is > 0, it will be huge by the time we get here
4413 */
Byongho Leeee221842015-12-15 01:42:10 +09004414 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004415 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004416 err = -EAGAIN;
4417 goto error;
4418 }
4419 }
4420
4421
Chris Masonb9473432009-03-13 11:00:37 -04004422 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004423 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004424 if (ret < 0) {
4425 err = ret;
4426 goto out;
4427 }
Chris Masond3977122009-01-05 21:25:51 -05004428
Chris Mason85e21ba2008-01-29 15:11:36 -05004429 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004430 /* there are no items in the tree for us to truncate, we're
4431 * done
4432 */
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (path->slots[0] == 0)
4434 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004435 path->slots[0]--;
4436 }
4437
Chris Masond3977122009-01-05 21:25:51 -05004438 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004439 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004440 leaf = path->nodes[0];
4441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004442 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004443
Li Zefan33345d012011-04-20 10:31:50 +08004444 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004445 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004446
Chris Mason85e21ba2008-01-29 15:11:36 -05004447 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004448 break;
4449
Chris Mason5f39d392007-10-15 16:14:19 -04004450 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004451 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004452 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004453 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004454 extent_type = btrfs_file_extent_type(leaf, fi);
4455 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004456 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004457 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004458 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004459 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004460 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 }
Yan008630c2007-11-07 13:31:09 -05004462 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004463 }
Yan, Zheng80825102009-11-12 09:35:36 +00004464 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004465 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004466 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004467 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004468 break;
4469 if (found_key.offset >= new_size)
4470 del_item = 1;
4471 else
4472 del_item = 0;
4473 }
Chris Mason39279cc2007-06-12 06:35:45 -04004474 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004475 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004476 if (found_type != BTRFS_EXTENT_DATA_KEY)
4477 goto delete;
4478
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004479 if (del_item)
4480 last_size = found_key.offset;
4481 else
4482 last_size = new_size;
4483
Chris Mason179e29e2007-11-01 11:28:41 -04004484 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004485 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004486 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004487 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004488 u64 orig_num_bytes =
4489 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004490 extent_num_bytes = ALIGN(new_size -
4491 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004492 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004493 btrfs_set_file_extent_num_bytes(leaf, fi,
4494 extent_num_bytes);
4495 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004496 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004497 if (test_bit(BTRFS_ROOT_REF_COWS,
4498 &root->state) &&
4499 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004500 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004501 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004502 } else {
Chris Masondb945352007-10-15 16:15:53 -04004503 extent_num_bytes =
4504 btrfs_file_extent_disk_num_bytes(leaf,
4505 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004506 extent_offset = found_key.offset -
4507 btrfs_file_extent_offset(leaf, fi);
4508
Chris Mason39279cc2007-06-12 06:35:45 -04004509 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004510 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004511 if (extent_start != 0) {
4512 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004513 if (test_bit(BTRFS_ROOT_REF_COWS,
4514 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004515 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
4517 }
Chris Mason90692182008-02-08 13:49:28 -05004518 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004519 /*
4520 * we can't truncate inline items that have had
4521 * special encodings
4522 */
4523 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004524 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4525 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004526
4527 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004528 * Need to release path in order to truncate a
4529 * compressed extent. So delete any accumulated
4530 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004531 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004532 if (btrfs_file_extent_compression(leaf, fi) !=
4533 BTRFS_COMPRESS_NONE && pending_del_nr) {
4534 err = btrfs_del_items(trans, root, path,
4535 pending_del_slot,
4536 pending_del_nr);
4537 if (err) {
4538 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004539 err);
4540 goto error;
4541 }
4542 pending_del_nr = 0;
4543 }
4544
4545 err = truncate_inline_extent(inode, path,
4546 &found_key,
4547 item_end,
4548 new_size);
4549 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004550 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004551 goto error;
4552 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004553 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4554 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004555 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004556 }
Chris Mason39279cc2007-06-12 06:35:45 -04004557 }
Chris Mason179e29e2007-11-01 11:28:41 -04004558delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004559 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004560 if (!pending_del_nr) {
4561 /* no pending yet, add ourselves */
4562 pending_del_slot = path->slots[0];
4563 pending_del_nr = 1;
4564 } else if (pending_del_nr &&
4565 path->slots[0] + 1 == pending_del_slot) {
4566 /* hop on the pending chunk */
4567 pending_del_nr++;
4568 pending_del_slot = path->slots[0];
4569 } else {
Chris Masond3977122009-01-05 21:25:51 -05004570 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004571 }
Chris Mason39279cc2007-06-12 06:35:45 -04004572 } else {
4573 break;
4574 }
Chris Mason28f75a02015-02-04 06:59:29 -08004575 should_throttle = 0;
4576
Miao Xie27cdeb72014-04-02 19:51:05 +08004577 if (found_extent &&
4578 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004579 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004580 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004581 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004582 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004583 extent_num_bytes, 0,
4584 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004585 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004586 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004587 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4588 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004589 trans->delayed_ref_updates * 2,
4590 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004591 if (be_nice) {
4592 if (truncate_space_check(trans, root,
4593 extent_num_bytes)) {
4594 should_end = 1;
4595 }
4596 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004597 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004598 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004601
Yan, Zheng80825102009-11-12 09:35:36 +00004602 if (found_type == BTRFS_INODE_ITEM_KEY)
4603 break;
4604
4605 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004606 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004607 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004608 if (pending_del_nr) {
4609 ret = btrfs_del_items(trans, root, path,
4610 pending_del_slot,
4611 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004612 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004613 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004614 goto error;
4615 }
Yan, Zheng80825102009-11-12 09:35:36 +00004616 pending_del_nr = 0;
4617 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004618 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004619 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004620 unsigned long updates = trans->delayed_ref_updates;
4621 if (updates) {
4622 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004623 ret = btrfs_run_delayed_refs(trans,
4624 fs_info,
4625 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004626 if (ret && !err)
4627 err = ret;
4628 }
4629 }
Chris Mason28f75a02015-02-04 06:59:29 -08004630 /*
4631 * if we failed to refill our space rsv, bail out
4632 * and let the transaction restart
4633 */
4634 if (should_end) {
4635 err = -EAGAIN;
4636 goto error;
4637 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004638 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004639 } else {
4640 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004641 }
Chris Mason39279cc2007-06-12 06:35:45 -04004642 }
Yan, Zheng80825102009-11-12 09:35:36 +00004643out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004644 if (pending_del_nr) {
4645 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4646 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004647 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004648 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004649 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004650error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004651 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4652 ASSERT(last_size >= new_size);
4653 if (!err && last_size > new_size)
4654 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004655 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004656 }
Chris Mason28ed1342014-12-17 09:41:04 -08004657
Chris Mason39279cc2007-06-12 06:35:45 -04004658 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004659
Liu Bo19fd2df2016-12-01 13:01:02 -08004660 if (err == 0) {
4661 /* only inline file may have last_size != new_size */
4662 if (new_size >= fs_info->sectorsize ||
4663 new_size > fs_info->max_inline)
4664 ASSERT(last_size == new_size);
4665 }
4666
Byongho Leeee221842015-12-15 01:42:10 +09004667 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004668 unsigned long updates = trans->delayed_ref_updates;
4669 if (updates) {
4670 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004671 ret = btrfs_run_delayed_refs(trans, fs_info,
4672 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004673 if (ret && !err)
4674 err = ret;
4675 }
4676 }
Yan, Zheng80825102009-11-12 09:35:36 +00004677 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004678}
4679
Chris Masona52d9a82007-08-27 16:49:44 -04004680/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304681 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004682 * @inode - inode that we're zeroing
4683 * @from - the offset to start zeroing
4684 * @len - the length to zero, 0 to zero the entire range respective to the
4685 * offset
4686 * @front - zero up to the offset instead of from the offset on
4687 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304688 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004689 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004690 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304691int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004692 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004695 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004696 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4697 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004698 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004699 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004700 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004701 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004703 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004704 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004705 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 u64 block_start;
4707 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004708
Josef Bacik2aaa6652012-08-29 14:27:18 -04004709 if ((offset & (blocksize - 1)) == 0 &&
4710 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004711 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304712
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004713 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004715 if (ret)
4716 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004717
Chris Mason211c17f2008-05-15 09:13:45 -04004718again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004719 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004720 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004721 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 round_down(from, blocksize),
4723 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004724 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004725 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004726 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004727
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 block_start = round_down(from, blocksize);
4729 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730
Chris Masona52d9a82007-08-27 16:49:44 -04004731 if (!PageUptodate(page)) {
4732 ret = btrfs_readpage(NULL, page);
4733 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004734 if (page->mapping != mapping) {
4735 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004736 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004737 goto again;
4738 }
Chris Masona52d9a82007-08-27 16:49:44 -04004739 if (!PageUptodate(page)) {
4740 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004741 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004742 }
4743 }
Chris Mason211c17f2008-05-15 09:13:45 -04004744 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004745
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004747 set_page_extent_mapped(page);
4748
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304749 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004750 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304751 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004752 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004753 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004754 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004755 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004756 btrfs_put_ordered_extent(ordered);
4757 goto again;
4758 }
4759
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004761 EXTENT_DIRTY | EXTENT_DELALLOC |
4762 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004763 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004764
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004766 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004767 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004769 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004770 goto out_unlock;
4771 }
4772
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004774 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004776 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004777 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 memset(kaddr + (block_start - page_offset(page)),
4779 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004780 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 memset(kaddr + (block_start - page_offset(page)) + offset,
4782 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783 flush_dcache_page(page);
4784 kunmap(page);
4785 }
Chris Mason247e7432008-07-17 12:53:51 -04004786 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004787 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004789 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004790
Chris Mason89642222008-07-24 09:41:53 -04004791out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004792 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 btrfs_delalloc_release_space(inode, block_start,
4794 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004796 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004797out:
4798 return ret;
4799}
4800
Josef Bacik16e75492013-10-22 12:18:51 -04004801static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4802 u64 offset, u64 len)
4803{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004805 struct btrfs_trans_handle *trans;
4806 int ret;
4807
4808 /*
4809 * Still need to make sure the inode looks like it's been updated so
4810 * that any holes get logged if we fsync.
4811 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4813 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004814 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4815 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4816 return 0;
4817 }
4818
4819 /*
4820 * 1 - for the one we're dropping
4821 * 1 - for the one we're adding
4822 * 1 - for updating the inode.
4823 */
4824 trans = btrfs_start_transaction(root, 3);
4825 if (IS_ERR(trans))
4826 return PTR_ERR(trans);
4827
4828 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4829 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004830 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004831 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004832 return ret;
4833 }
4834
David Sterbaf85b7372017-01-20 14:54:07 +01004835 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4836 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004837 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004838 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004839 else
4840 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004841 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004842 return ret;
4843}
4844
Josef Bacik695a0d02011-03-04 15:46:53 -05004845/*
4846 * This function puts in dummy file extents for the area we're creating a hole
4847 * for. So if we are truncating this file to a larger size we need to insert
4848 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4849 * the range between oldsize and size
4850 */
Josef Bacika41ad392011-01-31 15:30:16 -05004851int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004853 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004854 struct btrfs_root *root = BTRFS_I(inode)->root;
4855 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004856 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004857 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004858 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004859 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4860 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004861 u64 last_byte;
4862 u64 cur_offset;
4863 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004864 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004865
Josef Bacika71754f2013-06-17 17:14:39 -04004866 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 * If our size started in the middle of a block we need to zero out the
4868 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004869 * expose stale data.
4870 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304871 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004872 if (err)
4873 return err;
4874
Yan Zheng9036c102008-10-30 14:19:41 -04004875 if (size <= hole_start)
4876 return 0;
4877
Yan Zheng9036c102008-10-30 14:19:41 -04004878 while (1) {
4879 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004880
David Sterbaff13db42015-12-03 14:30:40 +01004881 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004882 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004883 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004884 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004885 if (!ordered)
4886 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004887 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4888 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004889 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004890 btrfs_put_ordered_extent(ordered);
4891 }
4892
Yan Zheng9036c102008-10-30 14:19:41 -04004893 cur_offset = hole_start;
4894 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004895 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004896 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004897 if (IS_ERR(em)) {
4898 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004899 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004900 break;
4901 }
Yan Zheng9036c102008-10-30 14:19:41 -04004902 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004903 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004904 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004905 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004906 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004907
Josef Bacik16e75492013-10-22 12:18:51 -04004908 err = maybe_insert_hole(root, inode, cur_offset,
4909 hole_size);
4910 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004911 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004912 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004913 cur_offset + hole_size - 1, 0);
4914 hole_em = alloc_extent_map();
4915 if (!hole_em) {
4916 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4917 &BTRFS_I(inode)->runtime_flags);
4918 goto next;
4919 }
4920 hole_em->start = cur_offset;
4921 hole_em->len = hole_size;
4922 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004923
Josef Bacik5dc562c2012-08-17 13:14:17 -04004924 hole_em->block_start = EXTENT_MAP_HOLE;
4925 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004926 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004927 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004929 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004931
4932 while (1) {
4933 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004934 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004935 write_unlock(&em_tree->lock);
4936 if (err != -EEXIST)
4937 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004938 btrfs_drop_extent_cache(BTRFS_I(inode),
4939 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004940 cur_offset +
4941 hole_size - 1, 0);
4942 }
4943 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004944 }
Josef Bacik16e75492013-10-22 12:18:51 -04004945next:
Yan Zheng9036c102008-10-30 14:19:41 -04004946 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004947 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004948 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004949 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004950 break;
4951 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004952 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004953 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4954 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004955 return err;
4956}
4957
Eric Sandeen3972f262013-01-12 02:57:22 +00004958static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004959{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004960 struct btrfs_root *root = BTRFS_I(inode)->root;
4961 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004962 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004963 loff_t newsize = attr->ia_size;
4964 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004965 int ret;
4966
Eric Sandeen3972f262013-01-12 02:57:22 +00004967 /*
4968 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4969 * special case where we need to update the times despite not having
4970 * these flags set. For all other operations the VFS set these flags
4971 * explicitly if it wants a timestamp update.
4972 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004973 if (newsize != oldsize) {
4974 inode_inc_iversion(inode);
4975 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4976 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004977 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004978 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004979
Josef Bacika41ad392011-01-31 15:30:16 -05004980 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004981 /*
4982 * Don't do an expanding truncate while snapshoting is ongoing.
4983 * This is to ensure the snapshot captures a fully consistent
4984 * state of this file - if the snapshot captures this expanding
4985 * truncation, it must capture all writes that happened before
4986 * this truncation.
4987 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004988 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004989 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004990 if (ret) {
4991 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004992 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004993 }
Yan, Zheng80825102009-11-12 09:35:36 +00004994
Miao Xief4a2f4c2011-12-14 20:12:01 -05004995 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004996 if (IS_ERR(trans)) {
4997 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004998 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004999 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005000
5001 i_size_write(inode, newsize);
5002 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305003 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005004 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005005 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005006 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005007 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005008
Josef Bacika41ad392011-01-31 15:30:16 -05005009 /*
5010 * We're truncating a file that used to have good data down to
5011 * zero. Make sure it gets into the ordered flush list so that
5012 * any new writes get down to disk quickly.
5013 */
5014 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005015 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5016 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005017
Josef Bacikf3fe8202013-01-07 17:03:21 -05005018 /*
5019 * 1 for the orphan item we're going to add
5020 * 1 for the orphan item deletion.
5021 */
5022 trans = btrfs_start_transaction(root, 2);
5023 if (IS_ERR(trans))
5024 return PTR_ERR(trans);
5025
5026 /*
5027 * We need to do this in case we fail at _any_ point during the
5028 * actual truncate. Once we do the truncate_setsize we could
5029 * invalidate pages which forces any outstanding ordered io to
5030 * be instantly completed which will give us extents that need
5031 * to be truncated. If we fail to get an orphan inode down we
5032 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005033 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005034 * will be consistent.
5035 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005036 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005037 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005038 if (ret)
5039 return ret;
5040
Josef Bacika41ad392011-01-31 15:30:16 -05005041 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5042 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005043
5044 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005045 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005046 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005047 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005048
Josef Bacika41ad392011-01-31 15:30:16 -05005049 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005050 if (ret && inode->i_nlink) {
5051 int err;
5052
Liu Bo19fd2df2016-12-01 13:01:02 -08005053 /* To get a stable disk_i_size */
5054 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5055 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005056 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005057 return err;
5058 }
5059
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005060 /*
5061 * failed to truncate, disk_i_size is only adjusted down
5062 * as we remove extents, so it should represent the true
5063 * size of the inode, so reset the in memory size and
5064 * delete our orphan entry.
5065 */
5066 trans = btrfs_join_transaction(root);
5067 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005068 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005069 return ret;
5070 }
5071 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005072 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005073 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005074 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005075 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005076 }
Yan, Zheng80825102009-11-12 09:35:36 +00005077 }
5078
Josef Bacika41ad392011-01-31 15:30:16 -05005079 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005080}
5081
Chris Mason39279cc2007-06-12 06:35:45 -04005082static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5083{
David Howells2b0143b2015-03-17 22:25:59 +00005084 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005085 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005086 int err;
5087
Li Zefanb83cc962010-12-20 16:04:08 +08005088 if (btrfs_root_readonly(root))
5089 return -EROFS;
5090
Jan Kara31051c82016-05-26 16:55:18 +02005091 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005092 if (err)
5093 return err;
5094
Chris Mason5a3f23d2009-03-31 13:27:11 -04005095 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005096 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005097 if (err)
5098 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005099 }
Yan Zheng9036c102008-10-30 14:19:41 -04005100
Christoph Hellwig10257742010-06-04 11:30:02 +02005101 if (attr->ia_valid) {
5102 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005103 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005104 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005105
Josef Bacik22c44fe2011-11-30 10:45:38 -05005106 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005107 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005108 }
5109
Chris Mason39279cc2007-06-12 06:35:45 -04005110 return err;
5111}
Chris Mason61295eb2008-01-14 16:24:38 -05005112
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005113/*
5114 * While truncating the inode pages during eviction, we get the VFS calling
5115 * btrfs_invalidatepage() against each page of the inode. This is slow because
5116 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5117 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5118 * extent_state structures over and over, wasting lots of time.
5119 *
5120 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5121 * those expensive operations on a per page basis and do only the ordered io
5122 * finishing, while we release here the extent_map and extent_state structures,
5123 * without the excessive merging and splitting.
5124 */
5125static void evict_inode_truncate_pages(struct inode *inode)
5126{
5127 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5128 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5129 struct rb_node *node;
5130
5131 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005132 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005133
5134 write_lock(&map_tree->lock);
5135 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5136 struct extent_map *em;
5137
5138 node = rb_first(&map_tree->map);
5139 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005140 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5141 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005142 remove_extent_mapping(map_tree, em);
5143 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005144 if (need_resched()) {
5145 write_unlock(&map_tree->lock);
5146 cond_resched();
5147 write_lock(&map_tree->lock);
5148 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005149 }
5150 write_unlock(&map_tree->lock);
5151
Filipe Manana6ca07092015-05-26 00:55:42 +01005152 /*
5153 * Keep looping until we have no more ranges in the io tree.
5154 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005155 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5156 * still in progress (unlocked the pages in the bio but did not yet
5157 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005158 * ranges can still be locked and eviction started because before
5159 * submitting those bios, which are executed by a separate task (work
5160 * queue kthread), inode references (inode->i_count) were not taken
5161 * (which would be dropped in the end io callback of each bio).
5162 * Therefore here we effectively end up waiting for those bios and
5163 * anyone else holding locked ranges without having bumped the inode's
5164 * reference count - if we don't do it, when they access the inode's
5165 * io_tree to unlock a range it may be too late, leading to an
5166 * use-after-free issue.
5167 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005168 spin_lock(&io_tree->lock);
5169 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5170 struct extent_state *state;
5171 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005172 u64 start;
5173 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005174
5175 node = rb_first(&io_tree->state);
5176 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005177 start = state->start;
5178 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 spin_unlock(&io_tree->lock);
5180
David Sterbaff13db42015-12-03 14:30:40 +01005181 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005182
5183 /*
5184 * If still has DELALLOC flag, the extent didn't reach disk,
5185 * and its reserved space won't be freed by delayed_ref.
5186 * So we need to free its reserved space here.
5187 * (Refer to comment in btrfs_invalidatepage, case 2)
5188 *
5189 * Note, end is the bytenr of last byte, so we need + 1 here.
5190 */
5191 if (state->state & EXTENT_DELALLOC)
5192 btrfs_qgroup_free_data(inode, start, end - start + 1);
5193
Filipe Manana6ca07092015-05-26 00:55:42 +01005194 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005195 EXTENT_LOCKED | EXTENT_DIRTY |
5196 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5197 EXTENT_DEFRAG, 1, 1,
5198 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005199
Filipe Manana7064dd52014-08-08 02:47:05 +01005200 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201 spin_lock(&io_tree->lock);
5202 }
5203 spin_unlock(&io_tree->lock);
5204}
5205
Al Virobd555972010-06-07 11:35:40 -04005206void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005207{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005208 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005209 struct btrfs_trans_handle *trans;
5210 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005211 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005212 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005213 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005214 int ret;
5215
liubo1abe9b82011-03-24 11:18:59 +00005216 trace_btrfs_inode_evict(inode);
5217
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005218 if (!root) {
5219 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5220 return;
5221 }
5222
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005223 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005224
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005225 evict_inode_truncate_pages(inode);
5226
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005227 if (inode->i_nlink &&
5228 ((btrfs_root_refs(&root->root_item) != 0 &&
5229 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005230 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005231 goto no_delete;
5232
Chris Mason39279cc2007-06-12 06:35:45 -04005233 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005234 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005235 goto no_delete;
5236 }
Al Virobd555972010-06-07 11:35:40 -04005237 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005238 if (!special_file(inode->i_mode))
5239 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005240
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005241 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005242
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005243 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005244 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005245 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005246 goto no_delete;
5247 }
5248
Yan, Zheng76dda932009-09-21 16:00:26 -04005249 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005250 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5251 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005252 goto no_delete;
5253 }
5254
Nikolay Borisovaa790212017-01-10 20:35:40 +02005255 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005256 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005257 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005258 goto no_delete;
5259 }
5260
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005261 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005262 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005263 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005264 goto no_delete;
5265 }
Josef Bacik4a338542011-08-29 11:01:31 -04005266 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005267 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005268 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005269
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005270 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005271
Josef Bacik4289a662011-08-05 13:22:24 -04005272 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005273 * This is a bit simpler than btrfs_truncate since we've already
5274 * reserved our space for our orphan item in the unlink, so we just
5275 * need to reserve some slack space in case we add bytes and update
5276 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005277 */
Yan, Zheng80825102009-11-12 09:35:36 +00005278 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005279 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5280 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005281
Josef Bacik726c35f2011-09-26 15:46:06 -04005282 /*
5283 * Try and steal from the global reserve since we will
5284 * likely not use this space anyway, we want to try as
5285 * hard as possible to get this to work.
5286 */
5287 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005288 steal_from_global++;
5289 else
5290 steal_from_global = 0;
5291 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005292
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 /*
5294 * steal_from_global == 0: we reserved stuff, hooray!
5295 * steal_from_global == 1: we didn't reserve stuff, boo!
5296 * steal_from_global == 2: we've committed, still not a lot of
5297 * room but maybe we'll have room in the global reserve this
5298 * time.
5299 * steal_from_global == 3: abandon all hope!
5300 */
5301 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 btrfs_warn(fs_info,
5303 "Could not get space for a delete, will truncate on mount %d",
5304 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005305 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005306 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005307 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005308 }
5309
Miao Xie0e8c36a2012-12-19 06:59:51 +00005310 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005311 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005312 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005313 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005314 goto no_delete;
5315 }
5316
Josef Bacik3bce8762015-02-24 12:35:51 -08005317 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005318 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005319 * sure there is room to do it, if not we need to commit and try
5320 * again.
5321 */
5322 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005323 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005324 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005325 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005326 else
5327 ret = -ENOSPC;
5328 }
5329
5330 /*
5331 * Couldn't steal from the global reserve, we have too much
5332 * pending stuff built up, commit the transaction and try it
5333 * again.
5334 */
5335 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005336 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005337 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005338 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005339 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005340 goto no_delete;
5341 }
5342 continue;
5343 } else {
5344 steal_from_global = 0;
5345 }
5346
Josef Bacik4289a662011-08-05 13:22:24 -04005347 trans->block_rsv = rsv;
5348
Yan, Zhengd68fc572010-05-16 10:49:58 -04005349 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005350 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005351 break;
5352
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005354 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005355 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005357 }
5358
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005359 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005360
Josef Bacik4ef31a42013-08-13 14:10:08 -04005361 /*
5362 * Errors here aren't a big deal, it just means we leave orphan items
5363 * in the tree. They will be cleaned up on the next mount.
5364 */
Yan, Zheng80825102009-11-12 09:35:36 +00005365 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005366 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005367 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005368 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005369 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005370 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005371
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005372 trans->block_rsv = &fs_info->trans_block_rsv;
5373 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005374 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005375 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005376
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005377 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005378 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005379no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005380 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005381 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005382}
5383
5384/*
5385 * this returns the key found in the dir entry in the location pointer.
5386 * If no dir entries were found, location->objectid is 0.
5387 */
5388static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5389 struct btrfs_key *location)
5390{
5391 const char *name = dentry->d_name.name;
5392 int namelen = dentry->d_name.len;
5393 struct btrfs_dir_item *di;
5394 struct btrfs_path *path;
5395 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005396 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005397
5398 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005399 if (!path)
5400 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005401
David Sterbaf85b7372017-01-20 14:54:07 +01005402 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5403 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005404 if (IS_ERR(di))
5405 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005406
David Sterbac7040052011-04-19 18:00:01 +02005407 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005408 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005409
Chris Mason5f39d392007-10-15 16:14:19 -04005410 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005411out:
Chris Mason39279cc2007-06-12 06:35:45 -04005412 btrfs_free_path(path);
5413 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005414out_err:
5415 location->objectid = 0;
5416 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005417}
5418
5419/*
5420 * when we hit a tree root in a directory, the btrfs part of the inode
5421 * needs to be changed to reflect the root directory of the tree root. This
5422 * is kind of like crossing a mount point.
5423 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005424static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425 struct inode *dir,
5426 struct dentry *dentry,
5427 struct btrfs_key *location,
5428 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005429{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 struct btrfs_path *path;
5431 struct btrfs_root *new_root;
5432 struct btrfs_root_ref *ref;
5433 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005434 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005435 int ret;
5436 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005437
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005438 path = btrfs_alloc_path();
5439 if (!path) {
5440 err = -ENOMEM;
5441 goto out;
5442 }
Chris Mason39279cc2007-06-12 06:35:45 -04005443
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005444 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005445 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5446 key.type = BTRFS_ROOT_REF_KEY;
5447 key.offset = location->objectid;
5448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005449 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005450 if (ret) {
5451 if (ret < 0)
5452 err = ret;
5453 goto out;
5454 }
Chris Mason39279cc2007-06-12 06:35:45 -04005455
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005456 leaf = path->nodes[0];
5457 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005458 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005459 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5460 goto out;
5461
5462 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5463 (unsigned long)(ref + 1),
5464 dentry->d_name.len);
5465 if (ret)
5466 goto out;
5467
David Sterbab3b4aa72011-04-21 01:20:15 +02005468 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005469
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005470 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005471 if (IS_ERR(new_root)) {
5472 err = PTR_ERR(new_root);
5473 goto out;
5474 }
5475
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005476 *sub_root = new_root;
5477 location->objectid = btrfs_root_dirid(&new_root->root_item);
5478 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005479 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 err = 0;
5481out:
5482 btrfs_free_path(path);
5483 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005484}
5485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486static void inode_tree_add(struct inode *inode)
5487{
5488 struct btrfs_root *root = BTRFS_I(inode)->root;
5489 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005490 struct rb_node **p;
5491 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005492 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005493 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005494
Al Viro1d3382cb2010-10-23 15:19:20 -04005495 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005496 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005497 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005499 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005500 while (*p) {
5501 parent = *p;
5502 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5503
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005504 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005505 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005506 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005507 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005508 else {
5509 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005510 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005511 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005512 RB_CLEAR_NODE(parent);
5513 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005514 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515 }
5516 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005517 rb_link_node(new, parent, p);
5518 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005519 spin_unlock(&root->inode_lock);
5520}
5521
5522static void inode_tree_del(struct inode *inode)
5523{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005524 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005525 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005528 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005532 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005533 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005534 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005535
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005536 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005538 spin_lock(&root->inode_lock);
5539 empty = RB_EMPTY_ROOT(&root->inode_tree);
5540 spin_unlock(&root->inode_lock);
5541 if (empty)
5542 btrfs_add_dead_root(root);
5543 }
5544}
5545
Jeff Mahoney143bede2012-03-01 14:56:26 +01005546void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005547{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005548 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005549 struct rb_node *node;
5550 struct rb_node *prev;
5551 struct btrfs_inode *entry;
5552 struct inode *inode;
5553 u64 objectid = 0;
5554
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005555 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005556 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005557
5558 spin_lock(&root->inode_lock);
5559again:
5560 node = root->inode_tree.rb_node;
5561 prev = NULL;
5562 while (node) {
5563 prev = node;
5564 entry = rb_entry(node, struct btrfs_inode, rb_node);
5565
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005566 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005567 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005568 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005569 node = node->rb_right;
5570 else
5571 break;
5572 }
5573 if (!node) {
5574 while (prev) {
5575 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005576 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005577 node = prev;
5578 break;
5579 }
5580 prev = rb_next(prev);
5581 }
5582 }
5583 while (node) {
5584 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005585 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 inode = igrab(&entry->vfs_inode);
5587 if (inode) {
5588 spin_unlock(&root->inode_lock);
5589 if (atomic_read(&inode->i_count) > 1)
5590 d_prune_aliases(inode);
5591 /*
Al Viro45321ac2010-06-07 13:43:19 -04005592 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005593 * the inode cache when its usage count
5594 * hits zero.
5595 */
5596 iput(inode);
5597 cond_resched();
5598 spin_lock(&root->inode_lock);
5599 goto again;
5600 }
5601
5602 if (cond_resched_lock(&root->inode_lock))
5603 goto again;
5604
5605 node = rb_next(node);
5606 }
5607 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608}
5609
Chris Masone02119d2008-09-05 16:13:11 -04005610static int btrfs_init_locked_inode(struct inode *inode, void *p)
5611{
5612 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005613 inode->i_ino = args->location->objectid;
5614 memcpy(&BTRFS_I(inode)->location, args->location,
5615 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005616 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005617 return 0;
5618}
5619
5620static int btrfs_find_actor(struct inode *inode, void *opaque)
5621{
5622 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005623 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005624 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005625}
5626
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005627static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005628 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005630{
5631 struct inode *inode;
5632 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005633 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005634
Chris Mason90d3e592014-01-09 17:28:00 -08005635 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005636 args.root = root;
5637
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005638 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005639 btrfs_init_locked_inode,
5640 (void *)&args);
5641 return inode;
5642}
5643
Balaji Rao1a54ef82008-07-21 02:01:04 +05305644/* Get an inode object given its location and corresponding root.
5645 * Returns in *is_new if the inode was read from disk
5646 */
5647struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005648 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305649{
5650 struct inode *inode;
5651
Chris Mason90d3e592014-01-09 17:28:00 -08005652 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305653 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305655
5656 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005657 int ret;
5658
5659 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005660 if (!is_bad_inode(inode)) {
5661 inode_tree_add(inode);
5662 unlock_new_inode(inode);
5663 if (new)
5664 *new = 1;
5665 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005666 unlock_new_inode(inode);
5667 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005668 ASSERT(ret < 0);
5669 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005670 }
5671 }
5672
Balaji Rao1a54ef82008-07-21 02:01:04 +05305673 return inode;
5674}
5675
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005676static struct inode *new_simple_dir(struct super_block *s,
5677 struct btrfs_key *key,
5678 struct btrfs_root *root)
5679{
5680 struct inode *inode = new_inode(s);
5681
5682 if (!inode)
5683 return ERR_PTR(-ENOMEM);
5684
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685 BTRFS_I(inode)->root = root;
5686 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005687 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005688
5689 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005690 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005691 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005692 inode->i_fop = &simple_dir_operations;
5693 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005694 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305695 inode->i_atime = inode->i_mtime;
5696 inode->i_ctime = inode->i_mtime;
5697 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005698
5699 return inode;
5700}
5701
Chris Mason3de45862008-11-17 21:02:50 -05005702struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005704 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005705 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005706 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005707 struct btrfs_root *sub_root = root;
5708 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005709 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005710 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005711
5712 if (dentry->d_name.len > BTRFS_NAME_LEN)
5713 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005714
Jeff Layton39e3c952012-11-28 11:30:53 -05005715 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005716 if (ret < 0)
5717 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005718
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005719 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005720 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721
5722 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005723 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005725 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726
5727 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5728
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005729 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005730 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 &location, &sub_root);
5732 if (ret < 0) {
5733 if (ret != -ENOENT)
5734 inode = ERR_PTR(ret);
5735 else
5736 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5737 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005738 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005739 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005740 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005741
Julia Lawall34d19ba2011-01-24 19:55:19 +00005742 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005743 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005744 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005745 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005746 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005747 if (ret) {
5748 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005749 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005750 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005751 }
5752
Chris Mason3de45862008-11-17 21:02:50 -05005753 return inode;
5754}
5755
Nick Pigginfe15ce42011-01-07 17:49:23 +11005756static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005757{
5758 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005759 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005760
Li Zefan848cce02012-02-21 17:04:28 +08005761 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005762 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005763
Li Zefan848cce02012-02-21 17:04:28 +08005764 if (inode) {
5765 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005766 if (btrfs_root_refs(&root->root_item) == 0)
5767 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005768
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005769 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005770 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005771 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005772 return 0;
5773}
5774
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005775static void btrfs_dentry_release(struct dentry *dentry)
5776{
Daeseok Youn944a4512014-04-14 15:37:02 +09005777 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005778}
5779
Chris Mason3de45862008-11-17 21:02:50 -05005780static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005781 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005782{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005783 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005784
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005785 inode = btrfs_lookup_dentry(dir, dentry);
5786 if (IS_ERR(inode)) {
5787 if (PTR_ERR(inode) == -ENOENT)
5788 inode = NULL;
5789 else
5790 return ERR_CAST(inode);
5791 }
5792
Al Viro41d28bc2014-10-12 22:24:21 -04005793 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005794}
5795
Miao Xie16cdcec2011-04-22 18:12:22 +08005796unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005797 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5798};
5799
Al Viro9cdda8d2013-05-22 16:48:09 -04005800static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005801{
Al Viro9cdda8d2013-05-22 16:48:09 -04005802 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005804 struct btrfs_root *root = BTRFS_I(inode)->root;
5805 struct btrfs_item *item;
5806 struct btrfs_dir_item *di;
5807 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005808 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005809 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005810 struct list_head ins_list;
5811 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005812 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005813 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005815 unsigned char d_type;
5816 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005817 char tmp_name[32];
5818 char *name_ptr;
5819 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005820 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005821 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005822
Al Viro9cdda8d2013-05-22 16:48:09 -04005823 if (!dir_emit_dots(file, ctx))
5824 return 0;
5825
David Woodhouse49593bf2008-08-17 17:08:36 +01005826 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005827 if (!path)
5828 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005829
David Sterbae4058b52015-11-27 16:31:35 +01005830 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005831
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005832 INIT_LIST_HEAD(&ins_list);
5833 INIT_LIST_HEAD(&del_list);
5834 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005835
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005836 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005837 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005838 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005839
Chris Mason39279cc2007-06-12 06:35:45 -04005840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5841 if (ret < 0)
5842 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005843
5844 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005845 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005846 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005847 if (slot >= btrfs_header_nritems(leaf)) {
5848 ret = btrfs_next_leaf(root, path);
5849 if (ret < 0)
5850 goto err;
5851 else if (ret > 0)
5852 break;
5853 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005854 }
Chris Mason3de45862008-11-17 21:02:50 -05005855
Ross Kirkdd3cc162013-09-16 15:58:09 +01005856 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005857 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5858
5859 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005860 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005861 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005862 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005863 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005864 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005865 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005866 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005867
Al Viro9cdda8d2013-05-22 16:48:09 -04005868 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005869
Chris Mason39279cc2007-06-12 06:35:45 -04005870 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005871 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005872 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005873
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 name_len = btrfs_dir_name_len(leaf, di);
5875 if (name_len <= sizeof(tmp_name)) {
5876 name_ptr = tmp_name;
5877 } else {
5878 name_ptr = kmalloc(name_len, GFP_KERNEL);
5879 if (!name_ptr) {
5880 ret = -ENOMEM;
5881 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005882 }
Chris Mason39279cc2007-06-12 06:35:45 -04005883 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005884 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5885 name_len);
5886
5887 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5888 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5889
5890 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5891 d_type);
5892
5893 if (name_ptr != tmp_name)
5894 kfree(name_ptr);
5895
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005896 if (over)
5897 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005898 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005899next:
5900 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005901 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005902
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005903 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005904 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005905 goto nopos;
5906
Zach Browndb62efb2013-07-11 16:19:42 -07005907 /*
5908 * Stop new entries from being returned after we return the last
5909 * entry.
5910 *
5911 * New directory entries are assigned a strictly increasing
5912 * offset. This means that new entries created during readdir
5913 * are *guaranteed* to be seen in the future by that readdir.
5914 * This has broken buggy programs which operate on names as
5915 * they're returned by readdir. Until we re-use freed offsets
5916 * we have this hack to stop new entries from being returned
5917 * under the assumption that they'll never reach this huge
5918 * offset.
5919 *
5920 * This is being careful not to overflow 32bit loff_t unless the
5921 * last entry requires it because doing so has broken 32bit apps
5922 * in the past.
5923 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005924 if (ctx->pos >= INT_MAX)
5925 ctx->pos = LLONG_MAX;
5926 else
5927 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005928nopos:
5929 ret = 0;
5930err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005931 if (put)
5932 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005933 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005934 return ret;
5935}
5936
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005937int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005938{
5939 struct btrfs_root *root = BTRFS_I(inode)->root;
5940 struct btrfs_trans_handle *trans;
5941 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005942 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005943
Josef Bacik72ac3c02012-05-23 14:13:11 -04005944 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005945 return 0;
5946
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005947 if (btrfs_fs_closing(root->fs_info) &&
5948 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005949 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005950
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005951 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005952 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005953 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005954 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005955 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005956 if (IS_ERR(trans))
5957 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005958 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 }
5960 return ret;
5961}
5962
5963/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005964 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005965 * inode changes. But, it is most likely to find the inode in cache.
5966 * FIXME, needs more benchmarking...there are no reasons other than performance
5967 * to keep or drop this code.
5968 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005969static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005970{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005971 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005972 struct btrfs_root *root = BTRFS_I(inode)->root;
5973 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005974 int ret;
5975
Josef Bacik72ac3c02012-05-23 14:13:11 -04005976 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005977 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005978
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005979 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005980 if (IS_ERR(trans))
5981 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005982
5983 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005984 if (ret && ret == -ENOSPC) {
5985 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005986 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005987 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005988 if (IS_ERR(trans))
5989 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005990
Chris Mason94b60442010-05-26 11:02:00 -04005991 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005992 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005993 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005994 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005995 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005996
5997 return ret;
5998}
5999
6000/*
6001 * This is a copy of file_update_time. We need this so we can return error on
6002 * ENOSPC for updating the inode in the case of file write and mmap writes.
6003 */
Josef Bacike41f9412012-03-26 09:46:47 -04006004static int btrfs_update_time(struct inode *inode, struct timespec *now,
6005 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006006{
Alexander Block2bc5565282012-06-15 09:49:33 +02006007 struct btrfs_root *root = BTRFS_I(inode)->root;
6008
6009 if (btrfs_root_readonly(root))
6010 return -EROFS;
6011
Josef Bacike41f9412012-03-26 09:46:47 -04006012 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006013 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006014 if (flags & S_CTIME)
6015 inode->i_ctime = *now;
6016 if (flags & S_MTIME)
6017 inode->i_mtime = *now;
6018 if (flags & S_ATIME)
6019 inode->i_atime = *now;
6020 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006021}
6022
Chris Masond352ac62008-09-29 15:18:18 -04006023/*
6024 * find the highest existing sequence number in a directory
6025 * and then set the in-memory index_cnt variable to reflect
6026 * free sequence numbers
6027 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006028static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006029{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006030 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006031 struct btrfs_key key, found_key;
6032 struct btrfs_path *path;
6033 struct extent_buffer *leaf;
6034 int ret;
6035
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006036 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006037 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006038 key.offset = (u64)-1;
6039
6040 path = btrfs_alloc_path();
6041 if (!path)
6042 return -ENOMEM;
6043
6044 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6045 if (ret < 0)
6046 goto out;
6047 /* FIXME: we should be able to handle this */
6048 if (ret == 0)
6049 goto out;
6050 ret = 0;
6051
6052 /*
6053 * MAGIC NUMBER EXPLANATION:
6054 * since we search a directory based on f_pos we have to start at 2
6055 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6056 * else has to start at 2
6057 */
6058 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006059 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006060 goto out;
6061 }
6062
6063 path->slots[0]--;
6064
6065 leaf = path->nodes[0];
6066 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6067
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006068 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006069 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006070 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006071 goto out;
6072 }
6073
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006074 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006075out:
6076 btrfs_free_path(path);
6077 return ret;
6078}
6079
Chris Masond352ac62008-09-29 15:18:18 -04006080/*
6081 * helper to find a free sequence number in a given directory. This current
6082 * code is very simple, later versions will do smarter things in the btree
6083 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006084int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006085{
6086 int ret = 0;
6087
Nikolay Borisov877574e2017-02-20 13:50:33 +02006088 if (dir->index_cnt == (u64)-1) {
6089 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006090 if (ret) {
6091 ret = btrfs_set_inode_index_count(dir);
6092 if (ret)
6093 return ret;
6094 }
Josef Bacikaec74772008-07-24 12:12:38 -04006095 }
6096
Nikolay Borisov877574e2017-02-20 13:50:33 +02006097 *index = dir->index_cnt;
6098 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006099
6100 return ret;
6101}
6102
Chris Masonb0d5d102014-09-08 13:08:51 -07006103static int btrfs_insert_inode_locked(struct inode *inode)
6104{
6105 struct btrfs_iget_args args;
6106 args.location = &BTRFS_I(inode)->location;
6107 args.root = BTRFS_I(inode)->root;
6108
6109 return insert_inode_locked4(inode,
6110 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6111 btrfs_find_actor, &args);
6112}
6113
Chris Mason39279cc2007-06-12 06:35:45 -04006114static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6115 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006116 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006117 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006118 u64 ref_objectid, u64 objectid,
6119 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006120{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006121 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006122 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006123 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006124 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006125 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006126 struct btrfs_inode_ref *ref;
6127 struct btrfs_key key[2];
6128 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006129 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006130 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006131 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006132
Chris Mason5f39d392007-10-15 16:14:19 -04006133 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006134 if (!path)
6135 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006136
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006137 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006138 if (!inode) {
6139 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006140 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006141 }
Chris Mason39279cc2007-06-12 06:35:45 -04006142
Li Zefan581bb052011-04-20 10:06:11 +08006143 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006144 * O_TMPFILE, set link count to 0, so that after this point,
6145 * we fill in an inode item with the correct link count.
6146 */
6147 if (!name)
6148 set_nlink(inode, 0);
6149
6150 /*
Li Zefan581bb052011-04-20 10:06:11 +08006151 * we have to initialize this early, so we can reclaim the inode
6152 * number if we fail afterwards in this function.
6153 */
6154 inode->i_ino = objectid;
6155
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006156 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006157 trace_btrfs_inode_request(dir);
6158
Nikolay Borisov877574e2017-02-20 13:50:33 +02006159 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006160 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006161 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006162 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006163 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006164 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006165 } else if (dir) {
6166 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006167 }
6168 /*
6169 * index_cnt is ignored for everything but a dir,
6170 * btrfs_get_inode_index_count has an explanation for the magic
6171 * number
6172 */
6173 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006174 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006175 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006176 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006177 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006178
Josef Bacik5dc562c2012-08-17 13:14:17 -04006179 /*
6180 * We could have gotten an inode number from somebody who was fsynced
6181 * and then removed in this same transaction, so let's just set full
6182 * sync since it will be a full sync anyway and this will blow away the
6183 * old info in the log.
6184 */
6185 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6186
Chris Mason9c583092008-01-29 15:15:18 -05006187 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006188 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006189 key[0].offset = 0;
6190
Chris Mason9c583092008-01-29 15:15:18 -05006191 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006192
6193 if (name) {
6194 /*
6195 * Start new inodes with an inode_ref. This is slightly more
6196 * efficient for small numbers of hard links since they will
6197 * be packed into one item. Extended refs will kick in if we
6198 * add more hard links than can fit in the ref item.
6199 */
6200 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006201 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006202 key[1].offset = ref_objectid;
6203
6204 sizes[1] = name_len + sizeof(*ref);
6205 }
Chris Mason9c583092008-01-29 15:15:18 -05006206
Chris Masonb0d5d102014-09-08 13:08:51 -07006207 location = &BTRFS_I(inode)->location;
6208 location->objectid = objectid;
6209 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006210 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006211
6212 ret = btrfs_insert_inode_locked(inode);
6213 if (ret < 0)
6214 goto fail;
6215
Chris Masonb9473432009-03-13 11:00:37 -04006216 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006217 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006218 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006219 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006220
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006221 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006222 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306223
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006224 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306225 inode->i_atime = inode->i_mtime;
6226 inode->i_ctime = inode->i_mtime;
6227 BTRFS_I(inode)->i_otime = inode->i_mtime;
6228
Chris Mason5f39d392007-10-15 16:14:19 -04006229 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6230 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006231 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006232 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006233 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006234
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006235 if (name) {
6236 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6237 struct btrfs_inode_ref);
6238 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6239 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6240 ptr = (unsigned long)(ref + 1);
6241 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6242 }
Chris Mason9c583092008-01-29 15:15:18 -05006243
Chris Mason5f39d392007-10-15 16:14:19 -04006244 btrfs_mark_buffer_dirty(path->nodes[0]);
6245 btrfs_free_path(path);
6246
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006247 btrfs_inherit_iflags(inode, dir);
6248
Al Viro569254b2011-07-24 17:08:40 -04006249 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006250 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006251 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006252 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006253 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6254 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006255 }
6256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006257 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006258
6259 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006260 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006261
Alexander Block8ea05e32012-07-25 17:35:53 +02006262 btrfs_update_root_times(trans, root);
6263
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006264 ret = btrfs_inode_inherit_props(trans, inode, dir);
6265 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006266 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006267 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006268 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006269
Chris Mason39279cc2007-06-12 06:35:45 -04006270 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006271
6272fail_unlock:
6273 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006274fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006275 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006276 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006277 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006278 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006279 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006280}
6281
6282static inline u8 btrfs_inode_type(struct inode *inode)
6283{
6284 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6285}
6286
Chris Masond352ac62008-09-29 15:18:18 -04006287/*
6288 * utility function to add 'inode' into 'parent_inode' with
6289 * a give name and a given sequence number.
6290 * if 'add_backref' is true, also insert a backref from the
6291 * inode to the parent directory.
6292 */
Chris Masone02119d2008-09-05 16:13:11 -04006293int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006294 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006295 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006296{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006297 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006298 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006299 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006300 struct btrfs_root *root = parent_inode->root;
6301 u64 ino = btrfs_ino(inode);
6302 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006303
Li Zefan33345d012011-04-20 10:31:50 +08006304 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006305 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006306 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006307 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006308 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006309 key.offset = 0;
6310 }
Chris Mason39279cc2007-06-12 06:35:45 -04006311
Li Zefan33345d012011-04-20 10:31:50 +08006312 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006313 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6314 root->root_key.objectid, parent_ino,
6315 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006316 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006317 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6318 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006319 }
6320
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006321 /* Nothing to clean up yet */
6322 if (ret)
6323 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006324
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006325 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6326 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006327 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006328 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006329 goto fail_dir_item;
6330 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006331 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006332 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006333 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006334
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006335 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006336 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006337 inode_inc_iversion(&parent_inode->vfs_inode);
6338 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6339 current_time(&parent_inode->vfs_inode);
6340 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006341 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006342 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006343 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006344
6345fail_dir_item:
6346 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6347 u64 local_index;
6348 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006349 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6350 root->root_key.objectid, parent_ino,
6351 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006352
6353 } else if (add_backref) {
6354 u64 local_index;
6355 int err;
6356
6357 err = btrfs_del_inode_ref(trans, root, name, name_len,
6358 ino, parent_ino, &local_index);
6359 }
6360 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006361}
6362
6363static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006364 struct btrfs_inode *dir, struct dentry *dentry,
6365 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006366{
Josef Bacika1b075d2010-11-19 20:36:11 +00006367 int err = btrfs_add_link(trans, dir, inode,
6368 dentry->d_name.name, dentry->d_name.len,
6369 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006370 if (err > 0)
6371 err = -EEXIST;
6372 return err;
6373}
6374
Josef Bacik618e21d2007-07-11 10:18:17 -04006375static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006376 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006377{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006378 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006379 struct btrfs_trans_handle *trans;
6380 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006381 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006382 int err;
6383 int drop_inode = 0;
6384 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006385 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006386
Josef Bacik9ed74f22009-09-11 16:12:44 -04006387 /*
6388 * 2 for inode item and ref
6389 * 2 for dir items
6390 * 1 for xattr if selinux is on
6391 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006392 trans = btrfs_start_transaction(root, 5);
6393 if (IS_ERR(trans))
6394 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006395
Li Zefan581bb052011-04-20 10:06:11 +08006396 err = btrfs_find_free_ino(root, &objectid);
6397 if (err)
6398 goto out_unlock;
6399
Josef Bacikaec74772008-07-24 12:12:38 -04006400 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006401 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6402 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006403 if (IS_ERR(inode)) {
6404 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006405 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006406 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006407
Casey Schauflerad19db72011-12-15 10:09:07 -05006408 /*
6409 * If the active LSM wants to access the inode during
6410 * d_instantiate it needs these. Smack checks to see
6411 * if the filesystem supports xattrs by looking at the
6412 * ops vector.
6413 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006414 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006415 init_special_inode(inode, inode->i_mode, rdev);
6416
6417 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006418 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006419 goto out_unlock_inode;
6420
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006421 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6422 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006423 if (err) {
6424 goto out_unlock_inode;
6425 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006426 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006427 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006428 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006429 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006430
Josef Bacik618e21d2007-07-11 10:18:17 -04006431out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006432 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006433 btrfs_balance_delayed_items(fs_info);
6434 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006435 if (drop_inode) {
6436 inode_dec_link_count(inode);
6437 iput(inode);
6438 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006439 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006440
6441out_unlock_inode:
6442 drop_inode = 1;
6443 unlock_new_inode(inode);
6444 goto out_unlock;
6445
Josef Bacik618e21d2007-07-11 10:18:17 -04006446}
6447
Chris Mason39279cc2007-06-12 06:35:45 -04006448static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006449 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006450{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006451 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006452 struct btrfs_trans_handle *trans;
6453 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006454 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006455 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006456 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006457 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006458 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006459
Josef Bacik9ed74f22009-09-11 16:12:44 -04006460 /*
6461 * 2 for inode item and ref
6462 * 2 for dir items
6463 * 1 for xattr if selinux is on
6464 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006465 trans = btrfs_start_transaction(root, 5);
6466 if (IS_ERR(trans))
6467 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006468
Li Zefan581bb052011-04-20 10:06:11 +08006469 err = btrfs_find_free_ino(root, &objectid);
6470 if (err)
6471 goto out_unlock;
6472
Josef Bacikaec74772008-07-24 12:12:38 -04006473 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006474 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6475 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006476 if (IS_ERR(inode)) {
6477 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006478 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006479 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006480 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006481 /*
6482 * If the active LSM wants to access the inode during
6483 * d_instantiate it needs these. Smack checks to see
6484 * if the filesystem supports xattrs by looking at the
6485 * ops vector.
6486 */
6487 inode->i_fop = &btrfs_file_operations;
6488 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006489 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006490
6491 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6492 if (err)
6493 goto out_unlock_inode;
6494
6495 err = btrfs_update_inode(trans, root, inode);
6496 if (err)
6497 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006498
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006499 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6500 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006501 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006502 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006503
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006504 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006505 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006506 d_instantiate(dentry, inode);
6507
Chris Mason39279cc2007-06-12 06:35:45 -04006508out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006509 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006510 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006511 inode_dec_link_count(inode);
6512 iput(inode);
6513 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006514 btrfs_balance_delayed_items(fs_info);
6515 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006516 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006517
6518out_unlock_inode:
6519 unlock_new_inode(inode);
6520 goto out_unlock;
6521
Chris Mason39279cc2007-06-12 06:35:45 -04006522}
6523
6524static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6525 struct dentry *dentry)
6526{
Filipe Manana271dba452016-01-05 16:24:05 +00006527 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006528 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006529 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006530 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006531 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006532 int err;
6533 int drop_inode = 0;
6534
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006535 /* do not allow sys_link's with other subvols of the same device */
6536 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006537 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006538
Mark Fashehf1863732012-08-08 11:32:27 -07006539 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006540 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006541
Nikolay Borisov877574e2017-02-20 13:50:33 +02006542 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006543 if (err)
6544 goto fail;
6545
Yan, Zhenga22285a2010-05-16 10:48:46 -04006546 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006547 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006548 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006549 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006550 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006551 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006552 if (IS_ERR(trans)) {
6553 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006554 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006555 goto fail;
6556 }
Chris Mason5f39d392007-10-15 16:14:19 -04006557
Miao Xie67de1172013-12-26 13:07:06 +08006558 /* There are several dir indexes for this inode, clear the cache. */
6559 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006560 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006561 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006562 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006563 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006564 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006565
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006566 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6567 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006568
Yan, Zhenga5719522009-09-24 09:17:31 -04006569 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006570 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006571 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006572 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006573 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006574 if (err)
6575 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006576 if (inode->i_nlink == 1) {
6577 /*
6578 * If new hard link count is 1, it's a file created
6579 * with open(2) O_TMPFILE flag.
6580 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006581 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006582 if (err)
6583 goto fail;
6584 }
Al Viro08c422c2011-12-23 07:58:13 -05006585 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006586 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006587 }
Chris Mason39279cc2007-06-12 06:35:45 -04006588
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006589 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006590fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006591 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006592 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006593 if (drop_inode) {
6594 inode_dec_link_count(inode);
6595 iput(inode);
6596 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006597 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006598 return err;
6599}
6600
Al Viro18bb1db2011-07-26 01:41:39 -04006601static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006602{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006603 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006604 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006605 struct btrfs_trans_handle *trans;
6606 struct btrfs_root *root = BTRFS_I(dir)->root;
6607 int err = 0;
6608 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006609 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006610 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006611
Josef Bacik9ed74f22009-09-11 16:12:44 -04006612 /*
6613 * 2 items for inode and ref
6614 * 2 items for dir items
6615 * 1 for xattr if selinux is on
6616 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006617 trans = btrfs_start_transaction(root, 5);
6618 if (IS_ERR(trans))
6619 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006620
Li Zefan581bb052011-04-20 10:06:11 +08006621 err = btrfs_find_free_ino(root, &objectid);
6622 if (err)
6623 goto out_fail;
6624
Josef Bacikaec74772008-07-24 12:12:38 -04006625 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006626 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6627 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 if (IS_ERR(inode)) {
6629 err = PTR_ERR(inode);
6630 goto out_fail;
6631 }
Chris Mason5f39d392007-10-15 16:14:19 -04006632
Chris Mason39279cc2007-06-12 06:35:45 -04006633 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006634 /* these must be set before we unlock the inode */
6635 inode->i_op = &btrfs_dir_inode_operations;
6636 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006637
Eric Paris2a7dba32011-02-01 11:05:39 -05006638 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006639 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006640 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006641
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006642 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 err = btrfs_update_inode(trans, root, inode);
6644 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006645 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006646
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006647 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6648 dentry->d_name.name,
6649 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006651 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006652
Chris Mason39279cc2007-06-12 06:35:45 -04006653 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006654 /*
6655 * mkdir is special. We're unlocking after we call d_instantiate
6656 * to avoid a race with nfsd calling d_instantiate.
6657 */
6658 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006659 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006660
6661out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006662 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006663 if (drop_on_err) {
6664 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006666 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006667 btrfs_balance_delayed_items(fs_info);
6668 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006669 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006670
6671out_fail_inode:
6672 unlock_new_inode(inode);
6673 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006674}
6675
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006676/* Find next extent map of a given extent map, caller needs to ensure locks */
6677static struct extent_map *next_extent_map(struct extent_map *em)
6678{
6679 struct rb_node *next;
6680
6681 next = rb_next(&em->rb_node);
6682 if (!next)
6683 return NULL;
6684 return container_of(next, struct extent_map, rb_node);
6685}
6686
6687static struct extent_map *prev_extent_map(struct extent_map *em)
6688{
6689 struct rb_node *prev;
6690
6691 prev = rb_prev(&em->rb_node);
6692 if (!prev)
6693 return NULL;
6694 return container_of(prev, struct extent_map, rb_node);
6695}
6696
Chris Masond352ac62008-09-29 15:18:18 -04006697/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006698 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006699 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006700 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006701 */
Chris Mason3b951512008-04-17 11:29:12 -04006702static int merge_extent_mapping(struct extent_map_tree *em_tree,
6703 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006704 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006705 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006706{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006707 struct extent_map *prev;
6708 struct extent_map *next;
6709 u64 start;
6710 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006711 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006712
Chris Masone6dcd2d2008-07-17 12:53:50 -04006713 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006714
6715 if (existing->start > map_start) {
6716 next = existing;
6717 prev = prev_extent_map(next);
6718 } else {
6719 prev = existing;
6720 next = next_extent_map(prev);
6721 }
6722
6723 start = prev ? extent_map_end(prev) : em->start;
6724 start = max_t(u64, start, em->start);
6725 end = next ? next->start : extent_map_end(em);
6726 end = min_t(u64, end, extent_map_end(em));
6727 start_diff = start - em->start;
6728 em->start = start;
6729 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006730 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6731 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006732 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006733 em->block_len -= start_diff;
6734 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006735 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006736}
6737
Chris Masonc8b97812008-10-29 14:49:59 -04006738static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006739 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006740 size_t pg_offset, u64 extent_offset,
6741 struct btrfs_file_extent_item *item)
6742{
6743 int ret;
6744 struct extent_buffer *leaf = path->nodes[0];
6745 char *tmp;
6746 size_t max_size;
6747 unsigned long inline_size;
6748 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006749 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006750
6751 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006752 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006753 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6754 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006755 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006756 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006757 if (!tmp)
6758 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006759 ptr = btrfs_file_extent_inline_start(item);
6760
6761 read_extent_buffer(leaf, tmp, ptr, inline_size);
6762
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006763 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006764 ret = btrfs_decompress(compress_type, tmp, page,
6765 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006766
6767 /*
6768 * decompression code contains a memset to fill in any space between the end
6769 * of the uncompressed data and the end of max_size in case the decompressed
6770 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6771 * the end of an inline extent and the beginning of the next block, so we
6772 * cover that region here.
6773 */
6774
6775 if (max_size + pg_offset < PAGE_SIZE) {
6776 char *map = kmap(page);
6777 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6778 kunmap(page);
6779 }
Chris Masonc8b97812008-10-29 14:49:59 -04006780 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006781 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006782}
6783
Chris Masond352ac62008-09-29 15:18:18 -04006784/*
6785 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006786 * the ugly parts come from merging extents from the disk with the in-ram
6787 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006788 * where the in-ram extents might be locked pending data=ordered completion.
6789 *
6790 * This also copies inline extents directly into the page.
6791 */
Chris Masond3977122009-01-05 21:25:51 -05006792
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006793struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6794 struct page *page,
6795 size_t pg_offset, u64 start, u64 len,
6796 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006797{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006799 int ret;
6800 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006801 u64 extent_start = 0;
6802 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006803 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006804 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006805 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006806 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006807 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006808 struct extent_buffer *leaf;
6809 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006811 struct extent_map_tree *em_tree = &inode->extent_tree;
6812 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006813 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006814 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006815
Chris Masona52d9a82007-08-27 16:49:44 -04006816again:
Chris Mason890871b2009-09-02 16:24:52 -04006817 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006818 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006819 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006820 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006821 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006822
Chris Masona52d9a82007-08-27 16:49:44 -04006823 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006824 if (em->start > start || em->start + em->len <= start)
6825 free_extent_map(em);
6826 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006827 free_extent_map(em);
6828 else
6829 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006830 }
David Sterba172ddd62011-04-21 00:48:27 +02006831 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006832 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006833 err = -ENOMEM;
6834 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006835 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006836 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006837 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006838 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006839 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006840 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006841
6842 if (!path) {
6843 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006844 if (!path) {
6845 err = -ENOMEM;
6846 goto out;
6847 }
6848 /*
6849 * Chances are we'll be called again, so go ahead and do
6850 * readahead
6851 */
David Sterbae4058b52015-11-27 16:31:35 +01006852 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006853 }
6854
Chris Mason179e29e2007-11-01 11:28:41 -04006855 ret = btrfs_lookup_file_extent(trans, root, path,
6856 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006857 if (ret < 0) {
6858 err = ret;
6859 goto out;
6860 }
6861
6862 if (ret != 0) {
6863 if (path->slots[0] == 0)
6864 goto not_found;
6865 path->slots[0]--;
6866 }
6867
Chris Mason5f39d392007-10-15 16:14:19 -04006868 leaf = path->nodes[0];
6869 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006870 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006871 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006872 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006873 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006874 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006875 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006876 /*
6877 * If we backup past the first extent we want to move forward
6878 * and see if there is an extent in front of us, otherwise we'll
6879 * say there is a hole for our whole search range which can
6880 * cause problems.
6881 */
6882 extent_end = start;
6883 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 }
6885
Chris Mason5f39d392007-10-15 16:14:19 -04006886 found_type = btrfs_file_extent_type(leaf, item);
6887 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006888 if (found_type == BTRFS_FILE_EXTENT_REG ||
6889 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006890 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006891 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006892 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6893 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006894 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006895 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006896 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006897 }
Josef Bacik25a50342013-10-14 12:08:38 -04006898next:
Yan Zheng9036c102008-10-30 14:19:41 -04006899 if (start >= extent_end) {
6900 path->slots[0]++;
6901 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6902 ret = btrfs_next_leaf(root, path);
6903 if (ret < 0) {
6904 err = ret;
6905 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 }
Yan Zheng9036c102008-10-30 14:19:41 -04006907 if (ret > 0)
6908 goto not_found;
6909 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006910 }
Yan Zheng9036c102008-10-30 14:19:41 -04006911 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6912 if (found_key.objectid != objectid ||
6913 found_key.type != BTRFS_EXTENT_DATA_KEY)
6914 goto not_found;
6915 if (start + len <= found_key.offset)
6916 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006917 if (start > found_key.offset)
6918 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006919 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006920 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006921 em->len = found_key.offset - start;
6922 goto not_found_em;
6923 }
6924
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006925 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006926 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006927
Yan Zhengd899e052008-10-30 14:25:28 -04006928 if (found_type == BTRFS_FILE_EXTENT_REG ||
6929 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006930 goto insert;
6931 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006932 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006934 size_t size;
6935 size_t extent_offset;
6936 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006937
Filipe Manana7ffbb592014-06-09 03:48:05 +01006938 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006939 goto out;
Yan689f9342007-10-29 11:41:07 -04006940
Chris Mason514ac8a2014-01-03 21:07:00 -08006941 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006942 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006943 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6944 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006945 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006946 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006947 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006948 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006949 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006950 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006951 if (btrfs_file_extent_compression(leaf, item) !=
6952 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006953 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006954 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006955 if (ret) {
6956 err = ret;
6957 goto out;
6958 }
Chris Masonc8b97812008-10-29 14:49:59 -04006959 } else {
6960 map = kmap(page);
6961 read_extent_buffer(leaf, map + pg_offset, ptr,
6962 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006963 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006964 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006965 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006966 copy_size);
6967 }
Chris Masonc8b97812008-10-29 14:49:59 -04006968 kunmap(page);
6969 }
Chris Mason179e29e2007-11-01 11:28:41 -04006970 flush_dcache_page(page);
6971 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006972 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006973 if (!trans) {
6974 kunmap(page);
6975 free_extent_map(em);
6976 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006977
David Sterbab3b4aa72011-04-21 01:20:15 +02006978 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006979 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006980
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006981 if (IS_ERR(trans))
6982 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006983 goto again;
6984 }
Chris Masonc8b97812008-10-29 14:49:59 -04006985 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006986 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006987 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006988 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006989 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Masond1310b22008-01-24 16:13:08 -05006991 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006992 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006993 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006994 }
6995not_found:
6996 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006997 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006998 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006999not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007000 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007001 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007002insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007003 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007004 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007005 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007006 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7007 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007008 err = -EIO;
7009 goto out;
7010 }
Chris Masond1310b22008-01-24 16:13:08 -05007011
7012 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007013 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007014 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007015 /* it is possible that someone inserted the extent into the tree
7016 * while we had the lock dropped. It is also possible that
7017 * an overlapping map exists in the tree
7018 */
Chris Masona52d9a82007-08-27 16:49:44 -04007019 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007020 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007021
7022 ret = 0;
7023
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007024 existing = search_extent_mapping(em_tree, start, len);
7025 /*
7026 * existing will always be non-NULL, since there must be
7027 * extent causing the -EEXIST.
7028 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007029 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007030 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007031 em->block_start == existing->block_start) {
7032 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007033 * The existing extent map already encompasses the
7034 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007035 */
7036 free_extent_map(em);
7037 em = existing;
7038 err = 0;
7039
7040 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007041 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007042 /*
7043 * The existing extent map is the one nearest to
7044 * the [start, start + len) range which overlaps
7045 */
7046 err = merge_extent_mapping(em_tree, existing,
7047 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007048 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007049 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007050 free_extent_map(em);
7051 em = NULL;
7052 }
7053 } else {
7054 free_extent_map(em);
7055 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007056 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007057 }
Chris Masona52d9a82007-08-27 16:49:44 -04007058 }
Chris Mason890871b2009-09-02 16:24:52 -04007059 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007060out:
liubo1abe9b82011-03-24 11:18:59 +00007061
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007062 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007063
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007064 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007065 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007066 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007067 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007068 err = ret;
7069 }
Chris Masona52d9a82007-08-27 16:49:44 -04007070 if (err) {
7071 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007072 return ERR_PTR(err);
7073 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007074 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007075 return em;
7076}
7077
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007078struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7079 struct page *page,
7080 size_t pg_offset, u64 start, u64 len,
7081 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007082{
7083 struct extent_map *em;
7084 struct extent_map *hole_em = NULL;
7085 u64 range_start = start;
7086 u64 end;
7087 u64 found;
7088 u64 found_end;
7089 int err = 0;
7090
7091 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7092 if (IS_ERR(em))
7093 return em;
7094 if (em) {
7095 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007096 * if our em maps to
7097 * - a hole or
7098 * - a pre-alloc extent,
7099 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007100 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007101 if (em->block_start != EXTENT_MAP_HOLE &&
7102 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007103 return em;
7104 else
7105 hole_em = em;
7106 }
7107
7108 /* check to see if we've wrapped (len == -1 or similar) */
7109 end = start + len;
7110 if (end < start)
7111 end = (u64)-1;
7112 else
7113 end -= 1;
7114
7115 em = NULL;
7116
7117 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007118 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007119 end, len, EXTENT_DELALLOC, 1);
7120 found_end = range_start + found;
7121 if (found_end < range_start)
7122 found_end = (u64)-1;
7123
7124 /*
7125 * we didn't find anything useful, return
7126 * the original results from get_extent()
7127 */
7128 if (range_start > end || found_end <= start) {
7129 em = hole_em;
7130 hole_em = NULL;
7131 goto out;
7132 }
7133
7134 /* adjust the range_start to make sure it doesn't
7135 * go backwards from the start they passed in
7136 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307137 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007138 found = found_end - range_start;
7139
7140 if (found > 0) {
7141 u64 hole_start = start;
7142 u64 hole_len = len;
7143
David Sterba172ddd62011-04-21 00:48:27 +02007144 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007145 if (!em) {
7146 err = -ENOMEM;
7147 goto out;
7148 }
7149 /*
7150 * when btrfs_get_extent can't find anything it
7151 * returns one huge hole
7152 *
7153 * make sure what it found really fits our range, and
7154 * adjust to make sure it is based on the start from
7155 * the caller
7156 */
7157 if (hole_em) {
7158 u64 calc_end = extent_map_end(hole_em);
7159
7160 if (calc_end <= start || (hole_em->start > end)) {
7161 free_extent_map(hole_em);
7162 hole_em = NULL;
7163 } else {
7164 hole_start = max(hole_em->start, start);
7165 hole_len = calc_end - hole_start;
7166 }
7167 }
7168 em->bdev = NULL;
7169 if (hole_em && range_start > hole_start) {
7170 /* our hole starts before our delalloc, so we
7171 * have to return just the parts of the hole
7172 * that go until the delalloc starts
7173 */
7174 em->len = min(hole_len,
7175 range_start - hole_start);
7176 em->start = hole_start;
7177 em->orig_start = hole_start;
7178 /*
7179 * don't adjust block start at all,
7180 * it is fixed at EXTENT_MAP_HOLE
7181 */
7182 em->block_start = hole_em->block_start;
7183 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007184 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7185 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007186 } else {
7187 em->start = range_start;
7188 em->len = found;
7189 em->orig_start = range_start;
7190 em->block_start = EXTENT_MAP_DELALLOC;
7191 em->block_len = found;
7192 }
7193 } else if (hole_em) {
7194 return hole_em;
7195 }
7196out:
7197
7198 free_extent_map(hole_em);
7199 if (err) {
7200 free_extent_map(em);
7201 return ERR_PTR(err);
7202 }
7203 return em;
7204}
7205
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007206static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7207 const u64 start,
7208 const u64 len,
7209 const u64 orig_start,
7210 const u64 block_start,
7211 const u64 block_len,
7212 const u64 orig_block_len,
7213 const u64 ram_bytes,
7214 const int type)
7215{
7216 struct extent_map *em = NULL;
7217 int ret;
7218
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007219 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007220 em = create_io_em(inode, start, len, orig_start,
7221 block_start, block_len, orig_block_len,
7222 ram_bytes,
7223 BTRFS_COMPRESS_NONE, /* compress_type */
7224 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007225 if (IS_ERR(em))
7226 goto out;
7227 }
7228 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7229 len, block_len, type);
7230 if (ret) {
7231 if (em) {
7232 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007233 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007234 start + len - 1, 0);
7235 }
7236 em = ERR_PTR(ret);
7237 }
7238 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007239
7240 return em;
7241}
7242
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7244 u64 start, u64 len)
7245{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007246 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007248 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 struct btrfs_key ins;
7250 u64 alloc_hint;
7251 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007254 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007255 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007256 if (ret)
7257 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007259 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7260 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007261 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007262 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007263 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007264 btrfs_free_reserved_extent(fs_info, ins.objectid,
7265 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007266
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 return em;
7268}
7269
Chris Mason46bfbb52010-05-26 11:04:10 -04007270/*
7271 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7272 * block must be cow'd
7273 */
Josef Bacik00361582013-08-14 14:02:47 -04007274noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007275 u64 *orig_start, u64 *orig_block_len,
7276 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007277{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007279 struct btrfs_path *path;
7280 int ret;
7281 struct extent_buffer *leaf;
7282 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007283 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007284 struct btrfs_file_extent_item *fi;
7285 struct btrfs_key key;
7286 u64 disk_bytenr;
7287 u64 backref_offset;
7288 u64 extent_end;
7289 u64 num_bytes;
7290 int slot;
7291 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007292 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007293
Chris Mason46bfbb52010-05-26 11:04:10 -04007294 path = btrfs_alloc_path();
7295 if (!path)
7296 return -ENOMEM;
7297
David Sterbaf85b7372017-01-20 14:54:07 +01007298 ret = btrfs_lookup_file_extent(NULL, root, path,
7299 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007300 if (ret < 0)
7301 goto out;
7302
7303 slot = path->slots[0];
7304 if (ret == 1) {
7305 if (slot == 0) {
7306 /* can't find the item, must cow */
7307 ret = 0;
7308 goto out;
7309 }
7310 slot--;
7311 }
7312 ret = 0;
7313 leaf = path->nodes[0];
7314 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007315 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 key.type != BTRFS_EXTENT_DATA_KEY) {
7317 /* not our file or wrong item type, must cow */
7318 goto out;
7319 }
7320
7321 if (key.offset > offset) {
7322 /* Wrong offset, must cow */
7323 goto out;
7324 }
7325
7326 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7327 found_type = btrfs_file_extent_type(leaf, fi);
7328 if (found_type != BTRFS_FILE_EXTENT_REG &&
7329 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7330 /* not a regular extent, must cow */
7331 goto out;
7332 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007333
7334 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7335 goto out;
7336
Miao Xiee77751a2013-12-27 21:11:50 +08007337 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7338 if (extent_end <= offset)
7339 goto out;
7340
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007342 if (disk_bytenr == 0)
7343 goto out;
7344
7345 if (btrfs_file_extent_compression(leaf, fi) ||
7346 btrfs_file_extent_encryption(leaf, fi) ||
7347 btrfs_file_extent_other_encoding(leaf, fi))
7348 goto out;
7349
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 backref_offset = btrfs_file_extent_offset(leaf, fi);
7351
Josef Bacik7ee9e442013-06-21 16:37:03 -04007352 if (orig_start) {
7353 *orig_start = key.offset - backref_offset;
7354 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7355 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7356 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007357
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007358 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007359 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007360
7361 num_bytes = min(offset + *len, extent_end) - offset;
7362 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7363 u64 range_end;
7364
Jeff Mahoneyda170662016-06-15 09:22:56 -04007365 range_end = round_up(offset + num_bytes,
7366 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007367 ret = test_range_bit(io_tree, offset, range_end,
7368 EXTENT_DELALLOC, 0, NULL);
7369 if (ret) {
7370 ret = -EAGAIN;
7371 goto out;
7372 }
7373 }
7374
Josef Bacik1bda19e2013-10-18 12:10:36 -04007375 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007376
7377 /*
7378 * look for other files referencing this extent, if we
7379 * find any we must cow
7380 */
Josef Bacik00361582013-08-14 14:02:47 -04007381
Liu Boe4c3b2d2017-01-30 12:25:28 -08007382 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007383 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007384 if (ret) {
7385 ret = 0;
7386 goto out;
7387 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007388
7389 /*
7390 * adjust disk_bytenr and num_bytes to cover just the bytes
7391 * in this extent we are about to write. If there
7392 * are any csums in that range we have to cow in order
7393 * to keep the csums correct
7394 */
7395 disk_bytenr += backref_offset;
7396 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007397 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7398 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007399 /*
7400 * all of the above have passed, it is safe to overwrite this extent
7401 * without cow
7402 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007403 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007404 ret = 1;
7405out:
7406 btrfs_free_path(path);
7407 return ret;
7408}
7409
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007410bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7411{
7412 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7413 int found = false;
7414 void **pagep = NULL;
7415 struct page *page = NULL;
7416 int start_idx;
7417 int end_idx;
7418
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007419 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007420
7421 /*
7422 * end is the last byte in the last page. end == start is legal
7423 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007424 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007425
7426 rcu_read_lock();
7427
7428 /* Most of the code in this while loop is lifted from
7429 * find_get_page. It's been modified to begin searching from a
7430 * page and return just the first page found in that range. If the
7431 * found idx is less than or equal to the end idx then we know that
7432 * a page exists. If no pages are found or if those pages are
7433 * outside of the range then we're fine (yay!) */
7434 while (page == NULL &&
7435 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7436 page = radix_tree_deref_slot(pagep);
7437 if (unlikely(!page))
7438 break;
7439
7440 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007441 if (radix_tree_deref_retry(page)) {
7442 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007443 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007444 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007445 /*
7446 * Otherwise, shmem/tmpfs must be storing a swap entry
7447 * here as an exceptional entry: so return it without
7448 * attempting to raise page count.
7449 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007450 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007451 break; /* TODO: Is this relevant for this use case? */
7452 }
7453
Filipe Manana91405152014-06-05 13:22:24 +01007454 if (!page_cache_get_speculative(page)) {
7455 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007456 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007457 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007458
7459 /*
7460 * Has the page moved?
7461 * This is part of the lockless pagecache protocol. See
7462 * include/linux/pagemap.h for details.
7463 */
7464 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007465 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007466 page = NULL;
7467 }
7468 }
7469
7470 if (page) {
7471 if (page->index <= end_idx)
7472 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007473 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007474 }
7475
7476 rcu_read_unlock();
7477 return found;
7478}
7479
Josef Bacikeb838e72012-07-31 16:28:48 -04007480static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7481 struct extent_state **cached_state, int writing)
7482{
7483 struct btrfs_ordered_extent *ordered;
7484 int ret = 0;
7485
7486 while (1) {
7487 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007488 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007489 /*
7490 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007491 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007492 * extents in this range.
7493 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007494 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 lockend - lockstart + 1);
7496
7497 /*
7498 * We need to make sure there are no buffered pages in this
7499 * range either, we could have raced between the invalidate in
7500 * generic_file_direct_write and locking the extent. The
7501 * invalidate needs to happen so that reads after a write do not
7502 * get stale data.
7503 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007504 if (!ordered &&
7505 (!writing ||
7506 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007507 break;
7508
7509 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7510 cached_state, GFP_NOFS);
7511
7512 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007513 /*
7514 * If we are doing a DIO read and the ordered extent we
7515 * found is for a buffered write, we can not wait for it
7516 * to complete and retry, because if we do so we can
7517 * deadlock with concurrent buffered writes on page
7518 * locks. This happens only if our DIO read covers more
7519 * than one extent map, if at this point has already
7520 * created an ordered extent for a previous extent map
7521 * and locked its range in the inode's io tree, and a
7522 * concurrent write against that previous extent map's
7523 * range and this range started (we unlock the ranges
7524 * in the io tree only when the bios complete and
7525 * buffered writes always lock pages before attempting
7526 * to lock range in the io tree).
7527 */
7528 if (writing ||
7529 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7530 btrfs_start_ordered_extent(inode, ordered, 1);
7531 else
7532 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 btrfs_put_ordered_extent(ordered);
7534 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007536 * We could trigger writeback for this range (and wait
7537 * for it to complete) and then invalidate the pages for
7538 * this range (through invalidate_inode_pages2_range()),
7539 * but that can lead us to a deadlock with a concurrent
7540 * call to readpages() (a buffered read or a defrag call
7541 * triggered a readahead) on a page lock due to an
7542 * ordered dio extent we created before but did not have
7543 * yet a corresponding bio submitted (whence it can not
7544 * complete), which makes readpages() wait for that
7545 * ordered extent to complete while holding a lock on
7546 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007548 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 }
7550
Filipe Mananaade77022016-02-18 14:28:55 +00007551 if (ret)
7552 break;
7553
Josef Bacikeb838e72012-07-31 16:28:48 -04007554 cond_resched();
7555 }
7556
7557 return ret;
7558}
7559
Liu Bo6f9994d2017-01-31 07:50:22 -08007560/* The callers of this must take lock_extent() */
7561static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7562 u64 orig_start, u64 block_start,
7563 u64 block_len, u64 orig_block_len,
7564 u64 ram_bytes, int compress_type,
7565 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007566{
7567 struct extent_map_tree *em_tree;
7568 struct extent_map *em;
7569 struct btrfs_root *root = BTRFS_I(inode)->root;
7570 int ret;
7571
Liu Bo6f9994d2017-01-31 07:50:22 -08007572 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7573 type == BTRFS_ORDERED_COMPRESSED ||
7574 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007575 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007576
Josef Bacik69ffb542012-09-11 15:40:07 -04007577 em_tree = &BTRFS_I(inode)->extent_tree;
7578 em = alloc_extent_map();
7579 if (!em)
7580 return ERR_PTR(-ENOMEM);
7581
7582 em->start = start;
7583 em->orig_start = orig_start;
7584 em->len = len;
7585 em->block_len = block_len;
7586 em->block_start = block_start;
7587 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007588 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007589 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007590 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007591 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007592 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007593 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007594 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007595 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7596 em->compress_type = compress_type;
7597 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007598
7599 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007600 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007601 em->start + em->len - 1, 0);
7602 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007603 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007604 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007605 /*
7606 * The caller has taken lock_extent(), who could race with us
7607 * to add em?
7608 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 } while (ret == -EEXIST);
7610
7611 if (ret) {
7612 free_extent_map(em);
7613 return ERR_PTR(ret);
7614 }
7615
Liu Bo6f9994d2017-01-31 07:50:22 -08007616 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007617 return em;
7618}
7619
Filipe Manana9c9464c2015-11-04 09:52:04 +00007620static void adjust_dio_outstanding_extents(struct inode *inode,
7621 struct btrfs_dio_data *dio_data,
7622 const u64 len)
7623{
David Sterba823bb202017-01-04 11:09:51 +01007624 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007625
Filipe Manana9c9464c2015-11-04 09:52:04 +00007626 /*
7627 * If we have an outstanding_extents count still set then we're
7628 * within our reservation, otherwise we need to adjust our inode
7629 * counter appropriately.
7630 */
Liu Boc2931662016-12-22 17:13:54 -08007631 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007632 dio_data->outstanding_extents -= num_extents;
7633 } else {
Liu Boc2931662016-12-22 17:13:54 -08007634 /*
7635 * If dio write length has been split due to no large enough
7636 * contiguous space, we need to compensate our inode counter
7637 * appropriately.
7638 */
7639 u64 num_needed = num_extents - dio_data->outstanding_extents;
7640
Filipe Manana9c9464c2015-11-04 09:52:04 +00007641 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007642 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007643 spin_unlock(&BTRFS_I(inode)->lock);
7644 }
7645}
7646
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7648 struct buffer_head *bh_result, int create)
7649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007652 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307653 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007658 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007659
Miao Xie172a5042013-02-21 02:48:22 -07007660 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007661 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007662 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007663 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007664
Josef Bacikc3298612012-08-03 16:49:19 -04007665 lockstart = start;
7666 lockend = start + len - 1;
7667
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007668 if (current->journal_info) {
7669 /*
7670 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007671 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007672 * confused.
7673 */
chandan50745b02015-08-28 21:10:13 +05307674 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007675 current->journal_info = NULL;
7676 }
7677
Josef Bacikeb838e72012-07-31 16:28:48 -04007678 /*
7679 * If this errors out it's because we couldn't invalidate pagecache for
7680 * this range and we need to fallback to buffered.
7681 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007682 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7683 create)) {
7684 ret = -ENOTBLK;
7685 goto err;
7686 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007687
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007688 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007689 if (IS_ERR(em)) {
7690 ret = PTR_ERR(em);
7691 goto unlock_err;
7692 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007693
7694 /*
7695 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7696 * io. INLINE is special, and we could probably kludge it in here, but
7697 * it's still buffered so for safety lets just fall back to the generic
7698 * buffered path.
7699 *
7700 * For COMPRESSED we _have_ to read the entire extent in so we can
7701 * decompress it, so there will be buffering required no matter what we
7702 * do, so go ahead and fallback to buffered.
7703 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007704 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007705 * to buffered IO. Don't blame me, this is the price we pay for using
7706 * the generic code.
7707 */
7708 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7709 em->block_start == EXTENT_MAP_INLINE) {
7710 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007711 ret = -ENOTBLK;
7712 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007713 }
7714
7715 /* Just a good old fashioned hole, return */
7716 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7717 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7718 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 }
7721
7722 /*
7723 * We don't allocate a new extent in the following cases
7724 *
7725 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7726 * existing extent.
7727 * 2) The extent is marked as PREALLOC. We're good to go here and can
7728 * just use the extent.
7729 *
7730 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007731 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007732 len = min(len, em->len - (start - em->start));
7733 lockstart = start + len;
7734 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007735 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736
7737 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7738 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7739 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007741 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742
7743 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7744 type = BTRFS_ORDERED_PREALLOC;
7745 else
7746 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007747 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007748 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007749
Josef Bacik00361582013-08-14 14:02:47 -04007750 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007751 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007752 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007753 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007754
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007755 em2 = btrfs_create_dio_extent(inode, start, len,
7756 orig_start, block_start,
7757 len, orig_block_len,
7758 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007759 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007760 if (type == BTRFS_ORDERED_PREALLOC) {
7761 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007762 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007763 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007764 if (em2 && IS_ERR(em2)) {
7765 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007767 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007768 /*
7769 * For inode marked NODATACOW or extent marked PREALLOC,
7770 * use the existing or preallocated extent, so does not
7771 * need to adjust btrfs_space_info's bytes_may_use.
7772 */
7773 btrfs_free_reserved_data_space_noquota(inode,
7774 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007775 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007776 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007777 }
Josef Bacik00361582013-08-14 14:02:47 -04007778
Chris Mason46bfbb52010-05-26 11:04:10 -04007779 /*
7780 * this will cow the extent, reset the len in case we changed
7781 * it above
7782 */
7783 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007784 free_extent_map(em);
7785 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007786 if (IS_ERR(em)) {
7787 ret = PTR_ERR(em);
7788 goto unlock_err;
7789 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007790 len = min(len, em->len - (start - em->start));
7791unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007792 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7793 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007794 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007795 bh_result->b_bdev = em->bdev;
7796 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007797 if (create) {
7798 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7799 set_buffer_new(bh_result);
7800
7801 /*
7802 * Need to update the i_size under the extent lock so buffered
7803 * readers will get the updated i_size when we unlock.
7804 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007805 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007806 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007807
Filipe Manana9c9464c2015-11-04 09:52:04 +00007808 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307809 WARN_ON(dio_data->reserve < len);
7810 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007811 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307812 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007813 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814
Josef Bacikeb838e72012-07-31 16:28:48 -04007815 /*
7816 * In the case of write we need to clear and unlock the entire range,
7817 * in the case of read we need to unlock only the end area that we
7818 * aren't using if there is any left over space.
7819 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007820 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007821 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7822 lockend, unlock_bits, 1, 0,
7823 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007824 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007825 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007826 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007827
Josef Bacik4b46fce2010-05-23 11:00:55 -04007828 free_extent_map(em);
7829
7830 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007831
7832unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007833 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7834 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007835err:
chandan50745b02015-08-28 21:10:13 +05307836 if (dio_data)
7837 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007838 /*
7839 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7840 * write less data then expected, so that we don't underflow our inode's
7841 * outstanding extents counter.
7842 */
7843 if (create && dio_data)
7844 adjust_dio_outstanding_extents(inode, dio_data, len);
7845
Josef Bacikeb838e72012-07-31 16:28:48 -04007846 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847}
7848
Miao Xie8b110e32014-09-12 18:44:03 +08007849static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007850 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007851{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007853 int ret;
7854
Mike Christie37226b22016-06-05 14:31:52 -05007855 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007856
7857 bio_get(bio);
7858
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007859 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007860 if (ret)
7861 goto err;
7862
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007863 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007864err:
7865 bio_put(bio);
7866 return ret;
7867}
7868
7869static int btrfs_check_dio_repairable(struct inode *inode,
7870 struct bio *failed_bio,
7871 struct io_failure_record *failrec,
7872 int failed_mirror)
7873{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 int num_copies;
7876
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007877 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007878 if (num_copies == 1) {
7879 /*
7880 * we only have a single copy of the data, so don't bother with
7881 * all the retry and error correction code that follows. no
7882 * matter what the error is, it is very likely to persist.
7883 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007884 btrfs_debug(fs_info,
7885 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7886 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 return 0;
7888 }
7889
7890 failrec->failed_mirror = failed_mirror;
7891 failrec->this_mirror++;
7892 if (failrec->this_mirror == failed_mirror)
7893 failrec->this_mirror++;
7894
7895 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007896 btrfs_debug(fs_info,
7897 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7898 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007899 return 0;
7900 }
7901
7902 return 1;
7903}
7904
7905static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906 struct page *page, unsigned int pgoff,
7907 u64 start, u64 end, int failed_mirror,
7908 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007909{
7910 struct io_failure_record *failrec;
7911 struct bio *bio;
7912 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007913 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007914 int ret;
7915
Mike Christie37226b22016-06-05 14:31:52 -05007916 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007917
7918 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7919 if (ret)
7920 return ret;
7921
7922 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7923 failed_mirror);
7924 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007925 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007926 return -EIO;
7927 }
7928
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 if ((failed_bio->bi_vcnt > 1)
7930 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007931 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007932 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007933
7934 isector = start - btrfs_io_bio(failed_bio)->logical;
7935 isector >>= inode->i_sb->s_blocksize_bits;
7936 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007938 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007939 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007940 return -EIO;
7941 }
Mike Christie37226b22016-06-05 14:31:52 -05007942 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007943
7944 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7945 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7946 read_mode, failrec->this_mirror, failrec->in_validation);
7947
Mike Christie81a75f672016-06-05 14:31:54 -05007948 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007949 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 bio_put(bio);
7952 }
7953
7954 return ret;
7955}
7956
7957struct btrfs_retry_complete {
7958 struct completion done;
7959 struct inode *inode;
7960 u64 start;
7961 int uptodate;
7962};
7963
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007964static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007965{
7966 struct btrfs_retry_complete *done = bio->bi_private;
7967 struct bio_vec *bvec;
7968 int i;
7969
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007970 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007971 goto end;
7972
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307973 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07007974 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975
Miao Xie8b110e32014-09-12 18:44:03 +08007976 done->uptodate = 1;
7977 bio_for_each_segment_all(bvec, bio, i)
Liu Bo2e949b02017-04-05 14:04:19 -07007978 clean_io_failure(BTRFS_I(done->inode), done->start,
7979 bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007980end:
7981 complete(&done->done);
7982 bio_put(bio);
7983}
7984
7985static int __btrfs_correct_data_nocsum(struct inode *inode,
7986 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007989 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007990 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007991 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 unsigned int pgoff;
7993 u32 sectorsize;
7994 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007995 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007996 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007997
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007999 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000
Miao Xiec1dc0892014-09-12 18:43:56 +08008001 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008002 done.inode = inode;
8003
8004 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8006 pgoff = bvec->bv_offset;
8007
8008next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008009 done.uptodate = 0;
8010 done.start = start;
8011 init_completion(&done.done);
8012
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8014 pgoff, start, start + sectorsize - 1,
8015 io_bio->mirror_num,
8016 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 if (ret)
8018 return ret;
8019
8020 wait_for_completion(&done.done);
8021
8022 if (!done.uptodate) {
8023 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 }
8026
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 start += sectorsize;
8028
Liu Bo97bf5a52017-04-07 13:11:10 -07008029 nr_sectors--;
8030 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008032 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 goto next_block_or_try_again;
8034 }
Miao Xie8b110e32014-09-12 18:44:03 +08008035 }
8036
8037 return 0;
8038}
8039
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008040static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008041{
8042 struct btrfs_retry_complete *done = bio->bi_private;
8043 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8044 struct bio_vec *bvec;
8045 int uptodate;
8046 int ret;
8047 int i;
8048
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008049 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008050 goto end;
8051
8052 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308054 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008055 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056
Miao Xie8b110e32014-09-12 18:44:03 +08008057 bio_for_each_segment_all(bvec, bio, i) {
8058 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308059 bvec->bv_page, bvec->bv_offset,
8060 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008061 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02008062 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008064 else
8065 uptodate = 0;
8066 }
8067
8068 done->uptodate = uptodate;
8069end:
8070 complete(&done->done);
8071 bio_put(bio);
8072}
8073
8074static int __btrfs_subio_endio_read(struct inode *inode,
8075 struct btrfs_io_bio *io_bio, int err)
8076{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008078 struct bio_vec *bvec;
8079 struct btrfs_retry_complete done;
8080 u64 start;
8081 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 u32 sectorsize;
8083 int nr_sectors;
8084 unsigned int pgoff;
8085 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008086 int i;
8087 int ret;
8088
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008090 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091
Miao Xie8b110e32014-09-12 18:44:03 +08008092 err = 0;
8093 start = io_bio->logical;
8094 done.inode = inode;
8095
Miao Xiec1dc0892014-09-12 18:43:56 +08008096 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8098
8099 pgoff = bvec->bv_offset;
8100next_block:
8101 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8102 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8103 bvec->bv_page, pgoff, start,
8104 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008105 if (likely(!ret))
8106 goto next;
8107try_again:
8108 done.uptodate = 0;
8109 done.start = start;
8110 init_completion(&done.done);
8111
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308112 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8113 pgoff, start, start + sectorsize - 1,
8114 io_bio->mirror_num,
8115 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008116 if (ret) {
8117 err = ret;
8118 goto next;
8119 }
8120
8121 wait_for_completion(&done.done);
8122
8123 if (!done.uptodate) {
8124 /* We might have another mirror, so try again */
8125 goto try_again;
8126 }
8127next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308128 offset += sectorsize;
8129 start += sectorsize;
8130
8131 ASSERT(nr_sectors);
8132
Liu Bo97bf5a52017-04-07 13:11:10 -07008133 nr_sectors--;
8134 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308135 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008136 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 goto next_block;
8138 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008139 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008140
8141 return err;
8142}
8143
Miao Xie8b110e32014-09-12 18:44:03 +08008144static int btrfs_subio_endio_read(struct inode *inode,
8145 struct btrfs_io_bio *io_bio, int err)
8146{
8147 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8148
8149 if (skip_csum) {
8150 if (unlikely(err))
8151 return __btrfs_correct_data_nocsum(inode, io_bio);
8152 else
8153 return 0;
8154 } else {
8155 return __btrfs_subio_endio_read(inode, io_bio, err);
8156 }
8157}
8158
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008159static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008160{
8161 struct btrfs_dio_private *dip = bio->bi_private;
8162 struct inode *inode = dip->inode;
8163 struct bio *dio_bio;
8164 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008165 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008166
Miao Xie8b110e32014-09-12 18:44:03 +08008167 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8168 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008169
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008171 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008172 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008173
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008175
Filipe Manana1636d1d2016-02-15 16:20:26 +00008176 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008177 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008178
8179 if (io_bio->end_io)
8180 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008181 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008182}
8183
Qu Wenruo524272602017-03-08 10:25:52 +08008184static void __endio_write_update_ordered(struct inode *inode,
8185 const u64 offset, const u64 bytes,
8186 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008190 struct btrfs_workqueue *wq;
8191 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008192 u64 ordered_offset = offset;
8193 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194 int ret;
8195
Qu Wenruo524272602017-03-08 10:25:52 +08008196 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8197 wq = fs_info->endio_freespace_worker;
8198 func = btrfs_freespace_write_helper;
8199 } else {
8200 wq = fs_info->endio_write_workers;
8201 func = btrfs_endio_write_helper;
8202 }
8203
Chris Mason163cf092010-11-28 19:56:33 -05008204again:
8205 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8206 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008207 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008208 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008210 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211
Qu Wenruo524272602017-03-08 10:25:52 +08008212 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8213 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008214out_test:
8215 /*
8216 * our bio might span multiple ordered extents. If we haven't
8217 * completed the accounting for the whole dio, go back and try again
8218 */
Filipe Manana14543772015-11-24 16:23:54 +00008219 if (ordered_offset < offset + bytes) {
8220 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008221 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008222 goto again;
8223 }
Filipe Manana14543772015-11-24 16:23:54 +00008224}
8225
8226static void btrfs_endio_direct_write(struct bio *bio)
8227{
8228 struct btrfs_dio_private *dip = bio->bi_private;
8229 struct bio *dio_bio = dip->dio_bio;
8230
Qu Wenruo524272602017-03-08 10:25:52 +08008231 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8232 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233
Josef Bacik4b46fce2010-05-23 11:00:55 -04008234 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008235
Filipe Manana1636d1d2016-02-15 16:20:26 +00008236 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008237 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008238 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008239}
8240
Mike Christie81a75f672016-06-05 14:31:54 -05008241static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008242 struct bio *bio, int mirror_num,
8243 unsigned long bio_flags, u64 offset)
8244{
8245 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008246 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008247 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008248 return 0;
8249}
8250
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008251static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008252{
8253 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008254 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008255
Miao Xie8b110e32014-09-12 18:44:03 +08008256 if (err)
8257 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008258 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008259 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8260 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008261 (unsigned long long)bio->bi_iter.bi_sector,
8262 bio->bi_iter.bi_size, err);
8263
8264 if (dip->subio_endio)
8265 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008266
8267 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008268 dip->errors = 1;
8269
8270 /*
8271 * before atomic variable goto zero, we must make sure
8272 * dip->errors is perceived to be set.
8273 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008274 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008275 }
8276
8277 /* if there are more bios still pending for this dio, just exit */
8278 if (!atomic_dec_and_test(&dip->pending_bios))
8279 goto out;
8280
Chris Mason9be33952013-05-17 18:30:14 -04008281 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008282 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008283 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008284 dip->dio_bio->bi_error = 0;
8285 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008286 }
8287out:
8288 bio_put(bio);
8289}
8290
8291static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8292 u64 first_sector, gfp_t gfp_flags)
8293{
Chris Masonda2f0f72015-07-02 13:57:22 -07008294 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008295 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008296 if (bio)
8297 bio_associate_current(bio);
8298 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008299}
8300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008301static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008302 struct btrfs_dio_private *dip,
8303 struct bio *bio,
8304 u64 file_offset)
8305{
8306 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8307 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8308 int ret;
8309
8310 /*
8311 * We load all the csum data we need when we submit
8312 * the first bio to reduce the csum tree search and
8313 * contention.
8314 */
8315 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008316 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008317 file_offset);
8318 if (ret)
8319 return ret;
8320 }
8321
8322 if (bio == dip->orig_bio)
8323 return 0;
8324
8325 file_offset -= dip->logical_offset;
8326 file_offset >>= inode->i_sb->s_blocksize_bits;
8327 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8328
8329 return 0;
8330}
8331
Miao Xiee65e1532010-11-22 03:04:43 +00008332static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008333 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008334 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008336 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008337 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008338 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008339 int ret;
8340
Josef Bacikb812ce22012-11-16 13:56:32 -05008341 if (async_submit)
8342 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8343
Miao Xiee65e1532010-11-22 03:04:43 +00008344 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008345
8346 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008347 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008348 if (ret)
8349 goto err;
8350 }
Miao Xiee65e1532010-11-22 03:04:43 +00008351
Josef Bacik1ae39932011-04-06 14:41:34 -04008352 if (skip_sum)
8353 goto map;
8354
8355 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008356 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8357 file_offset,
8358 __btrfs_submit_bio_start_direct_io,
8359 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008360 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008361 } else if (write) {
8362 /*
8363 * If we aren't doing async submit, calculate the csum of the
8364 * bio now.
8365 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008366 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008367 if (ret)
8368 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008369 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008370 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008371 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008372 if (ret)
8373 goto err;
8374 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008375map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008376 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008377err:
8378 bio_put(bio);
8379 return ret;
8380}
8381
Mike Christie81a75f672016-06-05 14:31:54 -05008382static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008383 int skip_sum)
8384{
8385 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008387 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008388 struct bio *bio;
8389 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008390 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008391 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008392 u64 file_offset = dip->logical_offset;
8393 u64 submit_len = 0;
8394 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008395 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008396 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308397 int nr_sectors;
8398 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008399 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008400
Kent Overstreet4f024f32013-10-11 15:44:27 -07008401 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008402 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8403 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008404 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008405 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008406
Kent Overstreet4f024f32013-10-11 15:44:27 -07008407 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008408 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008409 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008410 goto submit;
8411 }
8412
David Woodhouse53b381b2013-01-29 18:40:14 -05008413 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008414 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008415 async_submit = 0;
8416 else
8417 async_submit = 1;
8418
Josef Bacik02f57c72011-04-06 14:25:44 -04008419 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8420 if (!bio)
8421 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008422
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008423 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008424 bio->bi_private = dip;
8425 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008426 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008427 atomic_inc(&dip->pending_bios);
8428
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008429 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008430 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308431 i = 0;
8432next_block:
8433 if (unlikely(map_length < submit_len + blocksize ||
8434 bio_add_page(bio, bvec->bv_page, blocksize,
8435 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008436 /*
8437 * inc the count before we submit the bio so
8438 * we know the end IO handler won't happen before
8439 * we inc the count. Otherwise, the dip might get freed
8440 * before we're done setting it up
8441 */
8442 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008443 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008444 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008445 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008446 if (ret) {
8447 bio_put(bio);
8448 atomic_dec(&dip->pending_bios);
8449 goto out_err;
8450 }
8451
Miao Xiee65e1532010-11-22 03:04:43 +00008452 start_sector += submit_len >> 9;
8453 file_offset += submit_len;
8454
8455 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008456
8457 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8458 start_sector, GFP_NOFS);
8459 if (!bio)
8460 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008461 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008462 bio->bi_private = dip;
8463 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008464 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008465
Kent Overstreet4f024f32013-10-11 15:44:27 -07008466 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008467 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008468 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008469 &map_length, NULL, 0);
8470 if (ret) {
8471 bio_put(bio);
8472 goto out_err;
8473 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308474
8475 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008476 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308477 submit_len += blocksize;
8478 if (--nr_sectors) {
8479 i++;
8480 goto next_block;
8481 }
Miao Xiee65e1532010-11-22 03:04:43 +00008482 }
8483 }
8484
Josef Bacik02f57c72011-04-06 14:25:44 -04008485submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008486 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008487 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008488 if (!ret)
8489 return 0;
8490
8491 bio_put(bio);
8492out_err:
8493 dip->errors = 1;
8494 /*
8495 * before atomic variable goto zero, we must
8496 * make sure dip->errors is perceived to be set.
8497 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008498 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008499 if (atomic_dec_and_test(&dip->pending_bios))
8500 bio_io_error(dip->orig_bio);
8501
8502 /* bio_end_io() will handle error, so we needn't return it */
8503 return 0;
8504}
8505
Mike Christie8a4c1e42016-06-05 14:31:50 -05008506static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8507 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008508{
Filipe Manana61de7182015-07-01 12:13:10 +01008509 struct btrfs_dio_private *dip = NULL;
8510 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008511 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008513 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008514 int ret = 0;
8515
8516 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8517
Chris Mason9be33952013-05-17 18:30:14 -04008518 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008519 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008520 ret = -ENOMEM;
8521 goto free_ordered;
8522 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008523
Miao Xiec1dc0892014-09-12 18:43:56 +08008524 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008525 if (!dip) {
8526 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008527 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008528 }
8529
8530 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008531 dip->inode = inode;
8532 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008533 dip->bytes = dio_bio->bi_iter.bi_size;
8534 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008535 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008536 dip->orig_bio = io_bio;
8537 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008538 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 btrfs_bio = btrfs_io_bio(io_bio);
8540 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541
Miao Xiec1dc0892014-09-12 18:43:56 +08008542 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008543 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008544 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008545 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008546 dip->subio_endio = btrfs_subio_endio_read;
8547 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008548
Filipe Mananaf28a4922015-12-08 19:23:20 +00008549 /*
8550 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8551 * even if we fail to submit a bio, because in such case we do the
8552 * corresponding error handling below and it must not be done a second
8553 * time by btrfs_direct_IO().
8554 */
8555 if (write) {
8556 struct btrfs_dio_data *dio_data = current->journal_info;
8557
8558 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8559 dip->bytes;
8560 dio_data->unsubmitted_oe_range_start =
8561 dio_data->unsubmitted_oe_range_end;
8562 }
8563
Mike Christie81a75f672016-06-05 14:31:54 -05008564 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008565 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008566 return;
Chris Mason9be33952013-05-17 18:30:14 -04008567
Miao Xie23ea8e52014-09-12 18:43:54 +08008568 if (btrfs_bio->end_io)
8569 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008570
Josef Bacik4b46fce2010-05-23 11:00:55 -04008571free_ordered:
8572 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008573 * If we arrived here it means either we failed to submit the dip
8574 * or we either failed to clone the dio_bio or failed to allocate the
8575 * dip. If we cloned the dio_bio and allocated the dip, we can just
8576 * call bio_endio against our io_bio so that we get proper resource
8577 * cleanup if we fail to submit the dip, otherwise, we must do the
8578 * same as btrfs_endio_direct_[write|read] because we can't call these
8579 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008580 */
Filipe Manana61de7182015-07-01 12:13:10 +01008581 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008582 io_bio->bi_error = -EIO;
8583 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008584 /*
8585 * The end io callbacks free our dip, do the final put on io_bio
8586 * and all the cleanup and final put for dio_bio (through
8587 * dio_end_io()).
8588 */
8589 dip = NULL;
8590 io_bio = NULL;
8591 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008592 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008593 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008594 file_offset,
8595 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008596 false);
Filipe Manana14543772015-11-24 16:23:54 +00008597 else
Filipe Manana61de7182015-07-01 12:13:10 +01008598 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8599 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008600
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008601 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008602 /*
8603 * Releases and cleans up our dio_bio, no need to bio_put()
8604 * nor bio_endio()/bio_io_error() against dio_bio.
8605 */
8606 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008607 }
Filipe Manana61de7182015-07-01 12:13:10 +01008608 if (io_bio)
8609 bio_put(io_bio);
8610 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008611}
8612
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008613static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8614 struct kiocb *iocb,
8615 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008616{
8617 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008618 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008619 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008620 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008621
8622 if (offset & blocksize_mask)
8623 goto out;
8624
Al Viro28060d52014-03-22 05:15:17 -04008625 if (iov_iter_alignment(iter) & blocksize_mask)
8626 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008627
Al Viro28060d52014-03-22 05:15:17 -04008628 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008629 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008630 return 0;
8631 /*
8632 * Check to make sure we don't have duplicate iov_base's in this
8633 * iovec, if so return EINVAL, otherwise we'll get csum errors
8634 * when reading back.
8635 */
8636 for (seg = 0; seg < iter->nr_segs; seg++) {
8637 for (i = seg + 1; i < iter->nr_segs; i++) {
8638 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008639 goto out;
8640 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008641 }
8642 retval = 0;
8643out:
8644 return retval;
8645}
Josef Bacikeb838e72012-07-31 16:28:48 -04008646
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008647static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008648{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008649 struct file *file = iocb->ki_filp;
8650 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308652 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008653 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008654 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008655 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008656 bool wakeup = true;
8657 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008658 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008660 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008661 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008662
Jens Axboefe0f07d2015-04-15 17:05:48 -06008663 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008664 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008665
Josef Bacik0e267c42013-07-02 10:38:02 -04008666 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008667 * The generic stuff only does filemap_write_and_wait_range, which
8668 * isn't enough if we've written compressed pages to this area, so
8669 * we need to flush the dirty pages again to make absolutely sure
8670 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008671 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008672 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008673 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8674 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008675 filemap_fdatawrite_range(inode->i_mapping, offset,
8676 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008677
Omar Sandoval6f673762015-03-16 04:33:52 -07008678 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008679 /*
8680 * If the write DIO is beyond the EOF, we need update
8681 * the isize, but it is protected by i_mutex. So we can
8682 * not unlock the i_mutex at this case.
8683 */
8684 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008685 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008686 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008687 relock = true;
8688 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008689 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008690 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008691 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008692 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008693
8694 /*
8695 * We need to know how many extents we reserved so that we can
8696 * do the accounting properly if we go over the number we
8697 * originally calculated. Abuse current->journal_info for this.
8698 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008699 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008700 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008701 dio_data.unsubmitted_oe_range_start = (u64)offset;
8702 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308703 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308704 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008705 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8706 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008707 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008708 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8709 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008710 }
8711
Omar Sandoval17f8c842015-03-16 04:33:50 -07008712 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008713 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008714 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008715 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008716 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308717 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008718 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008719 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308720 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008721 btrfs_delalloc_release_space(inode, offset,
8722 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008723 /*
8724 * On error we might have left some ordered extents
8725 * without submitting corresponding bios for them, so
8726 * cleanup them up to avoid other tasks getting them
8727 * and waiting for them to complete forever.
8728 */
8729 if (dio_data.unsubmitted_oe_range_start <
8730 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008731 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008732 dio_data.unsubmitted_oe_range_start,
8733 dio_data.unsubmitted_oe_range_end -
8734 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008735 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008736 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008737 btrfs_delalloc_release_space(inode, offset,
8738 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008739 }
Miao Xie38851cc2013-02-08 07:04:11 +00008740out:
Miao Xie2e60a512013-02-08 07:01:08 +00008741 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008742 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008743 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008744 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008745
8746 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008747}
8748
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008749#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8750
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008751static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8752 __u64 start, __u64 len)
8753{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008754 int ret;
8755
8756 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8757 if (ret)
8758 return ret;
8759
Chris Masonec29ed52011-02-23 16:23:20 -05008760 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008761}
8762
Chris Mason9ebefb182007-06-15 13:50:00 -04008763int btrfs_readpage(struct file *file, struct page *page)
8764{
Chris Masond1310b22008-01-24 16:13:08 -05008765 struct extent_io_tree *tree;
8766 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008767 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008768}
Chris Mason1832a6d2007-12-21 16:27:21 -05008769
Chris Mason39279cc2007-06-12 06:35:45 -04008770static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8771{
Chris Masond1310b22008-01-24 16:13:08 -05008772 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008773 struct inode *inode = page->mapping->host;
8774 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008775
8776 if (current->flags & PF_MEMALLOC) {
8777 redirty_page_for_writepage(wbc, page);
8778 unlock_page(page);
8779 return 0;
8780 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008781
8782 /*
8783 * If we are under memory pressure we will call this directly from the
8784 * VM, we need to make sure we have the inode referenced for the ordered
8785 * extent. If not just return like we didn't do anything.
8786 */
8787 if (!igrab(inode)) {
8788 redirty_page_for_writepage(wbc, page);
8789 return AOP_WRITEPAGE_ACTIVATE;
8790 }
Chris Masond1310b22008-01-24 16:13:08 -05008791 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008792 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8793 btrfs_add_delayed_iput(inode);
8794 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008795}
Chris Mason39279cc2007-06-12 06:35:45 -04008796
Eric Sandeen48a3b632013-04-25 20:41:01 +00008797static int btrfs_writepages(struct address_space *mapping,
8798 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008799{
Chris Masond1310b22008-01-24 16:13:08 -05008800 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008801
Chris Masond1310b22008-01-24 16:13:08 -05008802 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008803 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8804}
8805
Chris Mason3ab2fb52007-11-08 10:59:22 -05008806static int
8807btrfs_readpages(struct file *file, struct address_space *mapping,
8808 struct list_head *pages, unsigned nr_pages)
8809{
Chris Masond1310b22008-01-24 16:13:08 -05008810 struct extent_io_tree *tree;
8811 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008812 return extent_readpages(tree, mapping, pages, nr_pages,
8813 btrfs_get_extent);
8814}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008816{
Chris Masond1310b22008-01-24 16:13:08 -05008817 struct extent_io_tree *tree;
8818 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008819 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008820
Chris Masond1310b22008-01-24 16:13:08 -05008821 tree = &BTRFS_I(page->mapping->host)->io_tree;
8822 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008823 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008824 if (ret == 1) {
8825 ClearPagePrivate(page);
8826 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008827 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008828 }
8829 return ret;
8830}
Chris Mason39279cc2007-06-12 06:35:45 -04008831
Chris Masone6dcd2d2008-07-17 12:53:50 -04008832static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8833{
Chris Mason98509cf2008-09-11 15:51:43 -04008834 if (PageWriteback(page) || PageDirty(page))
8835 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008836 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837}
8838
Lukas Czernerd47992f2013-05-21 23:17:23 -04008839static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8840 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008841{
Josef Bacik5fd02042012-05-02 14:00:54 -04008842 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008843 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008845 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008847 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308848 u64 start;
8849 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008851
Chris Mason8b62b722009-09-02 16:53:46 -04008852 /*
8853 * we have the page locked, so new writeback can't start,
8854 * and the dirty bit won't be cleared while we are here.
8855 *
8856 * Wait for IO on this page so that we can safely clear
8857 * the PagePrivate2 bit and do ordered accounting
8858 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008860
Josef Bacik5fd02042012-05-02 14:00:54 -04008861 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 if (offset) {
8863 btrfs_releasepage(page, GFP_NOFS);
8864 return;
8865 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008866
8867 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008868 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308869again:
8870 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008871 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308872 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008873 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008875 /*
8876 * IO on this page will never be started, so we need
8877 * to account for any ordered extents now
8878 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308880 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008881 EXTENT_DIRTY | EXTENT_DELALLOC |
8882 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8883 EXTENT_DEFRAG, 1, 0, &cached_state,
8884 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008885 /*
8886 * whoever cleared the private bit is responsible
8887 * for the finish_ordered_io
8888 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008889 if (TestClearPagePrivate2(page)) {
8890 struct btrfs_ordered_inode_tree *tree;
8891 u64 new_len;
8892
8893 tree = &BTRFS_I(inode)->ordered_tree;
8894
8895 spin_lock_irq(&tree->lock);
8896 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008898 if (new_len < ordered->truncated_len)
8899 ordered->truncated_len = new_len;
8900 spin_unlock_irq(&tree->lock);
8901
8902 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308903 start,
8904 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008905 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008906 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008908 if (!inode_evicting) {
8909 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308910 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008911 &cached_state);
8912 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308913
8914 start = end + 1;
8915 if (start < page_end)
8916 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008918
Qu Wenruob9d0b382015-09-29 10:35:16 +08008919 /*
8920 * Qgroup reserved space handler
8921 * Page here will be either
8922 * 1) Already written to disk
8923 * In this case, its reserved space is released from data rsv map
8924 * and will be freed by delayed_ref handler finally.
8925 * So even we call qgroup_free_data(), it won't decrease reserved
8926 * space.
8927 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008928 * This means the reserved space should be freed here. However,
8929 * if a truncate invalidates the page (by clearing PageDirty)
8930 * and the page is accounted for while allocating extent
8931 * in btrfs_check_data_free_space() we let delayed_ref to
8932 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008933 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008934 if (PageDirty(page))
8935 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008936 if (!inode_evicting) {
8937 clear_extent_bit(tree, page_start, page_end,
8938 EXTENT_LOCKED | EXTENT_DIRTY |
8939 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8940 EXTENT_DEFRAG, 1, 1,
8941 &cached_state, GFP_NOFS);
8942
8943 __btrfs_releasepage(page, GFP_NOFS);
8944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945
Chris Mason4a096752008-07-21 10:29:44 -04008946 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008947 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008948 ClearPagePrivate(page);
8949 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008950 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008951 }
Chris Mason39279cc2007-06-12 06:35:45 -04008952}
8953
Chris Mason9ebefb182007-06-15 13:50:00 -04008954/*
8955 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8956 * called from a page fault handler when a page is first dirtied. Hence we must
8957 * be careful to check for EOF conditions here. We set the page up correctly
8958 * for a written page which means we get ENOSPC checking when writing into
8959 * holes and correct delalloc and unwritten extent mapping on filesystems that
8960 * support these features.
8961 *
8962 * We are not allowed to take the i_mutex here so we have to play games to
8963 * protect against truncate races as the page could now be beyond EOF. Because
8964 * vmtruncate() writes the inode size before removing pages, once we have the
8965 * page lock we can determine safely if the page is beyond EOF. If it is not
8966 * beyond EOF, then the page is guaranteed safe against truncation until we
8967 * unlock the page.
8968 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008969int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008970{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008971 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008972 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8975 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008976 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008977 char *kaddr;
8978 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008979 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008980 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008981 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008983 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008984 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308985 u64 end;
8986
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008987 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008988
Jan Karab2b5ef52012-06-12 16:20:45 +02008989 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008990 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008991 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308992 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008993
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308994 /*
8995 * Reserving delalloc space after obtaining the page lock can lead to
8996 * deadlock. For example, if a dirty page is locked by this function
8997 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8998 * dirty page write out, then the btrfs_writepage() function could
8999 * end up waiting indefinitely to get a lock on the page currently
9000 * being processed by btrfs_page_mkwrite() function.
9001 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009002 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309003 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009004 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009005 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009006 reserved = 1;
9007 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009008 if (ret) {
9009 if (ret == -ENOMEM)
9010 ret = VM_FAULT_OOM;
9011 else /* -ENOSPC, -EIO, etc */
9012 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009013 if (reserved)
9014 goto out;
9015 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009016 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009017
Nick Piggin56a76f82009-03-31 15:23:23 -07009018 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009020 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009021 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009022
Chris Mason9ebefb182007-06-15 13:50:00 -04009023 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009025 /* page got truncated out from underneath us */
9026 goto out_unlock;
9027 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 wait_on_page_writeback(page);
9029
David Sterbaff13db42015-12-03 14:30:40 +01009030 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 set_page_extent_mapped(page);
9032
Chris Masoneb84ae02008-07-17 13:53:27 -04009033 /*
9034 * we can't set the delalloc bits if there are pending ordered
9035 * extents. Drop our locks and wait for them to finish
9036 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009037 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9038 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009039 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009040 unlock_extent_cached(io_tree, page_start, page_end,
9041 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009042 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009043 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009044 btrfs_put_ordered_extent(ordered);
9045 goto again;
9046 }
9047
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009048 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009049 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009050 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009051 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309052 end = page_start + reserved_space - 1;
9053 spin_lock(&BTRFS_I(inode)->lock);
9054 BTRFS_I(inode)->outstanding_extents++;
9055 spin_unlock(&BTRFS_I(inode)->lock);
9056 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309058 }
9059 }
9060
Josef Bacikfbf19082009-10-01 17:10:23 -04009061 /*
Liu Bo54160342016-12-13 12:15:19 -08009062 * page_mkwrite gets called when the page is firstly dirtied after it's
9063 * faulted in, but write(2) could also dirty a page and set delalloc
9064 * bits, thus in this case for space account reason, we still need to
9065 * clear any delalloc bits within this page range since we have to
9066 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009067 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309068 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009069 EXTENT_DIRTY | EXTENT_DELALLOC |
9070 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009071 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009072
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309073 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009074 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009075 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009076 unlock_extent_cached(io_tree, page_start, page_end,
9077 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009078 ret = VM_FAULT_SIGBUS;
9079 goto out_unlock;
9080 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009082
9083 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009084 if (page_start + PAGE_SIZE > size)
9085 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009086 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009087 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009088
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009089 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009090 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009091 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009092 flush_dcache_page(page);
9093 kunmap(page);
9094 }
Chris Mason247e7432008-07-17 12:53:51 -04009095 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009096 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009097 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009098
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009099 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009100 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009101 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009102
Josef Bacik2ac55d42010-02-03 19:33:23 +00009103 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009104
9105out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009106 if (!ret) {
9107 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009108 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009109 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009110 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009111out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309112 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009113out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009114 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009115 return ret;
9116}
9117
Josef Bacika41ad392011-01-31 15:30:16 -05009118static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009119{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009120 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009121 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009122 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009123 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009124 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009125 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009126 u64 mask = fs_info->sectorsize - 1;
9127 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009128
Josef Bacik0ef8b722013-10-25 16:13:35 -04009129 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9130 (u64)-1);
9131 if (ret)
9132 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009133
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009135 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009136 * 3 things going on here
9137 *
9138 * 1) We need to reserve space for our orphan item and the space to
9139 * delete our orphan item. Lord knows we don't want to have a dangling
9140 * orphan item because we didn't reserve space to remove it.
9141 *
9142 * 2) We need to reserve space to update our inode.
9143 *
9144 * 3) We need to have something to cache all the space that is going to
9145 * be free'd up by the truncate operation, but also have some slack
9146 * space reserved in case it uses space during the truncate (thank you
9147 * very much snapshotting).
9148 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009149 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009150 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009151 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009152 * doesn't end up using space reserved for updating the inode or
9153 * removing the orphan item. We also need to be able to stop the
9154 * transaction and start a new one, which means we need to be able to
9155 * update the inode several times, and we have no idea of knowing how
9156 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009157 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009158 * Then there is the orphan item, which does indeed need to be held on
9159 * to for the whole operation, and we need nobody to touch this reserved
9160 * space except the orphan code.
9161 *
9162 * So that leaves us with
9163 *
9164 * 1) root->orphan_block_rsv - for the orphan deletion.
9165 * 2) rsv - for the truncate reservation, which we will steal from the
9166 * transaction reservation.
9167 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9168 * updating the inode.
9169 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009170 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009171 if (!rsv)
9172 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009173 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009174 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009175
Josef Bacik907cbce2011-08-08 13:46:15 -04009176 /*
Josef Bacik07127182011-08-19 10:29:59 -04009177 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009178 * 1 for updating the inode.
9179 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009180 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009181 if (IS_ERR(trans)) {
9182 err = PTR_ERR(trans);
9183 goto out;
9184 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009185
Josef Bacik907cbce2011-08-08 13:46:15 -04009186 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009187 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009188 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009189 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009190
Chris Mason5a3f23d2009-03-31 13:27:11 -04009191 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009192 * So if we truncate and then write and fsync we normally would just
9193 * write the extents that changed, which is a problem if we need to
9194 * first truncate that entire inode. So set this flag so we write out
9195 * all of the extents in the inode to the sync log so we're completely
9196 * safe.
9197 */
9198 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009199 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009200
Yan, Zheng80825102009-11-12 09:35:36 +00009201 while (1) {
9202 ret = btrfs_truncate_inode_items(trans, root, inode,
9203 inode->i_size,
9204 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009205 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009206 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009207 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009208 }
Chris Mason39279cc2007-06-12 06:35:45 -04009209
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009210 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009211 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009212 if (ret) {
9213 err = ret;
9214 break;
9215 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009216
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009217 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009218 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009219
9220 trans = btrfs_start_transaction(root, 2);
9221 if (IS_ERR(trans)) {
9222 ret = err = PTR_ERR(trans);
9223 trans = NULL;
9224 break;
9225 }
9226
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009227 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009228 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009229 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009230 BUG_ON(ret); /* shouldn't happen */
9231 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009232 }
9233
9234 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009235 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009236 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009237 if (ret)
9238 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009239 }
9240
Chris Mason917c16b2011-11-08 14:49:59 -05009241 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009242 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009243 ret = btrfs_update_inode(trans, root, inode);
9244 if (ret && !err)
9245 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009246
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009247 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009248 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009249 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009251 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009252
Josef Bacik3893e332011-01-31 16:03:11 -05009253 if (ret && !err)
9254 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009255
Josef Bacik3893e332011-01-31 16:03:11 -05009256 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009257}
9258
Sven Wegener3b963622008-06-09 21:57:42 -04009259/*
Chris Masond352ac62008-09-29 15:18:18 -04009260 * create a new subvolume directory/inode (helper for the ioctl).
9261 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009262int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009263 struct btrfs_root *new_root,
9264 struct btrfs_root *parent_root,
9265 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009266{
Chris Mason39279cc2007-06-12 06:35:45 -04009267 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009268 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009269 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009270
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009271 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9272 new_dirid, new_dirid,
9273 S_IFDIR | (~current_umask() & S_IRWXUGO),
9274 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009275 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009276 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009277 inode->i_op = &btrfs_dir_inode_operations;
9278 inode->i_fop = &btrfs_dir_file_operations;
9279
Miklos Szeredibfe86842011-10-28 14:13:29 +02009280 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009281 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009282 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009283
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009284 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9285 if (err)
9286 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009287 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009288 new_root->root_key.objectid, err);
9289
Yan, Zheng76dda932009-09-21 16:00:26 -04009290 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009291
Yan, Zheng76dda932009-09-21 16:00:26 -04009292 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009293 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009294}
9295
Chris Mason39279cc2007-06-12 06:35:45 -04009296struct inode *btrfs_alloc_inode(struct super_block *sb)
9297{
9298 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009299 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009300
9301 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9302 if (!ei)
9303 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009304
9305 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009306 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009307 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009308 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009309 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009311 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009312 ei->disk_i_size = 0;
9313 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009314 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009316 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009318 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009319 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320
Josef Bacik9e0baf62011-07-15 15:16:44 +00009321 spin_lock_init(&ei->lock);
9322 ei->outstanding_extents = 0;
9323 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324
Josef Bacik72ac3c02012-05-23 14:13:11 -04009325 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009326 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327
Miao Xie16cdcec2011-04-22 18:12:22 +08009328 ei->delayed_node = NULL;
9329
chandan r9cc97d62012-07-04 12:48:07 +05309330 ei->i_otime.tv_sec = 0;
9331 ei->i_otime.tv_nsec = 0;
9332
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009333 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009334 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009335 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9336 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009337 ei->io_tree.track_uptodate = 1;
9338 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009339 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009341 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009342 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009343 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009344 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009345 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009346 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009347
9348 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009349}
9350
Josef Bacikaaedb552013-10-11 14:44:09 -04009351#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9352void btrfs_test_destroy_inode(struct inode *inode)
9353{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009354 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009355 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9356}
9357#endif
9358
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009359static void btrfs_i_callback(struct rcu_head *head)
9360{
9361 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009362 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9363}
9364
Chris Mason39279cc2007-06-12 06:35:45 -04009365void btrfs_destroy_inode(struct inode *inode)
9366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009367 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009368 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009369 struct btrfs_root *root = BTRFS_I(inode)->root;
9370
Al Virob3d9b7a2012-06-09 13:51:19 -04009371 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009372 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009373 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9374 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009375 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9376 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009377 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009378
Chris Mason5a3f23d2009-03-31 13:27:11 -04009379 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009380 * This can happen where we create an inode, but somebody else also
9381 * created the same inode and we need to destroy the one we already
9382 * created.
9383 */
9384 if (!root)
9385 goto free;
9386
Josef Bacik8a35d952012-05-23 14:26:42 -04009387 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9388 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009389 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009390 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009391 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009392 }
Josef Bacik7b128762008-07-24 12:17:14 -04009393
Chris Masond3977122009-01-05 21:25:51 -05009394 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009395 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9396 if (!ordered)
9397 break;
9398 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009399 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009400 "found ordered extent %llu %llu on inode cleanup",
9401 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009402 btrfs_remove_ordered_extent(inode, ordered);
9403 btrfs_put_ordered_extent(ordered);
9404 btrfs_put_ordered_extent(ordered);
9405 }
9406 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009407 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009408 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009409 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009410free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009411 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009412}
9413
Al Viro45321ac2010-06-07 13:43:19 -04009414int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009415{
9416 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009417
Naohiro Aota6379ef92013-06-06 09:56:34 +00009418 if (root == NULL)
9419 return 1;
9420
Liu Bofa6ac872013-02-20 14:10:23 +00009421 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009422 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009423 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009424 else
Al Viro45321ac2010-06-07 13:43:19 -04009425 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009426}
9427
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009428static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009429{
9430 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9431
9432 inode_init_once(&ei->vfs_inode);
9433}
9434
9435void btrfs_destroy_cachep(void)
9436{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009437 /*
9438 * Make sure all delayed rcu free inodes are flushed before we
9439 * destroy cache.
9440 */
9441 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009442 kmem_cache_destroy(btrfs_inode_cachep);
9443 kmem_cache_destroy(btrfs_trans_handle_cachep);
9444 kmem_cache_destroy(btrfs_transaction_cachep);
9445 kmem_cache_destroy(btrfs_path_cachep);
9446 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009447}
9448
9449int btrfs_init_cachep(void)
9450{
David Sterba837e1972012-09-07 03:00:48 -06009451 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009452 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009453 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9454 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009455 if (!btrfs_inode_cachep)
9456 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009457
David Sterba837e1972012-09-07 03:00:48 -06009458 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009459 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009460 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009461 if (!btrfs_trans_handle_cachep)
9462 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009463
David Sterba837e1972012-09-07 03:00:48 -06009464 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009465 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009466 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009467 if (!btrfs_transaction_cachep)
9468 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009469
David Sterba837e1972012-09-07 03:00:48 -06009470 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009471 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009472 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009473 if (!btrfs_path_cachep)
9474 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009475
David Sterba837e1972012-09-07 03:00:48 -06009476 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009477 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009478 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009479 if (!btrfs_free_space_cachep)
9480 goto fail;
9481
Chris Mason39279cc2007-06-12 06:35:45 -04009482 return 0;
9483fail:
9484 btrfs_destroy_cachep();
9485 return -ENOMEM;
9486}
9487
9488static int btrfs_getattr(struct vfsmount *mnt,
9489 struct dentry *dentry, struct kstat *stat)
9490{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009491 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009492 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009493 u32 blocksize = inode->i_sb->s_blocksize;
9494
Chris Mason39279cc2007-06-12 06:35:45 -04009495 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009496 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009497
9498 spin_lock(&BTRFS_I(inode)->lock);
9499 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9500 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009501 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009502 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009503 return 0;
9504}
9505
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506static int btrfs_rename_exchange(struct inode *old_dir,
9507 struct dentry *old_dentry,
9508 struct inode *new_dir,
9509 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009511 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009512 struct btrfs_trans_handle *trans;
9513 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009514 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 struct inode *new_inode = new_dentry->d_inode;
9516 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009517 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009519 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9520 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 u64 old_idx = 0;
9522 u64 new_idx = 0;
9523 u64 root_objectid;
9524 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009525 bool root_log_pinned = false;
9526 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527
9528 /* we only allow rename subvolume link between subvolumes */
9529 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9530 return -EXDEV;
9531
9532 /* close the race window with snapshot create/destroy ioctl */
9533 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009534 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009535 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009536 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537
9538 /*
9539 * We want to reserve the absolute worst case amount of items. So if
9540 * both inodes are subvols and we need to unlink them then that would
9541 * require 4 item modifications, but if they are both normal inodes it
9542 * would require 5 item modifications, so we'll assume their normal
9543 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9544 * should cover the worst case number of items we'll modify.
9545 */
9546 trans = btrfs_start_transaction(root, 12);
9547 if (IS_ERR(trans)) {
9548 ret = PTR_ERR(trans);
9549 goto out_notrans;
9550 }
9551
9552 /*
9553 * We need to find a free sequence number both in the source and
9554 * in the destination directory for the exchange.
9555 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009556 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 if (ret)
9558 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009559 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 if (ret)
9561 goto out_fail;
9562
9563 BTRFS_I(old_inode)->dir_index = 0ULL;
9564 BTRFS_I(new_inode)->dir_index = 0ULL;
9565
9566 /* Reference for the source. */
9567 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9568 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009569 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009571 btrfs_pin_log_trans(root);
9572 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 ret = btrfs_insert_inode_ref(trans, dest,
9574 new_dentry->d_name.name,
9575 new_dentry->d_name.len,
9576 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009577 btrfs_ino(BTRFS_I(new_dir)),
9578 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579 if (ret)
9580 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 }
9582
9583 /* And now for the dest. */
9584 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9585 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009586 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009588 btrfs_pin_log_trans(dest);
9589 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 ret = btrfs_insert_inode_ref(trans, root,
9591 old_dentry->d_name.name,
9592 old_dentry->d_name.len,
9593 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009594 btrfs_ino(BTRFS_I(old_dir)),
9595 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 if (ret)
9597 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 }
9599
9600 /* Update inode version and ctime/mtime. */
9601 inode_inc_iversion(old_dir);
9602 inode_inc_iversion(new_dir);
9603 inode_inc_iversion(old_inode);
9604 inode_inc_iversion(new_inode);
9605 old_dir->i_ctime = old_dir->i_mtime = ctime;
9606 new_dir->i_ctime = new_dir->i_mtime = ctime;
9607 old_inode->i_ctime = ctime;
9608 new_inode->i_ctime = ctime;
9609
9610 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009611 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9612 BTRFS_I(old_inode), 1);
9613 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9614 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 }
9616
9617 /* src is a subvolume */
9618 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9619 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9620 ret = btrfs_unlink_subvol(trans, root, old_dir,
9621 root_objectid,
9622 old_dentry->d_name.name,
9623 old_dentry->d_name.len);
9624 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009625 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9626 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 old_dentry->d_name.name,
9628 old_dentry->d_name.len);
9629 if (!ret)
9630 ret = btrfs_update_inode(trans, root, old_inode);
9631 }
9632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009633 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 goto out_fail;
9635 }
9636
9637 /* dest is a subvolume */
9638 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9639 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9640 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9641 root_objectid,
9642 new_dentry->d_name.name,
9643 new_dentry->d_name.len);
9644 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009645 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9646 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 new_dentry->d_name.name,
9648 new_dentry->d_name.len);
9649 if (!ret)
9650 ret = btrfs_update_inode(trans, dest, new_inode);
9651 }
9652 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009653 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 goto out_fail;
9655 }
9656
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009657 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 new_dentry->d_name.name,
9659 new_dentry->d_name.len, 0, old_idx);
9660 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009661 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 goto out_fail;
9663 }
9664
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009665 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 old_dentry->d_name.name,
9667 old_dentry->d_name.len, 0, new_idx);
9668 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009669 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670 goto out_fail;
9671 }
9672
9673 if (old_inode->i_nlink == 1)
9674 BTRFS_I(old_inode)->dir_index = old_idx;
9675 if (new_inode->i_nlink == 1)
9676 BTRFS_I(new_inode)->dir_index = new_idx;
9677
Filipe Manana86e8aa02016-05-05 02:02:27 +01009678 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009680 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9681 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009683 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009685 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009687 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9688 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009690 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 }
9692out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009693 /*
9694 * If we have pinned a log and an error happened, we unpin tasks
9695 * trying to sync the log and force them to fallback to a transaction
9696 * commit if the log currently contains any of the inodes involved in
9697 * this rename operation (to ensure we do not persist a log with an
9698 * inconsistent state for any of these inodes or leading to any
9699 * inconsistencies when replayed). If the transaction was aborted, the
9700 * abortion reason is propagated to userspace when attempting to commit
9701 * the transaction. If the log does not contain any of these inodes, we
9702 * allow the tasks to sync it.
9703 */
9704 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009705 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9706 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9707 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009708 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009709 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009710 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009711
9712 if (root_log_pinned) {
9713 btrfs_end_log_trans(root);
9714 root_log_pinned = false;
9715 }
9716 if (dest_log_pinned) {
9717 btrfs_end_log_trans(dest);
9718 dest_log_pinned = false;
9719 }
9720 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009721 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722out_notrans:
9723 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009724 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009726 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009727
9728 return ret;
9729}
9730
9731static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9732 struct btrfs_root *root,
9733 struct inode *dir,
9734 struct dentry *dentry)
9735{
9736 int ret;
9737 struct inode *inode;
9738 u64 objectid;
9739 u64 index;
9740
9741 ret = btrfs_find_free_ino(root, &objectid);
9742 if (ret)
9743 return ret;
9744
9745 inode = btrfs_new_inode(trans, root, dir,
9746 dentry->d_name.name,
9747 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009748 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009749 objectid,
9750 S_IFCHR | WHITEOUT_MODE,
9751 &index);
9752
9753 if (IS_ERR(inode)) {
9754 ret = PTR_ERR(inode);
9755 return ret;
9756 }
9757
9758 inode->i_op = &btrfs_special_inode_operations;
9759 init_special_inode(inode, inode->i_mode,
9760 WHITEOUT_DEV);
9761
9762 ret = btrfs_init_inode_security(trans, inode, dir,
9763 &dentry->d_name);
9764 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009765 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009766
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009767 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9768 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009770 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009771
9772 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009773out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009775 if (ret)
9776 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 iput(inode);
9778
Filipe Mananac9901612016-05-05 01:41:57 +01009779 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780}
9781
Chris Mason39279cc2007-06-12 06:35:45 -04009782static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 struct inode *new_dir, struct dentry *new_dentry,
9784 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009785{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009786 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009787 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009788 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009789 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9790 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009791 struct inode *new_inode = d_inode(new_dentry);
9792 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009793 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009794 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009795 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009796 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009797 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009798
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009799 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009800 return -EPERM;
9801
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009803 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009804 return -EXDEV;
9805
Li Zefan33345d012011-04-20 10:31:50 +08009806 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009807 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009808 return -ENOTEMPTY;
9809
Chris Mason39279cc2007-06-12 06:35:45 -04009810 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009811 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009812 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009813
9814
9815 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009816 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009817 new_dentry->d_name.name,
9818 new_dentry->d_name.len);
9819
9820 if (ret) {
9821 if (ret == -EEXIST) {
9822 /* we shouldn't get
9823 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309824 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009825 return ret;
9826 }
9827 } else {
9828 /* maybe -EOVERFLOW */
9829 return ret;
9830 }
9831 }
9832 ret = 0;
9833
Chris Mason5a3f23d2009-03-31 13:27:11 -04009834 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009835 * we're using rename to replace one file with another. Start IO on it
9836 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009837 */
Chris Mason8d875f92014-08-12 10:47:42 -07009838 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009839 filemap_flush(old_inode->i_mapping);
9840
Yan, Zheng76dda932009-09-21 16:00:26 -04009841 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009842 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009843 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009844 /*
9845 * We want to reserve the absolute worst case amount of items. So if
9846 * both inodes are subvols and we need to unlink them then that would
9847 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009849 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9850 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009851 * If our rename has the whiteout flag, we need more 5 units for the
9852 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9853 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009854 */
Filipe Manana5062af32016-05-05 10:26:26 +01009855 trans_num_items = 11;
9856 if (flags & RENAME_WHITEOUT)
9857 trans_num_items += 5;
9858 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009859 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009860 ret = PTR_ERR(trans);
9861 goto out_notrans;
9862 }
Chris Mason5f39d392007-10-15 16:14:19 -04009863
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009864 if (dest != root)
9865 btrfs_record_root_in_trans(trans, dest);
9866
Nikolay Borisov877574e2017-02-20 13:50:33 +02009867 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009868 if (ret)
9869 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009870
Miao Xie67de1172013-12-26 13:07:06 +08009871 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009872 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009873 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009874 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009876 btrfs_pin_log_trans(root);
9877 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009878 ret = btrfs_insert_inode_ref(trans, dest,
9879 new_dentry->d_name.name,
9880 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009881 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009882 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009883 if (ret)
9884 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009886
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009887 inode_inc_iversion(old_dir);
9888 inode_inc_iversion(new_dir);
9889 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009890 old_dir->i_ctime = old_dir->i_mtime =
9891 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009892 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009893
Chris Mason12fcfd22009-03-24 10:24:20 -04009894 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009895 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9896 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009897
Li Zefan33345d012011-04-20 10:31:50 +08009898 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009899 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9900 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9901 old_dentry->d_name.name,
9902 old_dentry->d_name.len);
9903 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009904 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9905 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009906 old_dentry->d_name.name,
9907 old_dentry->d_name.len);
9908 if (!ret)
9909 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009911 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009912 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009913 goto out_fail;
9914 }
Chris Mason39279cc2007-06-12 06:35:45 -04009915
9916 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009917 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009918 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009919 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009920 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9921 root_objectid = BTRFS_I(new_inode)->location.objectid;
9922 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9923 root_objectid,
9924 new_dentry->d_name.name,
9925 new_dentry->d_name.len);
9926 BUG_ON(new_inode->i_nlink == 0);
9927 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009928 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9929 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 new_dentry->d_name.name,
9931 new_dentry->d_name.len);
9932 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009933 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009934 ret = btrfs_orphan_add(trans,
9935 BTRFS_I(d_inode(new_dentry)));
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 }
Josef Bacikaec74772008-07-24 12:12:38 -04009941
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009942 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009943 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009944 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009945 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009946 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009947 goto out_fail;
9948 }
Chris Mason39279cc2007-06-12 06:35:45 -04009949
Miao Xie67de1172013-12-26 13:07:06 +08009950 if (old_inode->i_nlink == 1)
9951 BTRFS_I(old_inode)->dir_index = index;
9952
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009953 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009954 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009955
David Sterbaf85b7372017-01-20 14:54:07 +01009956 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9957 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009958 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009959 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009960 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009961
9962 if (flags & RENAME_WHITEOUT) {
9963 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9964 old_dentry);
9965
9966 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009967 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009968 goto out_fail;
9969 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009970 }
Chris Mason39279cc2007-06-12 06:35:45 -04009971out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009972 /*
9973 * If we have pinned the log and an error happened, we unpin tasks
9974 * trying to sync the log and force them to fallback to a transaction
9975 * commit if the log currently contains any of the inodes involved in
9976 * this rename operation (to ensure we do not persist a log with an
9977 * inconsistent state for any of these inodes or leading to any
9978 * inconsistencies when replayed). If the transaction was aborted, the
9979 * abortion reason is propagated to userspace when attempting to commit
9980 * the transaction. If the log does not contain any of these inodes, we
9981 * allow the tasks to sync it.
9982 */
9983 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009984 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9985 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9986 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009987 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009988 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009989 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009990
9991 btrfs_end_log_trans(root);
9992 log_pinned = false;
9993 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009994 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009995out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009996 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009997 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009998
Chris Mason39279cc2007-06-12 06:35:45 -04009999 return ret;
10000}
10001
Miklos Szeredi80ace852014-07-23 15:15:32 +020010002static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10003 struct inode *new_dir, struct dentry *new_dentry,
10004 unsigned int flags)
10005{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010006 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010007 return -EINVAL;
10008
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010009 if (flags & RENAME_EXCHANGE)
10010 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10011 new_dentry);
10012
10013 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010014}
10015
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016static void btrfs_run_delalloc_work(struct btrfs_work *work)
10017{
10018 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010019 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010020
10021 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10022 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010023 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010024 filemap_flush(inode->i_mapping);
10025 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10026 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010027 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010028
10029 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010030 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010031 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010032 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033 complete(&delalloc_work->completion);
10034}
10035
10036struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010037 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010038{
10039 struct btrfs_delalloc_work *work;
10040
David Sterba100d5702015-12-08 14:39:32 +010010041 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010042 if (!work)
10043 return NULL;
10044
10045 init_completion(&work->completion);
10046 INIT_LIST_HEAD(&work->list);
10047 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010049 WARN_ON_ONCE(!inode);
10050 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10051 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010052
10053 return work;
10054}
10055
10056void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10057{
10058 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010059 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010060}
10061
Chris Masond352ac62008-09-29 15:18:18 -040010062/*
10063 * some fairly slow code that needs optimization. This walks the list
10064 * of all the inodes with pending delalloc and forces them to disk.
10065 */
Miao Xie6c255e62014-03-06 13:55:01 +080010066static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10067 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010068{
Chris Masonea8c2812008-08-04 23:17:27 -040010069 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010070 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010071 struct btrfs_delalloc_work *work, *next;
10072 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010073 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010074 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010075
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010077 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010078
Miao Xie573bfb72014-03-06 13:55:03 +080010079 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 spin_lock(&root->delalloc_lock);
10081 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010082 while (!list_empty(&splice)) {
10083 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010084 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010085
Miao Xieeb73c1b2013-05-15 07:48:22 +000010086 list_move_tail(&binode->delalloc_inodes,
10087 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010088 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010089 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010091 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010092 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010094
David Sterba651d4942015-11-27 19:24:16 +010010095 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010096 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010097 if (delay_iput)
10098 btrfs_add_delayed_iput(inode);
10099 else
10100 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010101 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010102 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010103 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010104 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010105 btrfs_queue_work(root->fs_info->flush_workers,
10106 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010107 ret++;
10108 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010109 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010110 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010112 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010114
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010115out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010116 list_for_each_entry_safe(work, next, &works, list) {
10117 list_del_init(&work->list);
10118 btrfs_wait_and_free_delalloc_work(work);
10119 }
10120
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 if (!list_empty_careful(&splice)) {
10122 spin_lock(&root->delalloc_lock);
10123 list_splice_tail(&splice, &root->delalloc_inodes);
10124 spin_unlock(&root->delalloc_lock);
10125 }
Miao Xie573bfb72014-03-06 13:55:03 +080010126 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 return ret;
10128}
10129
10130int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10131{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 int ret;
10134
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010135 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010136 return -EROFS;
10137
Miao Xie6c255e62014-03-06 13:55:01 +080010138 ret = __start_delalloc_inodes(root, delay_iput, -1);
10139 if (ret > 0)
10140 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010141 /*
10142 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010143 * we have to make sure the IO is actually started and that
10144 * ordered extents get created before we return
10145 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010146 atomic_inc(&fs_info->async_submit_draining);
10147 while (atomic_read(&fs_info->nr_async_submits) ||
10148 atomic_read(&fs_info->async_delalloc_pages)) {
10149 wait_event(fs_info->async_submit_wait,
10150 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10151 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010152 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010153 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010154 return ret;
10155}
10156
Miao Xie6c255e62014-03-06 13:55:01 +080010157int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10158 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159{
10160 struct btrfs_root *root;
10161 struct list_head splice;
10162 int ret;
10163
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010164 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010165 return -EROFS;
10166
10167 INIT_LIST_HEAD(&splice);
10168
Miao Xie573bfb72014-03-06 13:55:03 +080010169 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_lock(&fs_info->delalloc_root_lock);
10171 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010172 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010173 root = list_first_entry(&splice, struct btrfs_root,
10174 delalloc_root);
10175 root = btrfs_grab_fs_root(root);
10176 BUG_ON(!root);
10177 list_move_tail(&root->delalloc_root,
10178 &fs_info->delalloc_roots);
10179 spin_unlock(&fs_info->delalloc_root_lock);
10180
Miao Xie6c255e62014-03-06 13:55:01 +080010181 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010183 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184 goto out;
10185
Miao Xie6c255e62014-03-06 13:55:01 +080010186 if (nr != -1) {
10187 nr -= ret;
10188 WARN_ON(nr < 0);
10189 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 spin_lock(&fs_info->delalloc_root_lock);
10191 }
10192 spin_unlock(&fs_info->delalloc_root_lock);
10193
Miao Xie6c255e62014-03-06 13:55:01 +080010194 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010195 atomic_inc(&fs_info->async_submit_draining);
10196 while (atomic_read(&fs_info->nr_async_submits) ||
10197 atomic_read(&fs_info->async_delalloc_pages)) {
10198 wait_event(fs_info->async_submit_wait,
10199 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10200 atomic_read(&fs_info->async_delalloc_pages) == 0));
10201 }
10202 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010203out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010204 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 spin_lock(&fs_info->delalloc_root_lock);
10206 list_splice_tail(&splice, &fs_info->delalloc_roots);
10207 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010208 }
Miao Xie573bfb72014-03-06 13:55:03 +080010209 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010210 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010211}
10212
Chris Mason39279cc2007-06-12 06:35:45 -040010213static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10214 const char *symname)
10215{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010216 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010217 struct btrfs_trans_handle *trans;
10218 struct btrfs_root *root = BTRFS_I(dir)->root;
10219 struct btrfs_path *path;
10220 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010221 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010222 int err;
10223 int drop_inode = 0;
10224 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010225 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010226 int name_len;
10227 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010228 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010229 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010230 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010231
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010232 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010233 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010234 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010235
Josef Bacik9ed74f22009-09-11 16:12:44 -040010236 /*
10237 * 2 items for inode item and ref
10238 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010239 * 1 item for updating parent inode item
10240 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010241 * 1 item for xattr if selinux is on
10242 */
Filipe Manana9269d122015-12-31 18:16:29 +000010243 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010244 if (IS_ERR(trans))
10245 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010246
Li Zefan581bb052011-04-20 10:06:11 +080010247 err = btrfs_find_free_ino(root, &objectid);
10248 if (err)
10249 goto out_unlock;
10250
Josef Bacikaec74772008-07-24 12:12:38 -040010251 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010252 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10253 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010254 if (IS_ERR(inode)) {
10255 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010256 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010257 }
Chris Mason39279cc2007-06-12 06:35:45 -040010258
Casey Schauflerad19db72011-12-15 10:09:07 -050010259 /*
10260 * If the active LSM wants to access the inode during
10261 * d_instantiate it needs these. Smack checks to see
10262 * if the filesystem supports xattrs by looking at the
10263 * ops vector.
10264 */
10265 inode->i_fop = &btrfs_file_operations;
10266 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010267 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010268 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10269
10270 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10271 if (err)
10272 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010273
Chris Mason39279cc2007-06-12 06:35:45 -040010274 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010275 if (!path) {
10276 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010277 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010278 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010279 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010280 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010281 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010282 datasize = btrfs_file_extent_calc_inline_size(name_len);
10283 err = btrfs_insert_empty_item(trans, root, path, &key,
10284 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010285 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010286 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010287 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010288 }
Chris Mason5f39d392007-10-15 16:14:19 -040010289 leaf = path->nodes[0];
10290 ei = btrfs_item_ptr(leaf, path->slots[0],
10291 struct btrfs_file_extent_item);
10292 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10293 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010294 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010295 btrfs_set_file_extent_encryption(leaf, ei, 0);
10296 btrfs_set_file_extent_compression(leaf, ei, 0);
10297 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10298 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10299
Chris Mason39279cc2007-06-12 06:35:45 -040010300 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010301 write_extent_buffer(leaf, symname, ptr, name_len);
10302 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010303 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010304
Chris Mason39279cc2007-06-12 06:35:45 -040010305 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010306 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010307 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010308 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010309 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010310 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010311 /*
10312 * Last step, add directory indexes for our symlink inode. This is the
10313 * last step to avoid extra cleanup of these indexes if an error happens
10314 * elsewhere above.
10315 */
10316 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010317 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10318 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010319 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010320 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010321 goto out_unlock_inode;
10322 }
10323
10324 unlock_new_inode(inode);
10325 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010326
10327out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010328 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010329 if (drop_inode) {
10330 inode_dec_link_count(inode);
10331 iput(inode);
10332 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010333 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010334 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010335
10336out_unlock_inode:
10337 drop_inode = 1;
10338 unlock_new_inode(inode);
10339 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010340}
Chris Mason16432982008-04-10 10:23:21 -040010341
Josef Bacik0af3d002010-06-21 14:48:16 -040010342static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10343 u64 start, u64 num_bytes, u64 min_size,
10344 loff_t actual_len, u64 *alloc_hint,
10345 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010346{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010347 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010348 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10349 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010350 struct btrfs_root *root = BTRFS_I(inode)->root;
10351 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010352 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010353 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010354 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010355 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010356 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010357 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010358 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010359
Josef Bacik0af3d002010-06-21 14:48:16 -040010360 if (trans)
10361 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010362 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010363 if (own_trans) {
10364 trans = btrfs_start_transaction(root, 3);
10365 if (IS_ERR(trans)) {
10366 ret = PTR_ERR(trans);
10367 break;
10368 }
Yan Zhengd899e052008-10-30 14:25:28 -040010369 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010370
Byongho Leeee221842015-12-15 01:42:10 +090010371 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010372 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010373 /*
10374 * If we are severely fragmented we could end up with really
10375 * small allocations, so if the allocator is returning small
10376 * chunks lets make its job easier by only searching for those
10377 * sized chunks.
10378 */
10379 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010380 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10381 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010382 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010383 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010384 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010385 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010386 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010387 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010388
Josef Bacik0b670dc2015-09-23 17:11:16 -040010389 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010390 ret = insert_reserved_file_extent(trans, inode,
10391 cur_offset, ins.objectid,
10392 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010393 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010394 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010395 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010396 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010397 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010398 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010399 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010400 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010401 break;
10402 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010403
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010404 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010405 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010406
Josef Bacik5dc562c2012-08-17 13:14:17 -040010407 em = alloc_extent_map();
10408 if (!em) {
10409 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10410 &BTRFS_I(inode)->runtime_flags);
10411 goto next;
10412 }
10413
10414 em->start = cur_offset;
10415 em->orig_start = cur_offset;
10416 em->len = ins.offset;
10417 em->block_start = ins.objectid;
10418 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010419 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010420 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010421 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010422 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10423 em->generation = trans->transid;
10424
10425 while (1) {
10426 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010427 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010428 write_unlock(&em_tree->lock);
10429 if (ret != -EEXIST)
10430 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010431 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010432 cur_offset + ins.offset - 1,
10433 0);
10434 }
10435 free_extent_map(em);
10436next:
Yan Zhengd899e052008-10-30 14:25:28 -040010437 num_bytes -= ins.offset;
10438 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010439 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010440
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010441 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010442 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010443 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010444 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010445 (actual_len > inode->i_size) &&
10446 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010447 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010448 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010449 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010450 i_size = cur_offset;
10451 i_size_write(inode, i_size);
10452 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010453 }
10454
Yan Zhengd899e052008-10-30 14:25:28 -040010455 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010456
10457 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010458 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010459 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010460 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010461 break;
10462 }
Yan Zhengd899e052008-10-30 14:25:28 -040010463
Josef Bacik0af3d002010-06-21 14:48:16 -040010464 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010465 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010466 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010467 if (cur_offset < end)
10468 btrfs_free_reserved_data_space(inode, cur_offset,
10469 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010470 return ret;
10471}
10472
Josef Bacik0af3d002010-06-21 14:48:16 -040010473int btrfs_prealloc_file_range(struct inode *inode, int mode,
10474 u64 start, u64 num_bytes, u64 min_size,
10475 loff_t actual_len, u64 *alloc_hint)
10476{
10477 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10478 min_size, actual_len, alloc_hint,
10479 NULL);
10480}
10481
10482int btrfs_prealloc_file_range_trans(struct inode *inode,
10483 struct btrfs_trans_handle *trans, int mode,
10484 u64 start, u64 num_bytes, u64 min_size,
10485 loff_t actual_len, u64 *alloc_hint)
10486{
10487 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10488 min_size, actual_len, alloc_hint, trans);
10489}
10490
Chris Masone6dcd2d2008-07-17 12:53:50 -040010491static int btrfs_set_page_dirty(struct page *page)
10492{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010493 return __set_page_dirty_nobuffers(page);
10494}
10495
Al Viro10556cb22011-06-20 19:28:19 -040010496static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010497{
Li Zefanb83cc962010-12-20 16:04:08 +080010498 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010499 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010500
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010501 if (mask & MAY_WRITE &&
10502 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10503 if (btrfs_root_readonly(root))
10504 return -EROFS;
10505 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10506 return -EACCES;
10507 }
Al Viro2830ba72011-06-20 19:16:29 -040010508 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010509}
Chris Mason39279cc2007-06-12 06:35:45 -040010510
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010511static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10512{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010513 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010514 struct btrfs_trans_handle *trans;
10515 struct btrfs_root *root = BTRFS_I(dir)->root;
10516 struct inode *inode = NULL;
10517 u64 objectid;
10518 u64 index;
10519 int ret = 0;
10520
10521 /*
10522 * 5 units required for adding orphan entry
10523 */
10524 trans = btrfs_start_transaction(root, 5);
10525 if (IS_ERR(trans))
10526 return PTR_ERR(trans);
10527
10528 ret = btrfs_find_free_ino(root, &objectid);
10529 if (ret)
10530 goto out;
10531
10532 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010533 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010534 if (IS_ERR(inode)) {
10535 ret = PTR_ERR(inode);
10536 inode = NULL;
10537 goto out;
10538 }
10539
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010540 inode->i_fop = &btrfs_file_operations;
10541 inode->i_op = &btrfs_file_inode_operations;
10542
10543 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10545
Chris Masonb0d5d102014-09-08 13:08:51 -070010546 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10547 if (ret)
10548 goto out_inode;
10549
10550 ret = btrfs_update_inode(trans, root, inode);
10551 if (ret)
10552 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010553 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010554 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010555 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010556
Filipe Manana5762b5c2014-08-01 00:10:32 +010010557 /*
10558 * We set number of links to 0 in btrfs_new_inode(), and here we set
10559 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10560 * through:
10561 *
10562 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10563 */
10564 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010565 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010566 d_tmpfile(dentry, inode);
10567 mark_inode_dirty(inode);
10568
10569out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010570 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010571 if (ret)
10572 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010573 btrfs_balance_delayed_items(fs_info);
10574 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010575 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010576
10577out_inode:
10578 unlock_new_inode(inode);
10579 goto out;
10580
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010581}
10582
David Sterba20a7db82017-02-17 16:24:29 +010010583__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010584static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010585{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010586 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010587}
10588
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010589static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010590 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010591 .lookup = btrfs_lookup,
10592 .create = btrfs_create,
10593 .unlink = btrfs_unlink,
10594 .link = btrfs_link,
10595 .mkdir = btrfs_mkdir,
10596 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010597 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010598 .symlink = btrfs_symlink,
10599 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010600 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010601 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010602 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010603 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010604 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010605 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010606 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010607};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010608static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010609 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010610 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010611 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010612};
Yan, Zheng76dda932009-09-21 16:00:26 -040010613
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010614static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010615 .llseek = generic_file_llseek,
10616 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010617 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010618 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010619#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010620 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010621#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010622 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010623 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010624};
10625
David Sterba20e55062015-11-19 11:42:28 +010010626static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010627 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010628 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010629 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010630 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010631 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010632
10633 /* optional callbacks */
10634 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010635 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010636 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010637 .set_bit_hook = btrfs_set_bit_hook,
10638 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010639 .merge_extent_hook = btrfs_merge_extent_hook,
10640 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010641};
10642
Chris Mason35054392009-01-21 13:11:13 -050010643/*
10644 * btrfs doesn't support the bmap operation because swapfiles
10645 * use bmap to make a mapping of extents in the file. They assume
10646 * these extents won't change over the life of the file and they
10647 * use the bmap result to do IO directly to the drive.
10648 *
10649 * the btrfs bmap call would return logical addresses that aren't
10650 * suitable for IO and they also will change frequently as COW
10651 * operations happen. So, swapfile + btrfs == corruption.
10652 *
10653 * For now we're avoiding this by dropping bmap.
10654 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010655static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010656 .readpage = btrfs_readpage,
10657 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010658 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010659 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010660 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010661 .invalidatepage = btrfs_invalidatepage,
10662 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010663 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010664 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010665};
10666
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010667static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010668 .readpage = btrfs_readpage,
10669 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010670 .invalidatepage = btrfs_invalidatepage,
10671 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010672};
10673
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010674static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010675 .getattr = btrfs_getattr,
10676 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010677 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010678 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010679 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010680 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010681 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010682 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010683};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010684static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010685 .getattr = btrfs_getattr,
10686 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010687 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010688 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010689 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010690 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010691 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010692};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010693static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010694 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010695 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010696 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010697 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010698 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010699 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010700};
Yan, Zheng76dda932009-09-21 16:00:26 -040010701
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010702const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010703 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010704 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010705};