blob: a21a984d84d999b6ffd4745d249455b0237d6cb9 [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;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
138 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
139 bytes - PAGE_SIZE, false);
140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
180 int err = 0;
181 int ret;
182 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400183 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400184
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400186 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189
190 if (!extent_inserted) {
191 struct btrfs_key key;
192 size_t datasize;
193
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200194 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200196 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197
198 datasize = btrfs_file_extent_calc_inline_size(cur_size);
199 path->leave_spinning = 1;
200 ret = btrfs_insert_empty_item(trans, root, path, &key,
201 datasize);
202 if (ret) {
203 err = ret;
204 goto fail;
205 }
Chris Masonc8b97812008-10-29 14:49:59 -0400206 }
207 leaf = path->nodes[0];
208 ei = btrfs_item_ptr(leaf, path->slots[0],
209 struct btrfs_file_extent_item);
210 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
211 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
212 btrfs_set_file_extent_encryption(leaf, ei, 0);
213 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
214 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
215 ptr = btrfs_file_extent_inline_start(ei);
216
Li Zefan261507a02010-12-17 14:21:50 +0800217 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct page *cpage;
219 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500220 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400221 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500222 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300223 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400226 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800227 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400228
229 i++;
230 ptr += cur_size;
231 compressed_size -= cur_size;
232 }
233 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800234 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 } else {
236 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800242 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300243 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 }
245 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000246 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400247
Yan, Zhengc2167752009-11-12 09:34:21 +0000248 /*
249 * we're an inline extent, so nobody can
250 * extend the file past i_size without locking
251 * a page we already have locked.
252 *
253 * We must do any isize and inode updates
254 * before we unlock the pages. Otherwise we
255 * could end up racing with unlink.
256 */
Chris Masonc8b97812008-10-29 14:49:59 -0400257 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100258 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000259
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400261fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400262 return err;
263}
264
265
266/*
267 * conditionally insert an inline extent into the file. This
268 * does the checks required to make sure the data is small enough
269 * to fit as an inline extent.
270 */
Josef Bacik00361582013-08-14 14:02:47 -0400271static noinline int cow_file_range_inline(struct btrfs_root *root,
272 struct inode *inode, u64 start,
273 u64 end, size_t compressed_size,
274 int compress_type,
275 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400276{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400278 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400279 u64 isize = i_size_read(inode);
280 u64 actual_end = min(end + 1, isize);
281 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400282 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400283 u64 data_len = inline_len;
284 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 struct btrfs_path *path;
286 int extent_inserted = 0;
287 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400288
289 if (compressed_size)
290 data_len = compressed_size;
291
292 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 actual_end > fs_info->sectorsize ||
294 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400295 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400296 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400297 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400299 return 1;
300 }
301
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 path = btrfs_alloc_path();
303 if (!path)
304 return -ENOMEM;
305
Josef Bacik00361582013-08-14 14:02:47 -0400306 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 if (IS_ERR(trans)) {
308 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400309 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400311 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400312
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 if (compressed_size && compressed_pages)
314 extent_item_size = btrfs_file_extent_calc_inline_size(
315 compressed_size);
316 else
317 extent_item_size = btrfs_file_extent_calc_inline_size(
318 inline_len);
319
320 ret = __btrfs_drop_extents(trans, root, inode, path,
321 start, aligned_end, NULL,
322 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400323 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400324 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400325 goto out;
326 }
Chris Masonc8b97812008-10-29 14:49:59 -0400327
328 if (isize > actual_end)
329 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000330 ret = insert_inline_extent(trans, path, extent_inserted,
331 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400332 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000333 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400334 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400335 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400336 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400337 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400338 ret = 1;
339 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400341
Josef Bacikbdc20e62013-02-28 13:23:38 -0500342 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200343 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200344 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400345out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800346 /*
347 * Don't forget to free the reserved space, as for inlined extent
348 * it won't count as data extent, free them directly here.
349 * And at reserve time, it's always aligned to page size, so
350 * just free one page here.
351 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300352 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000353 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400354 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400355 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400356}
357
Chris Mason771ed682008-11-06 22:02:51 -0500358struct async_extent {
359 u64 start;
360 u64 ram_size;
361 u64 compressed_size;
362 struct page **pages;
363 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800364 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500365 struct list_head list;
366};
367
368struct async_cow {
369 struct inode *inode;
370 struct btrfs_root *root;
371 struct page *locked_page;
372 u64 start;
373 u64 end;
374 struct list_head extents;
375 struct btrfs_work work;
376};
377
378static noinline int add_async_extent(struct async_cow *cow,
379 u64 start, u64 ram_size,
380 u64 compressed_size,
381 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800382 unsigned long nr_pages,
383 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500384{
385 struct async_extent *async_extent;
386
387 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100388 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500389 async_extent->start = start;
390 async_extent->ram_size = ram_size;
391 async_extent->compressed_size = compressed_size;
392 async_extent->pages = pages;
393 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800394 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500395 list_add_tail(&async_extent->list, &cow->extents);
396 return 0;
397}
398
Wang Shilongf79707b2014-07-17 11:44:09 +0800399static inline int inode_need_compress(struct inode *inode)
400{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800402
403 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400404 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800405 return 1;
406 /* bad compression ratios */
407 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
408 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800410 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
411 BTRFS_I(inode)->force_compress)
412 return 1;
413 return 0;
414}
415
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200416static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800417 u64 start, u64 end, u64 num_bytes, u64 small_write)
418{
419 /* If this is a small write inside eof, kick off a defrag */
420 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200421 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800422 btrfs_add_inode_defrag(NULL, inode);
423}
424
Chris Masonc8b97812008-10-29 14:49:59 -0400425/*
Chris Mason771ed682008-11-06 22:02:51 -0500426 * we create compressed extents in two phases. The first
427 * phase compresses a range of pages that have already been
428 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400429 *
Chris Mason771ed682008-11-06 22:02:51 -0500430 * This is done inside an ordered work queue, and the compression
431 * is spread across many cpus. The actual IO submission is step
432 * two, and the ordered work queue takes care of making sure that
433 * happens in the same order things were put onto the queue by
434 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400435 *
Chris Mason771ed682008-11-06 22:02:51 -0500436 * If this code finds it can't get good compression, it puts an
437 * entry onto the work queue to write the uncompressed bytes. This
438 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300439 * are written in the same order that the flusher thread sent them
440 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400441 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100442static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500443 struct page *locked_page,
444 u64 start, u64 end,
445 struct async_cow *async_cow,
446 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400449 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400450 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400451 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400452 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500453 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400454 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400455 struct page **pages = NULL;
456 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400457 unsigned long total_compressed = 0;
458 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400459 int i;
460 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400462 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400463
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200464 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
465 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400466
Chris Mason42dc7ba2008-12-15 11:44:56 -0500467 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400468again:
469 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300470 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100471 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
472 nr_pages = min_t(unsigned long, nr_pages,
473 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400474
Chris Masonf03d9301f2009-02-04 09:31:06 -0500475 /*
476 * we don't want to send crud past the end of i_size through
477 * compression, that's just a waste of CPU time. So, if the
478 * end of the file is before the start of our current
479 * requested range of bytes, we bail out to the uncompressed
480 * cleanup code that can deal with all of this.
481 *
482 * It isn't really the fastest way to fix things, but this is a
483 * very uncommon corner.
484 */
485 if (actual_end <= start)
486 goto cleanup_and_bail_uncompressed;
487
Chris Masonc8b97812008-10-29 14:49:59 -0400488 total_compressed = actual_end - start;
489
Shilong Wang4bcbb332014-10-07 18:44:35 -0400490 /*
491 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400492 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400493 */
494 if (total_compressed <= blocksize &&
495 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
496 goto cleanup_and_bail_uncompressed;
497
David Sterba069eac72017-02-14 19:36:54 +0100498 total_compressed = min_t(unsigned long, total_compressed,
499 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000500 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500501 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 total_in = 0;
503 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400504
Chris Mason771ed682008-11-06 22:02:51 -0500505 /*
506 * we do compression for mount -o compress and when the
507 * inode has not been flagged as nocompress. This flag can
508 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400509 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800510 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400511 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100512 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800513 if (!pages) {
514 /* just bail out to the uncompressed code */
515 goto cont;
516 }
Chris Mason179e29e2007-11-01 11:28:41 -0400517
Li Zefan261507a02010-12-17 14:21:50 +0800518 if (BTRFS_I(inode)->force_compress)
519 compress_type = BTRFS_I(inode)->force_compress;
520
Chris Mason4adaa612013-03-26 13:07:00 -0400521 /*
522 * we need to call clear_page_dirty_for_io on each
523 * page in the range. Otherwise applications with the file
524 * mmap'd can wander in and change the page contents while
525 * we are compressing them.
526 *
527 * If the compression fails for any reason, we set the pages
528 * dirty again later on.
529 */
530 extent_range_clear_dirty_for_io(inode, start, end);
531 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800532 ret = btrfs_compress_pages(compress_type,
533 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100534 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100535 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800536 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100537 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400538
539 if (!ret) {
540 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300541 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100542 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400543 char *kaddr;
544
545 /* zero the tail end of the last page, we might be
546 * sending it down to disk
547 */
548 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800549 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300551 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800552 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400553 }
554 will_compress = 1;
555 }
556 }
Li Zefan560f7d72011-09-08 10:22:01 +0800557cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400558 if (start == 0) {
559 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500560 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400561 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500562 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400563 */
Josef Bacik00361582013-08-14 14:02:47 -0400564 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800565 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400566 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500567 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400568 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000569 total_compressed,
570 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100572 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400573 unsigned long clear_flags = EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100574 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100575 unsigned long page_error_op;
576
Josef Bacik151a41b2013-07-29 13:22:24 -0400577 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100578 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400579
Chris Mason771ed682008-11-06 22:02:51 -0500580 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100581 * inline extent creation worked or returned error,
582 * we don't need to create any more async work items.
583 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500584 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800585 extent_clear_unlock_delalloc(inode, start, end, end,
586 NULL, clear_flags,
587 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400588 PAGE_CLEAR_DIRTY |
589 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100590 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400591 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000592 if (ret == 0)
593 btrfs_free_reserved_data_space_noquota(inode,
594 start,
595 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 goto free_pages_out;
597 }
598 }
599
600 if (will_compress) {
601 /*
602 * we aren't doing an inline extent round the compressed size
603 * up to a block size boundary so the allocator does sane
604 * things
605 */
Qu Wenruofda28322013-02-26 08:10:22 +0000606 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400607
608 /*
609 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300610 * win, compare the page count read with the blocks on disk,
611 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400612 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300613 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300614 if (total_compressed + blocksize <= total_in) {
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 |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100880 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400881 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100883 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
884 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100885 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500888}
889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
891 u64 num_bytes)
892{
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 struct extent_map *em;
895 u64 alloc_hint = 0;
896
897 read_lock(&em_tree->lock);
898 em = search_extent_mapping(em_tree, start, num_bytes);
899 if (em) {
900 /*
901 * if block start isn't an actual block number then find the
902 * first block in this inode and use that as a hint. If that
903 * block is also bogus then just don't worry about it.
904 */
905 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
906 free_extent_map(em);
907 em = search_extent_mapping(em_tree, 0, 0);
908 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
909 alloc_hint = em->block_start;
910 if (em)
911 free_extent_map(em);
912 } else {
913 alloc_hint = em->block_start;
914 free_extent_map(em);
915 }
916 }
917 read_unlock(&em_tree->lock);
918
919 return alloc_hint;
920}
921
Chris Mason771ed682008-11-06 22:02:51 -0500922/*
923 * when extent_io.c finds a delayed allocation range in the file,
924 * the call backs end up in this code. The basic idea is to
925 * allocate extents on disk for the range, and create ordered data structs
926 * in ram to track those extents.
927 *
928 * locked_page is the page that writepage had locked already. We use
929 * it to make sure we don't do extra locks or unlocks.
930 *
931 * *page_started is set to one if we unlock locked_page and do everything
932 * required to start IO on it. It may be clean and already done with
933 * IO when we return.
934 */
Josef Bacik00361582013-08-14 14:02:47 -0400935static noinline int cow_file_range(struct inode *inode,
936 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800937 u64 start, u64 end, u64 delalloc_end,
938 int *page_started, unsigned long *nr_written,
939 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
946 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400948 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500949 struct btrfs_key ins;
950 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000951 unsigned clear_bits;
952 unsigned long page_ops;
953 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500954 int ret = 0;
955
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200956 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400957 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500958 ret = -EINVAL;
959 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400960 }
Chris Mason771ed682008-11-06 22:02:51 -0500961
Qu Wenruofda28322013-02-26 08:10:22 +0000962 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500963 num_bytes = max(blocksize, num_bytes);
964 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500965
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200966 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400967
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (start == 0) {
969 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800970 ret = cow_file_range_inline(root, inode, start, end, 0,
971 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500972 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800973 extent_clear_unlock_delalloc(inode, start, end,
974 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400975 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100976 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400977 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400978 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
979 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 btrfs_free_reserved_data_space_noquota(inode, start,
981 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
991 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400992 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200995 btrfs_drop_extent_cache(BTRFS_I(inode), start,
996 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Chris Masond3977122009-01-05 21:25:51 -0500998 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +0000999 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001000 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001001 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001002 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001003 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001005 cur_alloc_size = ins.offset;
1006 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001009 em = create_io_em(inode, start, ins.offset, /* len */
1010 start, /* orig_start */
1011 ins.objectid, /* block_start */
1012 ins.offset, /* block_len */
1013 ins.offset, /* orig_block_len */
1014 ram_size, /* ram_bytes */
1015 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001016 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001017 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001018 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001019 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001023 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001024 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (root->root_key.objectid ==
1027 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1028 ret = btrfs_reloc_clone_csums(inode, start,
1029 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001030 /*
1031 * Only drop cache here, and process as normal.
1032 *
1033 * We must not allow extent_clear_unlock_delalloc()
1034 * at out_unlock label to free meta of this ordered
1035 * extent, as its meta should be freed by
1036 * btrfs_finish_ordered_io().
1037 *
1038 * So we must continue until @start is increased to
1039 * skip current ordered extent.
1040 */
Josef Bacik00361582013-08-14 14:02:47 -04001041 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001042 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1043 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 }
1045
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001046 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001047
Chris Masonc8b97812008-10-29 14:49:59 -04001048 /* we're not doing compressed IO, don't unlock the first
1049 * page (which the caller expects to stay locked), don't
1050 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001051 *
1052 * Do set the Private2 bit so we know this page was properly
1053 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001054 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001055 page_ops = unlock ? PAGE_UNLOCK : 0;
1056 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001057
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001059 start + ram_size - 1,
1060 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001062 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 if (disk_num_bytes < cur_alloc_size)
1064 disk_num_bytes = 0;
1065 else
1066 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001067 num_bytes -= cur_alloc_size;
1068 alloc_hint = ins.objectid + ins.offset;
1069 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001070 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071
1072 /*
1073 * btrfs_reloc_clone_csums() error, since start is increased
1074 * extent_clear_unlock_delalloc() at out_unlock label won't
1075 * free metadata of current ordered extent, we're OK to exit.
1076 */
1077 if (ret)
1078 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001080out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001081 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001082
Filipe Mananad9f85962014-08-25 10:43:00 +01001083out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001084 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001085out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001086 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001087 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001089 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1090 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1092 PAGE_END_WRITEBACK;
1093 /*
1094 * If we reserved an extent for our delalloc range (or a subrange) and
1095 * failed to create the respective ordered extent, then it means that
1096 * when we reserved the extent we decremented the extent's size from
1097 * the data space_info's bytes_may_use counter and incremented the
1098 * space_info's bytes_reserved counter by the same amount. We must make
1099 * sure extent_clear_unlock_delalloc() does not try to decrement again
1100 * the data space_info's bytes_may_use counter, therefore we do not pass
1101 * it the flag EXTENT_CLEAR_DATA_RESV.
1102 */
1103 if (extent_reserved) {
1104 extent_clear_unlock_delalloc(inode, start,
1105 start + cur_alloc_size,
1106 start + cur_alloc_size,
1107 locked_page,
1108 clear_bits,
1109 page_ops);
1110 start += cur_alloc_size;
1111 if (start >= end)
1112 goto out;
1113 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001114 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1115 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001116 clear_bits | EXTENT_CLEAR_DATA_RESV,
1117 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001119}
Chris Masonc8b97812008-10-29 14:49:59 -04001120
Chris Mason771ed682008-11-06 22:02:51 -05001121/*
1122 * work queue call back to started compression on a file and pages
1123 */
1124static noinline void async_cow_start(struct btrfs_work *work)
1125{
1126 struct async_cow *async_cow;
1127 int num_added = 0;
1128 async_cow = container_of(work, struct async_cow, work);
1129
1130 compress_file_range(async_cow->inode, async_cow->locked_page,
1131 async_cow->start, async_cow->end, async_cow,
1132 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001134 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001135 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 }
Chris Mason771ed682008-11-06 22:02:51 -05001137}
1138
1139/*
1140 * work queue call back to submit previously compressed pages
1141 */
1142static noinline void async_cow_submit(struct btrfs_work *work)
1143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001145 struct async_cow *async_cow;
1146 struct btrfs_root *root;
1147 unsigned long nr_pages;
1148
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001153 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1154 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001155
David Sterbaee863952015-02-16 19:41:40 +01001156 /*
1157 * atomic_sub_return implies a barrier for waitqueue_active
1158 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001160 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 waitqueue_active(&fs_info->async_submit_wait))
1162 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001163
Chris Masond3977122009-01-05 21:25:51 -05001164 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001165 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001166}
Chris Masonc8b97812008-10-29 14:49:59 -04001167
Chris Mason771ed682008-11-06 22:02:51 -05001168static noinline void async_cow_free(struct btrfs_work *work)
1169{
1170 struct async_cow *async_cow;
1171 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001172 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001173 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001174 kfree(async_cow);
1175}
1176
1177static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1178 u64 start, u64 end, int *page_started,
1179 unsigned long *nr_written)
1180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001182 struct async_cow *async_cow;
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
1184 unsigned long nr_pages;
1185 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001186
Chris Masona3429ab2009-10-08 12:30:20 -04001187 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1188 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001189 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001190 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001192 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001193 async_cow->root = root;
1194 async_cow->locked_page = locked_page;
1195 async_cow->start = start;
1196
Wang Shilongf79707b2014-07-17 11:44:09 +08001197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001199 cur_end = end;
1200 else
Byongho Leeee221842015-12-15 01:42:10 +09001201 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001202
1203 async_cow->end = cur_end;
1204 INIT_LIST_HEAD(&async_cow->extents);
1205
Liu Bo9e0af232014-08-15 23:36:53 +08001206 btrfs_init_work(&async_cow->work,
1207 btrfs_delalloc_helper,
1208 async_cow_start, async_cow_submit,
1209 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 nr_pages = (cur_end - start + PAGE_SIZE) >>
1212 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 while (atomic_read(&fs_info->async_submit_draining) &&
1218 atomic_read(&fs_info->async_delalloc_pages)) {
1219 wait_event(fs_info->async_submit_wait,
1220 (atomic_read(&fs_info->async_delalloc_pages) ==
1221 0));
Chris Mason771ed682008-11-06 22:02:51 -05001222 }
1223
1224 *nr_written += nr_pages;
1225 start = cur_end + 1;
1226 }
1227 *page_started = 1;
1228 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229}
1230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001231static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001232 u64 bytenr, u64 num_bytes)
1233{
1234 int ret;
1235 struct btrfs_ordered_sum *sums;
1236 LIST_HEAD(list);
1237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001239 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 if (ret == 0 && list_empty(&list))
1241 return 0;
1242
1243 while (!list_empty(&list)) {
1244 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1245 list_del(&sums->list);
1246 kfree(sums);
1247 }
1248 return 1;
1249}
1250
Chris Masond352ac62008-09-29 15:18:18 -04001251/*
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1254 *
1255 * If no cow copies or snapshots exist, we write directly to the existing
1256 * blocks on disk
1257 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001258static noinline int run_delalloc_nocow(struct inode *inode,
1259 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started, int force,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001269 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 cow_start;
1271 u64 cur_offset;
1272 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 u64 disk_bytenr;
1275 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001276 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001277 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001280 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 int nocow;
1282 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001283 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001284 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
1286 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001287 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001296 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001297 }
Li Zefan82d59022011-04-20 10:33:24 +08001298
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001299 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cow_start = (u64)-1;
1302 cur_offset = start;
1303 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001306 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key,
1311 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.type == BTRFS_EXTENT_DATA_KEY)
1314 path->slots[0]--;
1315 }
1316 check_prev = 0;
1317next_slot:
1318 leaf = path->nodes[0];
1319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1320 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001321 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 if (ret > 0)
1324 break;
1325 leaf = path->nodes[0];
1326 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 nocow = 0;
1329 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
Filipe Manana1d512cb2015-11-09 00:33:58 +00001333 if (found_key.objectid > ino)
1334 break;
1335 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1336 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1337 path->slots[0]++;
1338 goto next_slot;
1339 }
1340 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 found_key.offset > end)
1342 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (found_key.offset > cur_offset) {
1345 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001346 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto out_check;
1348 }
Chris Masonc31f8832008-01-08 15:46:31 -05001349
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 fi = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_file_extent_item);
1352 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Josef Bacikcc95bef2013-04-04 14:31:27 -04001354 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001355 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1356 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 extent_end = found_key.offset +
1360 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001361 disk_num_bytes =
1362 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 if (extent_end <= start) {
1364 path->slots[0]++;
1365 goto next_slot;
1366 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 if (disk_bytenr == 0)
1368 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 if (btrfs_file_extent_compression(leaf, fi) ||
1370 btrfs_file_extent_encryption(leaf, fi) ||
1371 btrfs_file_extent_other_encoding(leaf, fi))
1372 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1374 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001375 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001377 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 found_key.offset -
1379 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001380 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001381 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 disk_bytenr += cur_offset - found_key.offset;
1383 num_bytes = min(end + 1, extent_end) - cur_offset;
1384 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001385 * if there are pending snapshots for this root,
1386 * we fall into common COW way.
1387 */
1388 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001389 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (!err)
1391 goto out_check;
1392 }
1393 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 * force cow if csum exists in the range.
1395 * this ensure that csum for a given extent are
1396 * either valid or do not exist.
1397 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001398 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001399 num_bytes)) {
1400 if (!nolock)
1401 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001403 }
1404 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1405 if (!nolock)
1406 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001407 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 nocow = 1;
1410 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1411 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001412 btrfs_file_extent_inline_len(leaf,
1413 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001414 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001415 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 } else {
1417 BUG_ON(1);
1418 }
1419out_check:
1420 if (extent_end <= start) {
1421 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001423 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001424 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001425 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 goto next_slot;
1427 }
1428 if (!nocow) {
1429 if (cow_start == (u64)-1)
1430 cow_start = cur_offset;
1431 cur_offset = extent_end;
1432 if (cur_offset > end)
1433 break;
1434 path->slots[0]++;
1435 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001437
David Sterbab3b4aa72011-04-21 01:20:15 +02001438 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001440 ret = cow_file_range(inode, locked_page,
1441 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001442 end, page_started, nr_written, 1,
1443 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 if (ret) {
1445 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001446 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001448 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454
Yan Zhengd899e052008-10-30 14:25:28 -04001455 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001456 u64 orig_start = found_key.offset - extent_offset;
1457
1458 em = create_io_em(inode, cur_offset, num_bytes,
1459 orig_start,
1460 disk_bytenr, /* block_start */
1461 num_bytes, /* block_len */
1462 disk_num_bytes, /* orig_block_len */
1463 ram_bytes, BTRFS_COMPRESS_NONE,
1464 BTRFS_ORDERED_PREALLOC);
1465 if (IS_ERR(em)) {
1466 if (!nolock && nocow)
1467 btrfs_end_write_no_snapshoting(root);
1468 if (nocow)
1469 btrfs_dec_nocow_writers(fs_info,
1470 disk_bytenr);
1471 ret = PTR_ERR(em);
1472 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001474 free_extent_map(em);
1475 }
1476
1477 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001478 type = BTRFS_ORDERED_PREALLOC;
1479 } else {
1480 type = BTRFS_ORDERED_NOCOW;
1481 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001482
1483 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001484 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001485 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001488
Yan, Zhengefa56462010-05-16 10:49:59 -04001489 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001490 BTRFS_DATA_RELOC_TREE_OBJECTID)
1491 /*
1492 * Error handled later, as we must prevent
1493 * extent_clear_unlock_delalloc() in error handler
1494 * from freeing metadata of created ordered extent.
1495 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001496 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1497 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001498
Josef Bacikc2790a22013-07-29 11:20:47 -04001499 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001500 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001501 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001502 EXTENT_DELALLOC |
1503 EXTENT_CLEAR_DATA_RESV,
1504 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1505
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001507 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001508 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001509
1510 /*
1511 * btrfs_reloc_clone_csums() error, now we're OK to call error
1512 * handler, as metadata for created ordered extent will only
1513 * be freed by btrfs_finish_ordered_io().
1514 */
1515 if (ret)
1516 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 if (cur_offset > end)
1518 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001519 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001521
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 cur_offset = end;
1525 }
1526
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001528 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1529 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001530 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001531 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 }
1533
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001534error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001536 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001538 EXTENT_DELALLOC | EXTENT_DEFRAG |
1539 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1540 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 PAGE_SET_WRITEBACK |
1542 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001543 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001545}
1546
Wang Shilong47059d92014-07-03 18:22:07 +08001547static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1548{
1549
1550 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1551 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1552 return 0;
1553
1554 /*
1555 * @defrag_bytes is a hint value, no spinlock held here,
1556 * if is not zero, it means the file is defragging.
1557 * Force cow if given extent needs to be defragged.
1558 */
1559 if (BTRFS_I(inode)->defrag_bytes &&
1560 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1561 EXTENT_DEFRAG, 0, NULL))
1562 return 1;
1563
1564 return 0;
1565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * extent_io.c call back to do delayed allocation processing
1569 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001570static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001571 u64 start, u64 end, int *page_started,
1572 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001573{
Josef Bacikc6100a42017-05-05 11:57:13 -04001574 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001575 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001576 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001577
Wang Shilong47059d92014-07-03 18:22:07 +08001578 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001579 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001580 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001581 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001582 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001583 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001584 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001585 ret = cow_file_range(inode, locked_page, start, end, end,
1586 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001587 } else {
1588 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1589 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001590 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001591 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001592 }
Qu Wenruo524272602017-03-08 10:25:52 +08001593 if (ret)
1594 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001595 return ret;
1596}
1597
Josef Bacikc6100a42017-05-05 11:57:13 -04001598static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001599 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001600{
Josef Bacikc6100a42017-05-05 11:57:13 -04001601 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001602 u64 size;
1603
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001606 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607
Josef Bacikdcab6a32015-02-11 15:08:59 -05001608 size = orig->end - orig->start + 1;
1609 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001610 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001611 u64 new_size;
1612
1613 /*
Josef Bacikba117212015-03-13 15:01:24 -04001614 * See the explanation in btrfs_merge_extent_hook, the same
1615 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616 */
1617 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001618 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001619 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001620 num_extents += count_max_extents(new_size);
1621 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001622 return;
1623 }
1624
Josef Bacik9e0baf62011-07-15 15:16:44 +00001625 spin_lock(&BTRFS_I(inode)->lock);
1626 BTRFS_I(inode)->outstanding_extents++;
1627 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001628}
1629
1630/*
1631 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1632 * extents so we can keep track of new extents that are just merged onto old
1633 * extents, such as when we are doing sequential writes, so we can properly
1634 * account for the metadata space we'll need.
1635 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001636static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001637 struct extent_state *new,
1638 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001639{
Josef Bacikc6100a42017-05-05 11:57:13 -04001640 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001642 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 /* not delalloc, ignore it */
1645 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001646 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647
Josef Bacik8461a3d2015-03-13 15:12:08 -04001648 if (new->start > other->start)
1649 new_size = new->end - other->start + 1;
1650 else
1651 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652
1653 /* we're not bigger than the max, unreserve the space and go */
1654 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1655 spin_lock(&BTRFS_I(inode)->lock);
1656 BTRFS_I(inode)->outstanding_extents--;
1657 spin_unlock(&BTRFS_I(inode)->lock);
1658 return;
1659 }
1660
1661 /*
Josef Bacikba117212015-03-13 15:01:24 -04001662 * We have to add up either side to figure out how many extents were
1663 * accounted for before we merged into one big extent. If the number of
1664 * extents we accounted for is <= the amount we need for the new range
1665 * then we can return, otherwise drop. Think of it like this
1666 *
1667 * [ 4k][MAX_SIZE]
1668 *
1669 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1670 * need 2 outstanding extents, on one side we have 1 and the other side
1671 * we have 1 so they are == and we can return. But in this case
1672 *
1673 * [MAX_SIZE+4k][MAX_SIZE+4k]
1674 *
1675 * Each range on their own accounts for 2 extents, but merged together
1676 * they are only 3 extents worth of accounting, so we need to drop in
1677 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 */
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001680 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001681 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001682 num_extents += count_max_extents(old_size);
1683 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 return;
1685
Josef Bacik9e0baf62011-07-15 15:16:44 +00001686 spin_lock(&BTRFS_I(inode)->lock);
1687 BTRFS_I(inode)->outstanding_extents--;
1688 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001689}
1690
Miao Xieeb73c1b2013-05-15 07:48:22 +00001691static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1692 struct inode *inode)
1693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1695
Miao Xieeb73c1b2013-05-15 07:48:22 +00001696 spin_lock(&root->delalloc_lock);
1697 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1698 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1699 &root->delalloc_inodes);
1700 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1701 &BTRFS_I(inode)->runtime_flags);
1702 root->nr_delalloc_inodes++;
1703 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001705 BUG_ON(!list_empty(&root->delalloc_root));
1706 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001707 &fs_info->delalloc_roots);
1708 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001709 }
1710 }
1711 spin_unlock(&root->delalloc_lock);
1712}
1713
1714static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001715 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001718
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001720 if (!list_empty(&inode->delalloc_inodes)) {
1721 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001722 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001723 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 root->nr_delalloc_inodes--;
1725 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 BUG_ON(list_empty(&root->delalloc_root));
1728 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730 }
1731 }
1732 spin_unlock(&root->delalloc_lock);
1733}
1734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * extent_io.c set_bit_hook, used to track delayed allocation
1737 * bytes in this file, and to maintain the list of inodes that
1738 * have pending delalloc work to be done.
1739 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001740static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001741 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001742{
Josef Bacikc6100a42017-05-05 11:57:13 -04001743 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001744
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1746
Wang Shilong47059d92014-07-03 18:22:07 +08001747 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1748 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001749 /*
1750 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001751 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001752 * bit, which is only set or cleared with irqs on
1753 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001755 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001757 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001758
Josef Bacik9e0baf62011-07-15 15:16:44 +00001759 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001760 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 } else {
1762 spin_lock(&BTRFS_I(inode)->lock);
1763 BTRFS_I(inode)->outstanding_extents++;
1764 spin_unlock(&BTRFS_I(inode)->lock);
1765 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001766
Josef Bacik6a3891c2015-03-16 17:38:52 -04001767 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001769 return;
1770
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001771 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1772 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001775 if (*bits & EXTENT_DEFRAG)
1776 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001782
1783 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1784 (*bits & EXTENT_DELALLOC_NEW)) {
1785 spin_lock(&BTRFS_I(inode)->lock);
1786 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1787 state->start;
1788 spin_unlock(&BTRFS_I(inode)->lock);
1789 }
Chris Mason291d6732008-01-29 15:55:23 -05001790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
1793 * extent_io.c clear_bit_hook, see set_bit_hook for why
1794 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001795static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001796 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001797 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001798{
Josef Bacikc6100a42017-05-05 11:57:13 -04001799 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001801 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001802 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001803
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001804 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001805 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001806 inode->defrag_bytes -= len;
1807 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001808
Chris Mason75eff682008-12-15 15:54:40 -05001809 /*
1810 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001811 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001812 * bit, which is only set or cleared with irqs on
1813 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001815 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001816 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001817
Josef Bacik9e0baf62011-07-15 15:16:44 +00001818 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001819 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001820 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001821 spin_lock(&inode->lock);
1822 inode->outstanding_extents -= num_extents;
1823 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001824 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001825
Josef Bacikb6d08f02013-09-27 14:57:43 -04001826 /*
1827 * We don't reserve metadata space for space cache inodes so we
1828 * don't need to call dellalloc_release_metadata if there is an
1829 * error.
1830 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001831 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001833 btrfs_delalloc_release_metadata(inode, len);
1834
Josef Bacik6a3891c2015-03-16 17:38:52 -04001835 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001836 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001837 return;
1838
Filipe Mananaa315e682017-03-06 23:04:20 +00001839 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1840 do_list && !(state->state & EXTENT_NORESERVE) &&
1841 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001842 btrfs_free_reserved_data_space_noquota(
1843 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001844 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001845
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001846 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1847 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001848 spin_lock(&inode->lock);
1849 inode->delalloc_bytes -= len;
1850 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001851 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001852 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001853 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001854 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001855 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001856
1857 if ((state->state & EXTENT_DELALLOC_NEW) &&
1858 (*bits & EXTENT_DELALLOC_NEW)) {
1859 spin_lock(&inode->lock);
1860 ASSERT(inode->new_delalloc_bytes >= len);
1861 inode->new_delalloc_bytes -= len;
1862 spin_unlock(&inode->lock);
1863 }
Chris Mason291d6732008-01-29 15:55:23 -05001864}
1865
Chris Masond352ac62008-09-29 15:18:18 -04001866/*
1867 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1868 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001869 *
1870 * return 1 if page cannot be merged to bio
1871 * return 0 if page can be merged to bio
1872 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001873 */
Mike Christie81a75f672016-06-05 14:31:54 -05001874int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001875 size_t size, struct bio *bio,
1876 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 struct inode *inode = page->mapping->host;
1879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001880 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001881 u64 length = 0;
1882 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001883 int ret;
1884
Chris Mason771ed682008-11-06 22:02:51 -05001885 if (bio_flags & EXTENT_BIO_COMPRESSED)
1886 return 0;
1887
Kent Overstreet4f024f32013-10-11 15:44:27 -07001888 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001889 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1891 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001892 if (ret < 0)
1893 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001894 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001895 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001896 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001897}
1898
Chris Masond352ac62008-09-29 15:18:18 -04001899/*
1900 * in order to insert checksums into the metadata in large chunks,
1901 * we wait until bio submission time. All the pages in the bio are
1902 * checksummed and sums are attached onto the ordered extent record.
1903 *
1904 * At IO completion time the cums attached on the ordered extent record
1905 * are inserted into the btree
1906 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001907static int __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001908 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001909 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001910{
Josef Bacikc6100a42017-05-05 11:57:13 -04001911 struct inode *inode = private_data;
Chris Mason065631f2008-02-20 12:07:25 -05001912 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001913
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001914 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001916 return 0;
1917}
Chris Masone0156402008-04-16 11:15:20 -04001918
Chris Mason4a69a412008-11-06 22:03:00 -05001919/*
1920 * in order to insert checksums into the metadata in large chunks,
1921 * we wait until bio submission time. All the pages in the bio are
1922 * checksummed and sums are attached onto the ordered extent record.
1923 *
1924 * At IO completion time the cums attached on the ordered extent record
1925 * are inserted into the btree
1926 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001927static int __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001928 int mirror_num, unsigned long bio_flags,
1929 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001930{
Josef Bacikc6100a42017-05-05 11:57:13 -04001931 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001933 int ret;
1934
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001935 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001936 if (ret) {
1937 bio->bi_error = ret;
1938 bio_endio(bio);
1939 }
Stefan Behrens61891922012-11-05 18:51:52 +01001940 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001941}
1942
Chris Masond352ac62008-09-29 15:18:18 -04001943/*
Chris Masoncad321a2008-12-17 14:51:42 -05001944 * extent_io.c submission hook. This does the right thing for csum calculation
1945 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001946 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001947static int btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1948 int mirror_num, unsigned long bio_flags,
1949 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001950{
Josef Bacikc6100a42017-05-05 11:57:13 -04001951 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301954 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001955 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001956 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001957 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001958
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001959 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001960
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001961 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301962 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001963
Mike Christie37226b22016-06-05 14:31:52 -05001964 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001966 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001967 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001968
Chris Masond20f7042008-12-08 16:58:54 -05001969 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001970 ret = btrfs_submit_compressed_read(inode, bio,
1971 mirror_num,
1972 bio_flags);
1973 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001974 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001975 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001976 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001978 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001979 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001981 /* csum items have already been cloned */
1982 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1983 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001984 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001985 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1986 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001987 __btrfs_submit_bio_start,
1988 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001989 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001990 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001991 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001992 if (ret)
1993 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001994 }
1995
Chris Mason0b86a832008-03-24 15:01:56 -04001996mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001997 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001998
1999out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002000 if (ret < 0) {
2001 bio->bi_error = ret;
2002 bio_endio(bio);
2003 }
Stefan Behrens61891922012-11-05 18:51:52 +01002004 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002005}
Chris Mason6885f302008-02-20 16:11:05 -05002006
Chris Masond352ac62008-09-29 15:18:18 -04002007/*
2008 * given a list of ordered sums record them in the inode. This happens
2009 * at IO completion time based on sums calculated at bio submission time.
2010 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002011static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002012 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002013{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002014 struct btrfs_ordered_sum *sum;
2015
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002016 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002017 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002018 btrfs_csum_file_blocks(trans,
2019 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002020 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002021 }
2022 return 0;
2023}
2024
Josef Bacik2ac55d42010-02-03 19:33:23 +00002025int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002026 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002027{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002029 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002030 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002031}
2032
Chris Masond352ac62008-09-29 15:18:18 -04002033/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002034struct btrfs_writepage_fixup {
2035 struct page *page;
2036 struct btrfs_work work;
2037};
2038
Christoph Hellwigb2950862008-12-02 09:54:17 -05002039static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002040{
2041 struct btrfs_writepage_fixup *fixup;
2042 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002043 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002044 struct page *page;
2045 struct inode *inode;
2046 u64 page_start;
2047 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002048 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002049
2050 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2051 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002052again:
Chris Mason247e7432008-07-17 12:53:51 -04002053 lock_page(page);
2054 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2055 ClearPageChecked(page);
2056 goto out_page;
2057 }
2058
2059 inode = page->mapping->host;
2060 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002061 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002062
David Sterbaff13db42015-12-03 14:30:40 +01002063 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002064 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002065
2066 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002067 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002068 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002069
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002070 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002072 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002073 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2074 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002075 unlock_page(page);
2076 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002077 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002078 goto again;
2079 }
Chris Mason247e7432008-07-17 12:53:51 -04002080
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002081 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002083 if (ret) {
2084 mapping_set_error(page->mapping, ret);
2085 end_extent_writepage(page, ret, page_start, page_end);
2086 ClearPageChecked(page);
2087 goto out;
2088 }
2089
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002090 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2091 0);
Chris Mason247e7432008-07-17 12:53:51 -04002092 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002093 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002094out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002095 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2096 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002097out_page:
2098 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002099 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002100 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002101}
2102
2103/*
2104 * There are a few paths in the higher layers of the kernel that directly
2105 * set the page dirty bit without asking the filesystem if it is a
2106 * good idea. This causes problems because we want to make sure COW
2107 * properly happens and the data=ordered rules are followed.
2108 *
Chris Masonc8b97812008-10-29 14:49:59 -04002109 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002110 * hasn't been properly setup for IO. We kick off an async process
2111 * to fix it up. The async helper will wait for ordered extents, set
2112 * the delalloc bit and make it safe to write the page.
2113 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002114static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002115{
2116 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002118 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002119
Chris Mason8b62b722009-09-02 16:53:46 -04002120 /* this page is properly in the ordered list */
2121 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002122 return 0;
2123
2124 if (PageChecked(page))
2125 return -EAGAIN;
2126
2127 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2128 if (!fixup)
2129 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002130
Chris Mason247e7432008-07-17 12:53:51 -04002131 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002132 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002133 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2134 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002135 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002136 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002137 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002138}
2139
Yan Zhengd899e052008-10-30 14:25:28 -04002140static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2141 struct inode *inode, u64 file_pos,
2142 u64 disk_bytenr, u64 disk_num_bytes,
2143 u64 num_bytes, u64 ram_bytes,
2144 u8 compression, u8 encryption,
2145 u16 other_encoding, int extent_type)
2146{
2147 struct btrfs_root *root = BTRFS_I(inode)->root;
2148 struct btrfs_file_extent_item *fi;
2149 struct btrfs_path *path;
2150 struct extent_buffer *leaf;
2151 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002152 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002153 int ret;
2154
2155 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002156 if (!path)
2157 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002158
Chris Masona1ed8352009-09-11 12:27:37 -04002159 /*
2160 * we may be replacing one extent in the tree with another.
2161 * The new extent is pinned in the extent map, and we don't want
2162 * to drop it from the cache until it is completely in the btree.
2163 *
2164 * So, tell btrfs_drop_extents to leave this extent in the cache.
2165 * the caller is expected to unpin it and allow it to be merged
2166 * with the others.
2167 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002168 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2169 file_pos + num_bytes, NULL, 0,
2170 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002171 if (ret)
2172 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002173
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002174 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002175 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002176 ins.offset = file_pos;
2177 ins.type = BTRFS_EXTENT_DATA_KEY;
2178
2179 path->leave_spinning = 1;
2180 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2181 sizeof(*fi));
2182 if (ret)
2183 goto out;
2184 }
Yan Zhengd899e052008-10-30 14:25:28 -04002185 leaf = path->nodes[0];
2186 fi = btrfs_item_ptr(leaf, path->slots[0],
2187 struct btrfs_file_extent_item);
2188 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2189 btrfs_set_file_extent_type(leaf, fi, extent_type);
2190 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2191 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2192 btrfs_set_file_extent_offset(leaf, fi, 0);
2193 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2194 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2195 btrfs_set_file_extent_compression(leaf, fi, compression);
2196 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2197 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002198
Yan Zhengd899e052008-10-30 14:25:28 -04002199 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002200 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002201
2202 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002203
2204 ins.objectid = disk_bytenr;
2205 ins.offset = disk_num_bytes;
2206 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002207 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002208 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002209 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002210 * Release the reserved range from inode dirty range map, as it is
2211 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002212 */
2213 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002214out:
Yan Zhengd899e052008-10-30 14:25:28 -04002215 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002216
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002217 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002218}
2219
Liu Bo38c227d2013-01-29 03:18:40 +00002220/* snapshot-aware defrag */
2221struct sa_defrag_extent_backref {
2222 struct rb_node node;
2223 struct old_sa_defrag_extent *old;
2224 u64 root_id;
2225 u64 inum;
2226 u64 file_pos;
2227 u64 extent_offset;
2228 u64 num_bytes;
2229 u64 generation;
2230};
2231
2232struct old_sa_defrag_extent {
2233 struct list_head list;
2234 struct new_sa_defrag_extent *new;
2235
2236 u64 extent_offset;
2237 u64 bytenr;
2238 u64 offset;
2239 u64 len;
2240 int count;
2241};
2242
2243struct new_sa_defrag_extent {
2244 struct rb_root root;
2245 struct list_head head;
2246 struct btrfs_path *path;
2247 struct inode *inode;
2248 u64 file_pos;
2249 u64 len;
2250 u64 bytenr;
2251 u64 disk_len;
2252 u8 compress_type;
2253};
2254
2255static int backref_comp(struct sa_defrag_extent_backref *b1,
2256 struct sa_defrag_extent_backref *b2)
2257{
2258 if (b1->root_id < b2->root_id)
2259 return -1;
2260 else if (b1->root_id > b2->root_id)
2261 return 1;
2262
2263 if (b1->inum < b2->inum)
2264 return -1;
2265 else if (b1->inum > b2->inum)
2266 return 1;
2267
2268 if (b1->file_pos < b2->file_pos)
2269 return -1;
2270 else if (b1->file_pos > b2->file_pos)
2271 return 1;
2272
2273 /*
2274 * [------------------------------] ===> (a range of space)
2275 * |<--->| |<---->| =============> (fs/file tree A)
2276 * |<---------------------------->| ===> (fs/file tree B)
2277 *
2278 * A range of space can refer to two file extents in one tree while
2279 * refer to only one file extent in another tree.
2280 *
2281 * So we may process a disk offset more than one time(two extents in A)
2282 * and locate at the same extent(one extent in B), then insert two same
2283 * backrefs(both refer to the extent in B).
2284 */
2285 return 0;
2286}
2287
2288static void backref_insert(struct rb_root *root,
2289 struct sa_defrag_extent_backref *backref)
2290{
2291 struct rb_node **p = &root->rb_node;
2292 struct rb_node *parent = NULL;
2293 struct sa_defrag_extent_backref *entry;
2294 int ret;
2295
2296 while (*p) {
2297 parent = *p;
2298 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2299
2300 ret = backref_comp(backref, entry);
2301 if (ret < 0)
2302 p = &(*p)->rb_left;
2303 else
2304 p = &(*p)->rb_right;
2305 }
2306
2307 rb_link_node(&backref->node, parent, p);
2308 rb_insert_color(&backref->node, root);
2309}
2310
2311/*
2312 * Note the backref might has changed, and in this case we just return 0.
2313 */
2314static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2315 void *ctx)
2316{
2317 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002318 struct old_sa_defrag_extent *old = ctx;
2319 struct new_sa_defrag_extent *new = old->new;
2320 struct btrfs_path *path = new->path;
2321 struct btrfs_key key;
2322 struct btrfs_root *root;
2323 struct sa_defrag_extent_backref *backref;
2324 struct extent_buffer *leaf;
2325 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002327 int slot;
2328 int ret;
2329 u64 extent_offset;
2330 u64 num_bytes;
2331
2332 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002333 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002334 return 0;
2335
2336 key.objectid = root_id;
2337 key.type = BTRFS_ROOT_ITEM_KEY;
2338 key.offset = (u64)-1;
2339
Liu Bo38c227d2013-01-29 03:18:40 +00002340 root = btrfs_read_fs_root_no_name(fs_info, &key);
2341 if (IS_ERR(root)) {
2342 if (PTR_ERR(root) == -ENOENT)
2343 return 0;
2344 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002345 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002346 inum, offset, root_id);
2347 return PTR_ERR(root);
2348 }
2349
2350 key.objectid = inum;
2351 key.type = BTRFS_EXTENT_DATA_KEY;
2352 if (offset > (u64)-1 << 32)
2353 key.offset = 0;
2354 else
2355 key.offset = offset;
2356
2357 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302358 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002359 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002360 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002361
2362 while (1) {
2363 cond_resched();
2364
2365 leaf = path->nodes[0];
2366 slot = path->slots[0];
2367
2368 if (slot >= btrfs_header_nritems(leaf)) {
2369 ret = btrfs_next_leaf(root, path);
2370 if (ret < 0) {
2371 goto out;
2372 } else if (ret > 0) {
2373 ret = 0;
2374 goto out;
2375 }
2376 continue;
2377 }
2378
2379 path->slots[0]++;
2380
2381 btrfs_item_key_to_cpu(leaf, &key, slot);
2382
2383 if (key.objectid > inum)
2384 goto out;
2385
2386 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2387 continue;
2388
2389 extent = btrfs_item_ptr(leaf, slot,
2390 struct btrfs_file_extent_item);
2391
2392 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2393 continue;
2394
Liu Boe68afa42013-07-01 22:13:26 +08002395 /*
2396 * 'offset' refers to the exact key.offset,
2397 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2398 * (key.offset - extent_offset).
2399 */
2400 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002401 continue;
2402
Liu Boe68afa42013-07-01 22:13:26 +08002403 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002404 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002405
Liu Bo38c227d2013-01-29 03:18:40 +00002406 if (extent_offset >= old->extent_offset + old->offset +
2407 old->len || extent_offset + num_bytes <=
2408 old->extent_offset + old->offset)
2409 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002410 break;
2411 }
2412
2413 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2414 if (!backref) {
2415 ret = -ENOENT;
2416 goto out;
2417 }
2418
2419 backref->root_id = root_id;
2420 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002421 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002422 backref->num_bytes = num_bytes;
2423 backref->extent_offset = extent_offset;
2424 backref->generation = btrfs_file_extent_generation(leaf, extent);
2425 backref->old = old;
2426 backref_insert(&new->root, backref);
2427 old->count++;
2428out:
2429 btrfs_release_path(path);
2430 WARN_ON(ret);
2431 return ret;
2432}
2433
2434static noinline bool record_extent_backrefs(struct btrfs_path *path,
2435 struct new_sa_defrag_extent *new)
2436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002438 struct old_sa_defrag_extent *old, *tmp;
2439 int ret;
2440
2441 new->path = path;
2442
2443 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002444 ret = iterate_inodes_from_logical(old->bytenr +
2445 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002446 path, record_one_backref,
2447 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002448 if (ret < 0 && ret != -ENOENT)
2449 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002450
2451 /* no backref to be processed for this extent */
2452 if (!old->count) {
2453 list_del(&old->list);
2454 kfree(old);
2455 }
2456 }
2457
2458 if (list_empty(&new->head))
2459 return false;
2460
2461 return true;
2462}
2463
2464static int relink_is_mergable(struct extent_buffer *leaf,
2465 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002466 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002467{
Liu Bo116e0022013-08-02 16:30:40 +08002468 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002469 return 0;
2470
2471 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2472 return 0;
2473
Liu Bo116e0022013-08-02 16:30:40 +08002474 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2475 return 0;
2476
2477 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002478 btrfs_file_extent_other_encoding(leaf, fi))
2479 return 0;
2480
2481 return 1;
2482}
2483
2484/*
2485 * Note the backref might has changed, and in this case we just return 0.
2486 */
2487static noinline int relink_extent_backref(struct btrfs_path *path,
2488 struct sa_defrag_extent_backref *prev,
2489 struct sa_defrag_extent_backref *backref)
2490{
2491 struct btrfs_file_extent_item *extent;
2492 struct btrfs_file_extent_item *item;
2493 struct btrfs_ordered_extent *ordered;
2494 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002495 struct btrfs_root *root;
2496 struct btrfs_key key;
2497 struct extent_buffer *leaf;
2498 struct old_sa_defrag_extent *old = backref->old;
2499 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002500 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002501 struct inode *inode;
2502 struct extent_state *cached = NULL;
2503 int ret = 0;
2504 u64 start;
2505 u64 len;
2506 u64 lock_start;
2507 u64 lock_end;
2508 bool merge = false;
2509 int index;
2510
2511 if (prev && prev->root_id == backref->root_id &&
2512 prev->inum == backref->inum &&
2513 prev->file_pos + prev->num_bytes == backref->file_pos)
2514 merge = true;
2515
2516 /* step 1: get root */
2517 key.objectid = backref->root_id;
2518 key.type = BTRFS_ROOT_ITEM_KEY;
2519 key.offset = (u64)-1;
2520
Liu Bo38c227d2013-01-29 03:18:40 +00002521 index = srcu_read_lock(&fs_info->subvol_srcu);
2522
2523 root = btrfs_read_fs_root_no_name(fs_info, &key);
2524 if (IS_ERR(root)) {
2525 srcu_read_unlock(&fs_info->subvol_srcu, index);
2526 if (PTR_ERR(root) == -ENOENT)
2527 return 0;
2528 return PTR_ERR(root);
2529 }
Liu Bo38c227d2013-01-29 03:18:40 +00002530
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002531 if (btrfs_root_readonly(root)) {
2532 srcu_read_unlock(&fs_info->subvol_srcu, index);
2533 return 0;
2534 }
2535
Liu Bo38c227d2013-01-29 03:18:40 +00002536 /* step 2: get inode */
2537 key.objectid = backref->inum;
2538 key.type = BTRFS_INODE_ITEM_KEY;
2539 key.offset = 0;
2540
2541 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2542 if (IS_ERR(inode)) {
2543 srcu_read_unlock(&fs_info->subvol_srcu, index);
2544 return 0;
2545 }
2546
2547 srcu_read_unlock(&fs_info->subvol_srcu, index);
2548
2549 /* step 3: relink backref */
2550 lock_start = backref->file_pos;
2551 lock_end = backref->file_pos + backref->num_bytes - 1;
2552 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002553 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002554
2555 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2556 if (ordered) {
2557 btrfs_put_ordered_extent(ordered);
2558 goto out_unlock;
2559 }
2560
2561 trans = btrfs_join_transaction(root);
2562 if (IS_ERR(trans)) {
2563 ret = PTR_ERR(trans);
2564 goto out_unlock;
2565 }
2566
2567 key.objectid = backref->inum;
2568 key.type = BTRFS_EXTENT_DATA_KEY;
2569 key.offset = backref->file_pos;
2570
2571 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2572 if (ret < 0) {
2573 goto out_free_path;
2574 } else if (ret > 0) {
2575 ret = 0;
2576 goto out_free_path;
2577 }
2578
2579 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2580 struct btrfs_file_extent_item);
2581
2582 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2583 backref->generation)
2584 goto out_free_path;
2585
2586 btrfs_release_path(path);
2587
2588 start = backref->file_pos;
2589 if (backref->extent_offset < old->extent_offset + old->offset)
2590 start += old->extent_offset + old->offset -
2591 backref->extent_offset;
2592
2593 len = min(backref->extent_offset + backref->num_bytes,
2594 old->extent_offset + old->offset + old->len);
2595 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2596
2597 ret = btrfs_drop_extents(trans, root, inode, start,
2598 start + len, 1);
2599 if (ret)
2600 goto out_free_path;
2601again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002602 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002603 key.type = BTRFS_EXTENT_DATA_KEY;
2604 key.offset = start;
2605
Liu Boa09a0a72013-03-11 09:20:58 +00002606 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002607 if (merge) {
2608 struct btrfs_file_extent_item *fi;
2609 u64 extent_len;
2610 struct btrfs_key found_key;
2611
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002612 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002613 if (ret < 0)
2614 goto out_free_path;
2615
2616 path->slots[0]--;
2617 leaf = path->nodes[0];
2618 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2619
2620 fi = btrfs_item_ptr(leaf, path->slots[0],
2621 struct btrfs_file_extent_item);
2622 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2623
Liu Bo116e0022013-08-02 16:30:40 +08002624 if (extent_len + found_key.offset == start &&
2625 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002626 btrfs_set_file_extent_num_bytes(leaf, fi,
2627 extent_len + len);
2628 btrfs_mark_buffer_dirty(leaf);
2629 inode_add_bytes(inode, len);
2630
2631 ret = 1;
2632 goto out_free_path;
2633 } else {
2634 merge = false;
2635 btrfs_release_path(path);
2636 goto again;
2637 }
2638 }
2639
2640 ret = btrfs_insert_empty_item(trans, root, path, &key,
2641 sizeof(*extent));
2642 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002643 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002644 goto out_free_path;
2645 }
2646
2647 leaf = path->nodes[0];
2648 item = btrfs_item_ptr(leaf, path->slots[0],
2649 struct btrfs_file_extent_item);
2650 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2651 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2652 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2653 btrfs_set_file_extent_num_bytes(leaf, item, len);
2654 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2655 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2656 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2657 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2658 btrfs_set_file_extent_encryption(leaf, item, 0);
2659 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2660
2661 btrfs_mark_buffer_dirty(leaf);
2662 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002663 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002664
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002665 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002666 new->disk_len, 0,
2667 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002668 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002669 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002670 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002671 goto out_free_path;
2672 }
2673
2674 ret = 1;
2675out_free_path:
2676 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002677 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002678 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002679out_unlock:
2680 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2681 &cached, GFP_NOFS);
2682 iput(inode);
2683 return ret;
2684}
2685
Liu Bo6f519562013-10-29 10:45:05 +08002686static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2687{
2688 struct old_sa_defrag_extent *old, *tmp;
2689
2690 if (!new)
2691 return;
2692
2693 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002694 kfree(old);
2695 }
2696 kfree(new);
2697}
2698
Liu Bo38c227d2013-01-29 03:18:40 +00002699static void relink_file_extents(struct new_sa_defrag_extent *new)
2700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002701 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002702 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002703 struct sa_defrag_extent_backref *backref;
2704 struct sa_defrag_extent_backref *prev = NULL;
2705 struct inode *inode;
2706 struct btrfs_root *root;
2707 struct rb_node *node;
2708 int ret;
2709
2710 inode = new->inode;
2711 root = BTRFS_I(inode)->root;
2712
2713 path = btrfs_alloc_path();
2714 if (!path)
2715 return;
2716
2717 if (!record_extent_backrefs(path, new)) {
2718 btrfs_free_path(path);
2719 goto out;
2720 }
2721 btrfs_release_path(path);
2722
2723 while (1) {
2724 node = rb_first(&new->root);
2725 if (!node)
2726 break;
2727 rb_erase(node, &new->root);
2728
2729 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2730
2731 ret = relink_extent_backref(path, prev, backref);
2732 WARN_ON(ret < 0);
2733
2734 kfree(prev);
2735
2736 if (ret == 1)
2737 prev = backref;
2738 else
2739 prev = NULL;
2740 cond_resched();
2741 }
2742 kfree(prev);
2743
2744 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002745out:
Liu Bo6f519562013-10-29 10:45:05 +08002746 free_sa_defrag_extent(new);
2747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002748 atomic_dec(&fs_info->defrag_running);
2749 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002750}
2751
2752static struct new_sa_defrag_extent *
2753record_old_file_extents(struct inode *inode,
2754 struct btrfs_ordered_extent *ordered)
2755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002757 struct btrfs_root *root = BTRFS_I(inode)->root;
2758 struct btrfs_path *path;
2759 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002760 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002761 struct new_sa_defrag_extent *new;
2762 int ret;
2763
2764 new = kmalloc(sizeof(*new), GFP_NOFS);
2765 if (!new)
2766 return NULL;
2767
2768 new->inode = inode;
2769 new->file_pos = ordered->file_offset;
2770 new->len = ordered->len;
2771 new->bytenr = ordered->start;
2772 new->disk_len = ordered->disk_len;
2773 new->compress_type = ordered->compress_type;
2774 new->root = RB_ROOT;
2775 INIT_LIST_HEAD(&new->head);
2776
2777 path = btrfs_alloc_path();
2778 if (!path)
2779 goto out_kfree;
2780
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002781 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002782 key.type = BTRFS_EXTENT_DATA_KEY;
2783 key.offset = new->file_pos;
2784
2785 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2786 if (ret < 0)
2787 goto out_free_path;
2788 if (ret > 0 && path->slots[0] > 0)
2789 path->slots[0]--;
2790
2791 /* find out all the old extents for the file range */
2792 while (1) {
2793 struct btrfs_file_extent_item *extent;
2794 struct extent_buffer *l;
2795 int slot;
2796 u64 num_bytes;
2797 u64 offset;
2798 u64 end;
2799 u64 disk_bytenr;
2800 u64 extent_offset;
2801
2802 l = path->nodes[0];
2803 slot = path->slots[0];
2804
2805 if (slot >= btrfs_header_nritems(l)) {
2806 ret = btrfs_next_leaf(root, path);
2807 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002808 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002809 else if (ret > 0)
2810 break;
2811 continue;
2812 }
2813
2814 btrfs_item_key_to_cpu(l, &key, slot);
2815
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002816 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002817 break;
2818 if (key.type != BTRFS_EXTENT_DATA_KEY)
2819 break;
2820 if (key.offset >= new->file_pos + new->len)
2821 break;
2822
2823 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2824
2825 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2826 if (key.offset + num_bytes < new->file_pos)
2827 goto next;
2828
2829 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2830 if (!disk_bytenr)
2831 goto next;
2832
2833 extent_offset = btrfs_file_extent_offset(l, extent);
2834
2835 old = kmalloc(sizeof(*old), GFP_NOFS);
2836 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002837 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002838
2839 offset = max(new->file_pos, key.offset);
2840 end = min(new->file_pos + new->len, key.offset + num_bytes);
2841
2842 old->bytenr = disk_bytenr;
2843 old->extent_offset = extent_offset;
2844 old->offset = offset - key.offset;
2845 old->len = end - offset;
2846 old->new = new;
2847 old->count = 0;
2848 list_add_tail(&old->list, &new->head);
2849next:
2850 path->slots[0]++;
2851 cond_resched();
2852 }
2853
2854 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002855 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002856
2857 return new;
2858
Liu Bo38c227d2013-01-29 03:18:40 +00002859out_free_path:
2860 btrfs_free_path(path);
2861out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002862 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002863 return NULL;
2864}
2865
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002866static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002867 u64 start, u64 len)
2868{
2869 struct btrfs_block_group_cache *cache;
2870
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002871 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002872 ASSERT(cache);
2873
2874 spin_lock(&cache->lock);
2875 cache->delalloc_bytes -= len;
2876 spin_unlock(&cache->lock);
2877
2878 btrfs_put_block_group(cache);
2879}
2880
Chris Masond352ac62008-09-29 15:18:18 -04002881/* as ordered data IO finishes, this gets called so we can finish
2882 * an ordered extent if the range of bytes in the file it covers are
2883 * fully written.
2884 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002885static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002886{
Josef Bacik5fd02042012-05-02 14:00:54 -04002887 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002889 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002890 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002892 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002893 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002894 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002895 int ret = 0;
2896 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002897 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002898 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002899 bool range_locked = false;
2900 bool clear_new_delalloc_bytes = false;
2901
2902 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2903 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2904 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2905 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002907 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002908
Josef Bacik5fd02042012-05-02 14:00:54 -04002909 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2910 ret = -EIO;
2911 goto out;
2912 }
2913
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002914 btrfs_free_io_failure_record(BTRFS_I(inode),
2915 ordered_extent->file_offset,
2916 ordered_extent->file_offset +
2917 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002918
Josef Bacik77cef2e2013-08-29 13:57:21 -04002919 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2920 truncated = true;
2921 logical_len = ordered_extent->truncated_len;
2922 /* Truncated the entire extent, don't bother adding */
2923 if (!logical_len)
2924 goto out;
2925 }
2926
Yan, Zhengc2167752009-11-12 09:34:21 +00002927 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002929
2930 /*
2931 * For mwrite(mmap + memset to write) case, we still reserve
2932 * space for NOCOW range.
2933 * As NOCOW won't cause a new delayed ref, just free the space
2934 */
2935 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2936 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002937 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2938 if (nolock)
2939 trans = btrfs_join_transaction_nolock(root);
2940 else
2941 trans = btrfs_join_transaction(root);
2942 if (IS_ERR(trans)) {
2943 ret = PTR_ERR(trans);
2944 trans = NULL;
2945 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002946 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002947 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002948 ret = btrfs_update_inode_fallback(trans, root, inode);
2949 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002950 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002951 goto out;
2952 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002954 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002955 lock_extent_bits(io_tree, ordered_extent->file_offset,
2956 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002957 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958
Liu Bo38c227d2013-01-29 03:18:40 +00002959 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2960 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002961 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002962 if (ret) {
2963 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002964 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002965 /* the inode is shared */
2966 new = record_old_file_extents(inode, ordered_extent);
2967
2968 clear_extent_bit(io_tree, ordered_extent->file_offset,
2969 ordered_extent->file_offset + ordered_extent->len - 1,
2970 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2971 }
2972
Josef Bacik0cb59c92010-07-02 12:14:14 -04002973 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002974 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002976 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 if (IS_ERR(trans)) {
2978 ret = PTR_ERR(trans);
2979 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002980 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002981 }
Chris Masona79b7d42014-05-22 16:18:52 -07002982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002983 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002984
Chris Masonc8b97812008-10-29 14:49:59 -04002985 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002986 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002987 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002988 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002989 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002990 ordered_extent->file_offset,
2991 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002992 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002993 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002994 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002995 ret = insert_reserved_file_extent(trans, inode,
2996 ordered_extent->file_offset,
2997 ordered_extent->start,
2998 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002999 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003000 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003001 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003002 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003003 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003004 ordered_extent->start,
3005 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003006 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003007 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3008 ordered_extent->file_offset, ordered_extent->len,
3009 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003010 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003011 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003012 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003014
David Sterbadf9f6282017-02-10 19:35:37 +01003015 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016
Josef Bacik6c760c02012-11-09 10:53:21 -05003017 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3018 ret = btrfs_update_inode_fallback(trans, root, inode);
3019 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003020 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003021 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003022 }
3023 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003024out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003025 if (range_locked || clear_new_delalloc_bytes) {
3026 unsigned int clear_bits = 0;
3027
3028 if (range_locked)
3029 clear_bits |= EXTENT_LOCKED;
3030 if (clear_new_delalloc_bytes)
3031 clear_bits |= EXTENT_DELALLOC_NEW;
3032 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3033 ordered_extent->file_offset,
3034 ordered_extent->file_offset +
3035 ordered_extent->len - 1,
3036 clear_bits,
3037 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3038 0, &cached_state, GFP_NOFS);
3039 }
3040
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003041 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003042 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3043 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003044 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003045 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003046
Josef Bacik77cef2e2013-08-29 13:57:21 -04003047 if (ret || truncated) {
3048 u64 start, end;
3049
3050 if (truncated)
3051 start = ordered_extent->file_offset + logical_len;
3052 else
3053 start = ordered_extent->file_offset;
3054 end = ordered_extent->file_offset + ordered_extent->len - 1;
3055 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3056
3057 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003058 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003059
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003060 /*
3061 * If the ordered extent had an IOERR or something else went
3062 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003063 * back to the allocator. We only free the extent in the
3064 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003065 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003066 if ((ret || !logical_len) &&
3067 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003068 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003069 btrfs_free_reserved_extent(fs_info,
3070 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003071 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003072 }
3073
3074
Josef Bacik5fd02042012-05-02 14:00:54 -04003075 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003076 * This needs to be done to make sure anybody waiting knows we are done
3077 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003078 */
3079 btrfs_remove_ordered_extent(inode, ordered_extent);
3080
Liu Bo38c227d2013-01-29 03:18:40 +00003081 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003082 if (new) {
3083 if (ret) {
3084 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003085 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003086 } else {
3087 relink_file_extents(new);
3088 }
3089 }
Liu Bo38c227d2013-01-29 03:18:40 +00003090
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091 /* once for us */
3092 btrfs_put_ordered_extent(ordered_extent);
3093 /* once for the tree */
3094 btrfs_put_ordered_extent(ordered_extent);
3095
Josef Bacik5fd02042012-05-02 14:00:54 -04003096 return ret;
3097}
3098
3099static void finish_ordered_fn(struct btrfs_work *work)
3100{
3101 struct btrfs_ordered_extent *ordered_extent;
3102 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3103 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104}
3105
David Sterbac3988d62017-02-17 15:18:32 +01003106static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003107 struct extent_state *state, int uptodate)
3108{
Josef Bacik5fd02042012-05-02 14:00:54 -04003109 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003111 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003112 struct btrfs_workqueue *wq;
3113 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003114
liubo1abe9b82011-03-24 11:18:59 +00003115 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3116
Chris Mason8b62b722009-09-02 16:53:46 -04003117 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003118 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3119 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003120 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003121
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003122 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003123 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003124 func = btrfs_freespace_write_helper;
3125 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003126 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003127 func = btrfs_endio_write_helper;
3128 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
Liu Bo9e0af232014-08-15 23:36:53 +08003130 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3131 NULL);
3132 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003133}
3134
Miao Xiedc380ae2014-09-12 18:43:55 +08003135static int __readpage_endio_check(struct inode *inode,
3136 struct btrfs_io_bio *io_bio,
3137 int icsum, struct page *page,
3138 int pgoff, u64 start, size_t len)
3139{
3140 char *kaddr;
3141 u32 csum_expected;
3142 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003143
3144 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3145
3146 kaddr = kmap_atomic(page);
3147 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003148 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003149 if (csum != csum_expected)
3150 goto zeroit;
3151
3152 kunmap_atomic(kaddr);
3153 return 0;
3154zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003155 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003156 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003157 memset(kaddr + pgoff, 1, len);
3158 flush_dcache_page(page);
3159 kunmap_atomic(kaddr);
3160 if (csum_expected == 0)
3161 return 0;
3162 return -EIO;
3163}
3164
Chris Masond352ac62008-09-29 15:18:18 -04003165/*
Chris Masond352ac62008-09-29 15:18:18 -04003166 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003167 * if there's a match, we allow the bio to finish. If not, the code in
3168 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003169 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003170static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3171 u64 phy_offset, struct page *page,
3172 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003173{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003174 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003175 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003176 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003177 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003178
Chris Masond20f7042008-12-08 16:58:54 -05003179 if (PageChecked(page)) {
3180 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003181 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003182 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003183
3184 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003185 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003186
Yan Zheng17d217f2008-12-12 10:03:38 -05003187 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003188 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003189 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003190 return 0;
3191 }
3192
Miao Xiefacc8a222013-07-25 19:22:34 +08003193 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003194 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3195 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003196}
Chris Masonb888db2b2007-08-27 16:49:44 -04003197
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003198void btrfs_add_delayed_iput(struct inode *inode)
3199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003200 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003201 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003202
3203 if (atomic_add_unless(&inode->i_count, -1, 1))
3204 return;
3205
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003206 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003207 if (binode->delayed_iput_count == 0) {
3208 ASSERT(list_empty(&binode->delayed_iput));
3209 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3210 } else {
3211 binode->delayed_iput_count++;
3212 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003213 spin_unlock(&fs_info->delayed_iput_lock);
3214}
3215
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003216void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003217{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003220 while (!list_empty(&fs_info->delayed_iputs)) {
3221 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003222
David Sterba8089fe62015-11-19 14:15:51 +01003223 inode = list_first_entry(&fs_info->delayed_iputs,
3224 struct btrfs_inode, delayed_iput);
3225 if (inode->delayed_iput_count) {
3226 inode->delayed_iput_count--;
3227 list_move_tail(&inode->delayed_iput,
3228 &fs_info->delayed_iputs);
3229 } else {
3230 list_del_init(&inode->delayed_iput);
3231 }
3232 spin_unlock(&fs_info->delayed_iput_lock);
3233 iput(&inode->vfs_inode);
3234 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235 }
David Sterba8089fe62015-11-19 14:15:51 +01003236 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003237}
3238
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003240 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003241 * files in the subvolume, it removes orphan item and frees block_rsv
3242 * structure.
3243 */
3244void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3245 struct btrfs_root *root)
3246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003247 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003248 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 int ret;
3250
Josef Bacik8a35d952012-05-23 14:26:42 -04003251 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3253 return;
3254
Josef Bacik90290e12011-12-02 15:44:12 -05003255 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003256 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003257 spin_unlock(&root->orphan_lock);
3258 return;
3259 }
3260
3261 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3262 spin_unlock(&root->orphan_lock);
3263 return;
3264 }
3265
3266 block_rsv = root->orphan_block_rsv;
3267 root->orphan_block_rsv = NULL;
3268 spin_unlock(&root->orphan_lock);
3269
Miao Xie27cdeb72014-04-02 19:51:05 +08003270 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003272 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003274 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003275 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003276 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003277 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3278 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 }
3280
Josef Bacik90290e12011-12-02 15:44:12 -05003281 if (block_rsv) {
3282 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003283 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 }
3285}
3286
3287/*
Josef Bacik7b128762008-07-24 12:17:14 -04003288 * This creates an orphan entry for the given inode in case something goes
3289 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 *
3291 * NOTE: caller of this function should reserve 5 units of metadata for
3292 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003293 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003294int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3295 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003296{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003297 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3298 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 struct btrfs_block_rsv *block_rsv = NULL;
3300 int reserve = 0;
3301 int insert = 0;
3302 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003303
Yan, Zhengd68fc572010-05-16 10:49:58 -04003304 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003305 block_rsv = btrfs_alloc_block_rsv(fs_info,
3306 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003307 if (!block_rsv)
3308 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 }
3310
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 spin_lock(&root->orphan_lock);
3312 if (!root->orphan_block_rsv) {
3313 root->orphan_block_rsv = block_rsv;
3314 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003315 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 block_rsv = NULL;
3317 }
Josef Bacik7b128762008-07-24 12:17:14 -04003318
Josef Bacik8a35d952012-05-23 14:26:42 -04003319 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003320 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321#if 0
3322 /*
3323 * For proper ENOSPC handling, we should do orphan
3324 * cleanup when mounting. But this introduces backward
3325 * compatibility issue.
3326 */
3327 if (!xchg(&root->orphan_item_inserted, 1))
3328 insert = 2;
3329 else
3330 insert = 1;
3331#endif
3332 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003333 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 }
Josef Bacik7b128762008-07-24 12:17:14 -04003335
Josef Bacik72ac3c02012-05-23 14:13:11 -04003336 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003337 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003340
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 /* grab metadata reservation from transaction handle */
3342 if (reserve) {
3343 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003344 ASSERT(!ret);
3345 if (ret) {
3346 atomic_dec(&root->orphan_inodes);
3347 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003348 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003349 if (insert)
3350 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003351 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003352 return ret;
3353 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 }
3355
3356 /* insert an orphan item to track this unlinked/truncated file */
3357 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003358 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003359 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003360 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003361 if (reserve) {
3362 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003363 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003364 btrfs_orphan_release_metadata(inode);
3365 }
3366 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003367 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003368 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003369 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003370 return ret;
3371 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003372 }
3373 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003374 }
3375
3376 /* insert an orphan item to track subvolume contains orphan files */
3377 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003380 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003381 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003382 return ret;
3383 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003384 }
3385 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003386}
3387
3388/*
3389 * We have done the truncate/delete so we can go ahead and remove the orphan
3390 * item for this particular inode.
3391 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003392static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003393 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003394{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003395 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 int delete_item = 0;
3397 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003398 int ret = 0;
3399
Yan, Zhengd68fc572010-05-16 10:49:58 -04003400 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003401 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003402 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003404
Josef Bacik72ac3c02012-05-23 14:13:11 -04003405 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003406 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003407 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 spin_unlock(&root->orphan_lock);
3409
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003410 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003411 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003412 if (trans)
3413 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003414 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003415 }
Josef Bacik7b128762008-07-24 12:17:14 -04003416
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003417 if (release_rsv)
3418 btrfs_orphan_release_metadata(inode);
3419
Josef Bacik4ef31a42013-08-13 14:10:08 -04003420 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003421}
3422
3423/*
3424 * this cleans up any orphans that may be left on the list from the last use
3425 * of this root.
3426 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003427int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003429 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003430 struct btrfs_path *path;
3431 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003432 struct btrfs_key key, found_key;
3433 struct btrfs_trans_handle *trans;
3434 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003435 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003436 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3437
Yan, Zhengd68fc572010-05-16 10:49:58 -04003438 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003439 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003440
3441 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003442 if (!path) {
3443 ret = -ENOMEM;
3444 goto out;
3445 }
David Sterbae4058b52015-11-27 16:31:35 +01003446 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003447
3448 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003449 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003450 key.offset = (u64)-1;
3451
Josef Bacik7b128762008-07-24 12:17:14 -04003452 while (1) {
3453 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003454 if (ret < 0)
3455 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003456
3457 /*
3458 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003459 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003460 * find the key and see if we have stuff that matches
3461 */
3462 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003464 if (path->slots[0] == 0)
3465 break;
3466 path->slots[0]--;
3467 }
3468
3469 /* pull out the item */
3470 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003471 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3472
3473 /* make sure the item matches what we want */
3474 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3475 break;
David Sterba962a2982014-06-04 18:41:45 +02003476 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003477 break;
3478
3479 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003480 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003481
3482 /*
3483 * this is where we are basically btrfs_lookup, without the
3484 * crossing root thing. we store the inode number in the
3485 * offset of the orphan item.
3486 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003487
3488 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003489 btrfs_err(fs_info,
3490 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003491 ret = -EINVAL;
3492 goto out;
3493 }
3494
3495 last_objectid = found_key.offset;
3496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003497 found_key.objectid = found_key.offset;
3498 found_key.type = BTRFS_INODE_ITEM_KEY;
3499 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303501 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003502 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003503 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003505 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003506 struct btrfs_root *dead_root;
3507 struct btrfs_fs_info *fs_info = root->fs_info;
3508 int is_dead_root = 0;
3509
3510 /*
3511 * this is an orphan in the tree root. Currently these
3512 * could come from 2 sources:
3513 * a) a snapshot deletion in progress
3514 * b) a free space cache inode
3515 * We need to distinguish those two, as the snapshot
3516 * orphan must not get deleted.
3517 * find_dead_roots already ran before us, so if this
3518 * is a snapshot deletion, we should find the root
3519 * in the dead_roots list
3520 */
3521 spin_lock(&fs_info->trans_lock);
3522 list_for_each_entry(dead_root, &fs_info->dead_roots,
3523 root_list) {
3524 if (dead_root->root_key.objectid ==
3525 found_key.objectid) {
3526 is_dead_root = 1;
3527 break;
3528 }
3529 }
3530 spin_unlock(&fs_info->trans_lock);
3531 if (is_dead_root) {
3532 /* prevent this orphan from being found again */
3533 key.offset = found_key.objectid - 1;
3534 continue;
3535 }
3536 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003537 /*
3538 * Inode is already gone but the orphan item is still there,
3539 * kill the orphan item.
3540 */
Filipe Manana67710892016-06-06 11:51:25 +01003541 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003542 trans = btrfs_start_transaction(root, 1);
3543 if (IS_ERR(trans)) {
3544 ret = PTR_ERR(trans);
3545 goto out;
3546 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 btrfs_debug(fs_info, "auto deleting %Lu",
3548 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003549 ret = btrfs_del_orphan_item(trans, root,
3550 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003551 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003552 if (ret)
3553 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003554 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003555 }
Josef Bacik7b128762008-07-24 12:17:14 -04003556
Josef Bacik7b128762008-07-24 12:17:14 -04003557 /*
3558 * add this inode to the orphan list so btrfs_orphan_del does
3559 * the proper thing when we hit it
3560 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003561 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3562 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003563 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003564
Josef Bacik7b128762008-07-24 12:17:14 -04003565 /* if we have links, this was a truncate, lets do that */
3566 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303567 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003568 iput(inode);
3569 continue;
3570 }
Josef Bacik7b128762008-07-24 12:17:14 -04003571 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003572
3573 /* 1 for the orphan item deletion. */
3574 trans = btrfs_start_transaction(root, 1);
3575 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003576 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003577 ret = PTR_ERR(trans);
3578 goto out;
3579 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003580 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003581 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003582 if (ret) {
3583 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003584 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003585 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003586
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003587 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003588 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003589 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003590 } else {
3591 nr_unlink++;
3592 }
3593
3594 /* this will do delete_inode and everything for us */
3595 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003596 if (ret)
3597 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003598 }
Miao Xie3254c872011-11-10 20:45:05 -05003599 /* release the path since we're done with it */
3600 btrfs_release_path(path);
3601
Yan, Zhengd68fc572010-05-16 10:49:58 -04003602 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3603
3604 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003605 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003606 (u64)-1);
3607
Miao Xie27cdeb72014-04-02 19:51:05 +08003608 if (root->orphan_block_rsv ||
3609 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003610 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003611 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003612 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003613 }
Josef Bacik7b128762008-07-24 12:17:14 -04003614
3615 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003616 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003617 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003619
3620out:
3621 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003622 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003623 btrfs_free_path(path);
3624 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003625}
3626
Chris Masond352ac62008-09-29 15:18:18 -04003627/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003628 * very simple check to peek ahead in the leaf looking for xattrs. If we
3629 * don't find any xattrs, we know there can't be any acls.
3630 *
3631 * slot is the slot the inode is in, objectid is the objectid of the inode
3632 */
3633static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003634 int slot, u64 objectid,
3635 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003636{
3637 u32 nritems = btrfs_header_nritems(leaf);
3638 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003639 static u64 xattr_access = 0;
3640 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003641 int scanned = 0;
3642
Josef Bacikf23b5a52013-06-19 10:16:26 -04003643 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003644 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3645 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3646 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3647 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003648 }
3649
Chris Mason46a53cc2009-04-27 11:47:50 -04003650 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003651 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003652 while (slot < nritems) {
3653 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3654
3655 /* we found a different objectid, there must not be acls */
3656 if (found_key.objectid != objectid)
3657 return 0;
3658
3659 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003660 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003661 if (*first_xattr_slot == -1)
3662 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003663 if (found_key.offset == xattr_access ||
3664 found_key.offset == xattr_default)
3665 return 1;
3666 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003667
3668 /*
3669 * we found a key greater than an xattr key, there can't
3670 * be any acls later on
3671 */
3672 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3673 return 0;
3674
3675 slot++;
3676 scanned++;
3677
3678 /*
3679 * it goes inode, inode backrefs, xattrs, extents,
3680 * so if there are a ton of hard links to an inode there can
3681 * be a lot of backrefs. Don't waste time searching too hard,
3682 * this is just an optimization
3683 */
3684 if (scanned >= 8)
3685 break;
3686 }
3687 /* we hit the end of the leaf before we found an xattr or
3688 * something larger than an xattr. We have to assume the inode
3689 * has acls
3690 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003691 if (*first_xattr_slot == -1)
3692 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003693 return 1;
3694}
3695
3696/*
Chris Masond352ac62008-09-29 15:18:18 -04003697 * read an inode from the btree into the in-memory inode
3698 */
Filipe Manana67710892016-06-06 11:51:25 +01003699static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003702 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003703 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 struct btrfs_inode_item *inode_item;
3705 struct btrfs_root *root = BTRFS_I(inode)->root;
3706 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003707 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003708 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003709 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003710 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003711 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003712 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003713
3714 ret = btrfs_fill_inode(inode, &rdev);
3715 if (!ret)
3716 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003717
3718 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003719 if (!path) {
3720 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003721 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003722 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003723
Chris Mason39279cc2007-06-12 06:35:45 -04003724 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003725
Chris Mason39279cc2007-06-12 06:35:45 -04003726 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003727 if (ret) {
3728 if (ret > 0)
3729 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003730 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003731 }
Chris Mason39279cc2007-06-12 06:35:45 -04003732
Chris Mason5f39d392007-10-15 16:14:19 -04003733 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003734
3735 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003736 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003737
Chris Mason5f39d392007-10-15 16:14:19 -04003738 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3739 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003740 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003741 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003742 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3743 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003744 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003745
David Sterbaa937b972014-12-12 17:39:12 +01003746 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3747 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
David Sterbaa937b972014-12-12 17:39:12 +01003749 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3750 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
David Sterbaa937b972014-12-12 17:39:12 +01003752 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3753 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
chandan r9cc97d62012-07-04 12:48:07 +05303755 BTRFS_I(inode)->i_otime.tv_sec =
3756 btrfs_timespec_sec(leaf, &inode_item->otime);
3757 BTRFS_I(inode)->i_otime.tv_nsec =
3758 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003760 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003761 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003762 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3763
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003764 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003765 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003766 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003767 rdev = btrfs_inode_rdev(leaf, inode_item);
3768
Josef Bacikaec74772008-07-24 12:12:38 -04003769 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003770 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003771
3772cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003773 /*
3774 * If we were modified in the current generation and evicted from memory
3775 * and then re-read we need to do a full sync since we don't have any
3776 * idea about which extents were modified before we were evicted from
3777 * cache.
3778 *
3779 * This is required for both inode re-read from disk and delayed inode
3780 * in delayed_nodes_tree.
3781 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003782 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003783 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3784 &BTRFS_I(inode)->runtime_flags);
3785
Filipe Mananabde6c242015-07-24 00:00:19 +01003786 /*
3787 * We don't persist the id of the transaction where an unlink operation
3788 * against the inode was last made. So here we assume the inode might
3789 * have been evicted, and therefore the exact value of last_unlink_trans
3790 * lost, and set it to last_trans to avoid metadata inconsistencies
3791 * between the inode and its parent if the inode is fsync'ed and the log
3792 * replayed. For example, in the scenario:
3793 *
3794 * touch mydir/foo
3795 * ln mydir/foo mydir/bar
3796 * sync
3797 * unlink mydir/bar
3798 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3799 * xfs_io -c fsync mydir/foo
3800 * <power failure>
3801 * mount fs, triggers fsync log replay
3802 *
3803 * We must make sure that when we fsync our inode foo we also log its
3804 * parent inode, otherwise after log replay the parent still has the
3805 * dentry with the "bar" name but our inode foo has a link count of 1
3806 * and doesn't have an inode ref with the name "bar" anymore.
3807 *
3808 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003809 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003810 * transaction commits on fsync if our inode is a directory, or if our
3811 * inode is not a directory, logging its parent unnecessarily.
3812 */
3813 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3814
Miao Xie67de1172013-12-26 13:07:06 +08003815 path->slots[0]++;
3816 if (inode->i_nlink != 1 ||
3817 path->slots[0] >= btrfs_header_nritems(leaf))
3818 goto cache_acl;
3819
3820 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003821 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003822 goto cache_acl;
3823
3824 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3825 if (location.type == BTRFS_INODE_REF_KEY) {
3826 struct btrfs_inode_ref *ref;
3827
3828 ref = (struct btrfs_inode_ref *)ptr;
3829 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3830 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3831 struct btrfs_inode_extref *extref;
3832
3833 extref = (struct btrfs_inode_extref *)ptr;
3834 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3835 extref);
3836 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003837cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003838 /*
3839 * try to precache a NULL acl entry for files that don't have
3840 * any xattrs or acls
3841 */
Li Zefan33345d012011-04-20 10:31:50 +08003842 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003843 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003844 if (first_xattr_slot != -1) {
3845 path->slots[0] = first_xattr_slot;
3846 ret = btrfs_load_inode_props(inode, path);
3847 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003848 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003849 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003850 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003851 root->root_key.objectid, ret);
3852 }
3853 btrfs_free_path(path);
3854
Al Viro72c04902009-06-24 16:58:48 -04003855 if (!maybe_acls)
3856 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003857
Chris Mason39279cc2007-06-12 06:35:45 -04003858 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003859 case S_IFREG:
3860 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003861 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 inode->i_fop = &btrfs_file_operations;
3863 inode->i_op = &btrfs_file_inode_operations;
3864 break;
3865 case S_IFDIR:
3866 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003867 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003868 break;
3869 case S_IFLNK:
3870 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003871 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003872 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3873 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003874 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003875 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003876 init_special_inode(inode, inode->i_mode, rdev);
3877 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003878 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003879
3880 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003881 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003882
3883make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003884 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003885 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003886 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003887}
3888
Chris Masond352ac62008-09-29 15:18:18 -04003889/*
3890 * given a leaf and an inode, copy the inode fields into the leaf
3891 */
Chris Masone02119d2008-09-05 16:13:11 -04003892static void fill_inode_item(struct btrfs_trans_handle *trans,
3893 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003894 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003895 struct inode *inode)
3896{
Liu Bo51fab692012-12-27 09:01:21 +00003897 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003898
Liu Bo51fab692012-12-27 09:01:21 +00003899 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003900
Liu Bo51fab692012-12-27 09:01:21 +00003901 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3902 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3903 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3904 &token);
3905 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3906 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003907
David Sterbaa937b972014-12-12 17:39:12 +01003908 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003909 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003910 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003911 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003912
David Sterbaa937b972014-12-12 17:39:12 +01003913 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003914 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003915 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003916 inode->i_mtime.tv_nsec, &token);
3917
David Sterbaa937b972014-12-12 17:39:12 +01003918 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003919 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003920 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003921 inode->i_ctime.tv_nsec, &token);
3922
chandan r9cc97d62012-07-04 12:48:07 +05303923 btrfs_set_token_timespec_sec(leaf, &item->otime,
3924 BTRFS_I(inode)->i_otime.tv_sec, &token);
3925 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3926 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3927
Liu Bo51fab692012-12-27 09:01:21 +00003928 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3929 &token);
3930 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3931 &token);
3932 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3933 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3934 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3935 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3936 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003937}
3938
Chris Masond352ac62008-09-29 15:18:18 -04003939/*
3940 * copy everything in the in-memory inode into the btree.
3941 */
Chris Mason21151332011-11-10 20:39:08 -05003942static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003943 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003944{
3945 struct btrfs_inode_item *inode_item;
3946 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003947 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003948 int ret;
3949
3950 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003951 if (!path)
3952 return -ENOMEM;
3953
Chris Masonb9473432009-03-13 11:00:37 -04003954 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003955 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3956 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 if (ret) {
3958 if (ret > 0)
3959 ret = -ENOENT;
3960 goto failed;
3961 }
3962
Chris Mason5f39d392007-10-15 16:14:19 -04003963 leaf = path->nodes[0];
3964 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003965 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003966
Chris Masone02119d2008-09-05 16:13:11 -04003967 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003968 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003969 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 ret = 0;
3971failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003972 btrfs_free_path(path);
3973 return ret;
3974}
3975
Chris Masond352ac62008-09-29 15:18:18 -04003976/*
Chris Mason21151332011-11-10 20:39:08 -05003977 * copy everything in the in-memory inode into the btree.
3978 */
3979noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3980 struct btrfs_root *root, struct inode *inode)
3981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003982 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003983 int ret;
3984
3985 /*
3986 * If the inode is a free space inode, we can deadlock during commit
3987 * if we put it into the delayed code.
3988 *
3989 * The data relocation inode should also be directly updated
3990 * without delay
3991 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003992 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003993 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003994 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003995 btrfs_update_root_times(trans, root);
3996
Chris Mason21151332011-11-10 20:39:08 -05003997 ret = btrfs_delayed_update_inode(trans, root, inode);
3998 if (!ret)
3999 btrfs_set_inode_last_trans(trans, inode);
4000 return ret;
4001 }
4002
4003 return btrfs_update_inode_item(trans, root, inode);
4004}
4005
Josef Bacikbe6aef62012-10-22 15:43:12 -04004006noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4007 struct btrfs_root *root,
4008 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004009{
4010 int ret;
4011
4012 ret = btrfs_update_inode(trans, root, inode);
4013 if (ret == -ENOSPC)
4014 return btrfs_update_inode_item(trans, root, inode);
4015 return ret;
4016}
4017
4018/*
Chris Masond352ac62008-09-29 15:18:18 -04004019 * unlink helper that gets used here in inode.c and in the tree logging
4020 * recovery code. It remove a link in a directory with a given name, and
4021 * also drops the back refs in the inode to the directory
4022 */
Al Viro92986792011-03-04 17:14:37 +00004023static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004025 struct btrfs_inode *dir,
4026 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004027 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004029 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004031 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004032 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004034 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004035 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004036 u64 ino = btrfs_ino(inode);
4037 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004038
4039 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004040 if (!path) {
4041 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004042 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004043 }
4044
Chris Masonb9473432009-03-13 11:00:37 -04004045 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004046 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004047 name, name_len, -1);
4048 if (IS_ERR(di)) {
4049 ret = PTR_ERR(di);
4050 goto err;
4051 }
4052 if (!di) {
4053 ret = -ENOENT;
4054 goto err;
4055 }
Chris Mason5f39d392007-10-15 16:14:19 -04004056 leaf = path->nodes[0];
4057 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004058 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004059 if (ret)
4060 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004061 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004062
Miao Xie67de1172013-12-26 13:07:06 +08004063 /*
4064 * If we don't have dir index, we have to get it by looking up
4065 * the inode ref, since we get the inode ref, remove it directly,
4066 * it is unnecessary to do delayed deletion.
4067 *
4068 * But if we have dir index, needn't search inode ref to get it.
4069 * Since the inode ref is close to the inode item, it is better
4070 * that we delay to delete it, and just do this deletion when
4071 * we update the inode item.
4072 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004073 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004074 ret = btrfs_delayed_delete_inode_ref(inode);
4075 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004076 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004077 goto skip_backref;
4078 }
4079 }
4080
Li Zefan33345d012011-04-20 10:31:50 +08004081 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4082 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004083 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004084 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004085 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004086 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004087 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004088 goto err;
4089 }
Miao Xie67de1172013-12-26 13:07:06 +08004090skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004091 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004093 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 }
Chris Mason39279cc2007-06-12 06:35:45 -04004096
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004097 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4098 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004100 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 goto err;
4102 }
Chris Masone02119d2008-09-05 16:13:11 -04004103
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004104 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4105 index);
Chris Mason6418c962010-10-30 07:34:24 -04004106 if (ret == -ENOENT)
4107 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004108 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004109 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004110err:
4111 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004112 if (ret)
4113 goto out;
4114
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004115 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004116 inode_inc_iversion(&inode->vfs_inode);
4117 inode_inc_iversion(&dir->vfs_inode);
4118 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4119 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4120 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004121out:
Chris Mason39279cc2007-06-12 06:35:45 -04004122 return ret;
4123}
4124
Al Viro92986792011-03-04 17:14:37 +00004125int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4126 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004127 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004128 const char *name, int name_len)
4129{
4130 int ret;
4131 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4132 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 drop_nlink(&inode->vfs_inode);
4134 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004135 }
4136 return ret;
4137}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004138
4139/*
4140 * helper to start transaction for unlink and rmdir.
4141 *
Josef Bacikd52be812013-05-29 14:54:47 -04004142 * unlink and rmdir are special in btrfs, they do not always free space, so
4143 * if we cannot make our reservations the normal way try and see if there is
4144 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4145 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146 */
Josef Bacikd52be812013-05-29 14:54:47 -04004147static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004148{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150
Josef Bacike70bea52011-10-11 14:18:24 -04004151 /*
4152 * 1 for the possible orphan item
4153 * 1 for the dir item
4154 * 1 for the dir index
4155 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004156 * 1 for the inode
4157 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004158 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004159}
4160
Chris Mason39279cc2007-06-12 06:35:45 -04004161static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4162{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004163 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004165 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004166 int ret;
4167
Josef Bacikd52be812013-05-29 14:54:47 -04004168 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004170 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004171
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004172 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4173 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004174
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004175 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4176 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4177 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004178 if (ret)
4179 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004180
Yan, Zhenga22285a2010-05-16 10:48:46 -04004181 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004182 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004183 if (ret)
4184 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004185 }
Josef Bacik7b128762008-07-24 12:17:14 -04004186
Tsutomu Itohb5324022011-07-19 07:27:20 +00004187out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004188 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004189 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004190 return ret;
4191}
4192
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004193int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4194 struct btrfs_root *root,
4195 struct inode *dir, u64 objectid,
4196 const char *name, int name_len)
4197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004198 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199 struct btrfs_path *path;
4200 struct extent_buffer *leaf;
4201 struct btrfs_dir_item *di;
4202 struct btrfs_key key;
4203 u64 index;
4204 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004205 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206
4207 path = btrfs_alloc_path();
4208 if (!path)
4209 return -ENOMEM;
4210
Li Zefan33345d012011-04-20 10:31:50 +08004211 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004213 if (IS_ERR_OR_NULL(di)) {
4214 if (!di)
4215 ret = -ENOENT;
4216 else
4217 ret = PTR_ERR(di);
4218 goto out;
4219 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004220
4221 leaf = path->nodes[0];
4222 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4223 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4224 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004226 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227 goto out;
4228 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004229 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004230
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4232 root->root_key.objectid, dir_ino,
4233 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004236 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004237 goto out;
4238 }
Li Zefan33345d012011-04-20 10:31:50 +08004239 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (IS_ERR_OR_NULL(di)) {
4242 if (!di)
4243 ret = -ENOENT;
4244 else
4245 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004246 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 goto out;
4248 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249
4250 leaf = path->nodes[0];
4251 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004252 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 index = key.offset;
4254 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004255 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004257 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004258 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004259 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 goto out;
4261 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004262
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004263 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004264 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004265 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004266 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004268 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004269out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004270 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272}
4273
Chris Mason39279cc2007-06-12 06:35:45 -04004274static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4275{
David Howells2b0143b2015-03-17 22:25:59 +00004276 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004277 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004278 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004280 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004281
David Sterbab3ae2442012-09-13 16:04:34 -06004282 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004283 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004284 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004285 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004286
Josef Bacikd52be812013-05-29 14:54:47 -04004287 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004288 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004289 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004290
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004291 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292 err = btrfs_unlink_subvol(trans, root, dir,
4293 BTRFS_I(inode)->location.objectid,
4294 dentry->d_name.name,
4295 dentry->d_name.len);
4296 goto out;
4297 }
4298
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004299 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004300 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004301 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004302
Filipe Manana44f714d2016-06-06 16:11:13 +01004303 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4304
Chris Mason39279cc2007-06-12 06:35:45 -04004305 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004306 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4307 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4308 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004309 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004310 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004311 /*
4312 * Propagate the last_unlink_trans value of the deleted dir to
4313 * its parent directory. This is to prevent an unrecoverable
4314 * log tree in the case we do something like this:
4315 * 1) create dir foo
4316 * 2) create snapshot under dir foo
4317 * 3) delete the snapshot
4318 * 4) rmdir foo
4319 * 5) mkdir foo
4320 * 6) fsync foo or some file inside foo
4321 */
4322 if (last_unlink_trans >= trans->transid)
4323 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4324 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004325out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004326 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004327 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004328
Chris Mason39279cc2007-06-12 06:35:45 -04004329 return err;
4330}
4331
Chris Mason28f75a02015-02-04 06:59:29 -08004332static int truncate_space_check(struct btrfs_trans_handle *trans,
4333 struct btrfs_root *root,
4334 u64 bytes_deleted)
4335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004336 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004337 int ret;
4338
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004339 /*
4340 * This is only used to apply pressure to the enospc system, we don't
4341 * intend to use this reservation at all.
4342 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004343 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 bytes_deleted *= fs_info->nodesize;
4345 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004346 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004347 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004348 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004349 trans->transid,
4350 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004351 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004352 }
Chris Mason28f75a02015-02-04 06:59:29 -08004353 return ret;
4354
4355}
4356
Filipe Manana0305cd52015-10-16 12:34:25 +01004357static int truncate_inline_extent(struct inode *inode,
4358 struct btrfs_path *path,
4359 struct btrfs_key *found_key,
4360 const u64 item_end,
4361 const u64 new_size)
4362{
4363 struct extent_buffer *leaf = path->nodes[0];
4364 int slot = path->slots[0];
4365 struct btrfs_file_extent_item *fi;
4366 u32 size = (u32)(new_size - found_key->offset);
4367 struct btrfs_root *root = BTRFS_I(inode)->root;
4368
4369 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4370
4371 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4372 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004373 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004374
4375 /*
4376 * Zero out the remaining of the last page of our inline extent,
4377 * instead of directly truncating our inline extent here - that
4378 * would be much more complex (decompressing all the data, then
4379 * compressing the truncated data, which might be bigger than
4380 * the size of the inline extent, resize the extent, etc).
4381 * We release the path because to get the page we might need to
4382 * read the extent item from disk (data not in the page cache).
4383 */
4384 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304385 return btrfs_truncate_block(inode, offset, page_end - offset,
4386 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004387 }
4388
4389 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4390 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004391 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004392
4393 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4394 inode_sub_bytes(inode, item_end + 1 - new_size);
4395
4396 return 0;
4397}
4398
Chris Mason323ac952008-10-01 19:05:46 -04004399/*
Chris Mason39279cc2007-06-12 06:35:45 -04004400 * this can truncate away extent items, csum items and directory items.
4401 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004402 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004403 *
4404 * csum items that cross the new i_size are truncated to the new size
4405 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004406 *
4407 * min_type is the minimum key type to truncate down to. If set to 0, this
4408 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004409 */
Yan, Zheng80825102009-11-12 09:35:36 +00004410int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4411 struct btrfs_root *root,
4412 struct inode *inode,
4413 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004415 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004417 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004418 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004419 struct btrfs_key key;
4420 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004422 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004423 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004424 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004425 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004426 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 int found_extent;
4428 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004429 int pending_del_nr = 0;
4430 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004431 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004432 int ret;
4433 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004434 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004435 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004436 bool be_nice = 0;
4437 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004438 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004439
4440 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004441
Chris Mason28ed1342014-12-17 09:41:04 -08004442 /*
4443 * for non-free space inodes and ref cows, we want to back off from
4444 * time to time
4445 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004446 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004447 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4448 be_nice = 1;
4449
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004450 path = btrfs_alloc_path();
4451 if (!path)
4452 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004453 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004454
Josef Bacik5dc562c2012-08-17 13:14:17 -04004455 /*
4456 * We want to drop from the next block forward in case this new size is
4457 * not block aligned since we will be keeping the last block of the
4458 * extent just the way it is.
4459 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004460 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004462 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004463 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004464 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004465
Miao Xie16cdcec2011-04-22 18:12:22 +08004466 /*
4467 * This function is also used to drop the items in the log tree before
4468 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4469 * it is used to drop the loged items. So we shouldn't kill the delayed
4470 * items.
4471 */
4472 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004473 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004474
Li Zefan33345d012011-04-20 10:31:50 +08004475 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004477 key.type = (u8)-1;
4478
Chris Mason85e21ba2008-01-29 15:11:36 -05004479search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004480 /*
4481 * with a 16K leaf size and 128MB extents, you can actually queue
4482 * up a huge file in a single leaf. Most of the time that
4483 * bytes_deleted is > 0, it will be huge by the time we get here
4484 */
Byongho Leeee221842015-12-15 01:42:10 +09004485 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004486 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004487 err = -EAGAIN;
4488 goto error;
4489 }
4490 }
4491
4492
Chris Masonb9473432009-03-13 11:00:37 -04004493 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004494 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004495 if (ret < 0) {
4496 err = ret;
4497 goto out;
4498 }
Chris Masond3977122009-01-05 21:25:51 -05004499
Chris Mason85e21ba2008-01-29 15:11:36 -05004500 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004501 /* there are no items in the tree for us to truncate, we're
4502 * done
4503 */
Yan, Zheng80825102009-11-12 09:35:36 +00004504 if (path->slots[0] == 0)
4505 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004506 path->slots[0]--;
4507 }
4508
Chris Masond3977122009-01-05 21:25:51 -05004509 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004510 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004511 leaf = path->nodes[0];
4512 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004513 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004514
Li Zefan33345d012011-04-20 10:31:50 +08004515 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004516 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004517
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004519 break;
4520
Chris Mason5f39d392007-10-15 16:14:19 -04004521 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004522 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004523 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004524 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004525 extent_type = btrfs_file_extent_type(leaf, fi);
4526 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004527 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004528 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004529
4530 trace_btrfs_truncate_show_fi_regular(
4531 BTRFS_I(inode), leaf, fi,
4532 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004533 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004534 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004535 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004536
4537 trace_btrfs_truncate_show_fi_inline(
4538 BTRFS_I(inode), leaf, fi, path->slots[0],
4539 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004540 }
Yan008630c2007-11-07 13:31:09 -05004541 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004542 }
Yan, Zheng80825102009-11-12 09:35:36 +00004543 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004544 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004545 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004546 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004547 break;
4548 if (found_key.offset >= new_size)
4549 del_item = 1;
4550 else
4551 del_item = 0;
4552 }
Chris Mason39279cc2007-06-12 06:35:45 -04004553 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004554 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004555 if (found_type != BTRFS_EXTENT_DATA_KEY)
4556 goto delete;
4557
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004558 if (del_item)
4559 last_size = found_key.offset;
4560 else
4561 last_size = new_size;
4562
Chris Mason179e29e2007-11-01 11:28:41 -04004563 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004564 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004565 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004566 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004567 u64 orig_num_bytes =
4568 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004569 extent_num_bytes = ALIGN(new_size -
4570 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004571 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004572 btrfs_set_file_extent_num_bytes(leaf, fi,
4573 extent_num_bytes);
4574 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004575 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004576 if (test_bit(BTRFS_ROOT_REF_COWS,
4577 &root->state) &&
4578 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004579 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004580 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004581 } else {
Chris Masondb945352007-10-15 16:15:53 -04004582 extent_num_bytes =
4583 btrfs_file_extent_disk_num_bytes(leaf,
4584 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 extent_offset = found_key.offset -
4586 btrfs_file_extent_offset(leaf, fi);
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004589 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (extent_start != 0) {
4591 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004592 if (test_bit(BTRFS_ROOT_REF_COWS,
4593 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004594 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004595 }
4596 }
Chris Mason90692182008-02-08 13:49:28 -05004597 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004598 /*
4599 * we can't truncate inline items that have had
4600 * special encodings
4601 */
4602 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004603 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4604 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004605
4606 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004607 * Need to release path in order to truncate a
4608 * compressed extent. So delete any accumulated
4609 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004610 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004611 if (btrfs_file_extent_compression(leaf, fi) !=
4612 BTRFS_COMPRESS_NONE && pending_del_nr) {
4613 err = btrfs_del_items(trans, root, path,
4614 pending_del_slot,
4615 pending_del_nr);
4616 if (err) {
4617 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004618 err);
4619 goto error;
4620 }
4621 pending_del_nr = 0;
4622 }
4623
4624 err = truncate_inline_extent(inode, path,
4625 &found_key,
4626 item_end,
4627 new_size);
4628 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004629 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004630 goto error;
4631 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004632 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4633 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004634 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004635 }
Chris Mason39279cc2007-06-12 06:35:45 -04004636 }
Chris Mason179e29e2007-11-01 11:28:41 -04004637delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004638 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004639 if (!pending_del_nr) {
4640 /* no pending yet, add ourselves */
4641 pending_del_slot = path->slots[0];
4642 pending_del_nr = 1;
4643 } else if (pending_del_nr &&
4644 path->slots[0] + 1 == pending_del_slot) {
4645 /* hop on the pending chunk */
4646 pending_del_nr++;
4647 pending_del_slot = path->slots[0];
4648 } else {
Chris Masond3977122009-01-05 21:25:51 -05004649 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004650 }
Chris Mason39279cc2007-06-12 06:35:45 -04004651 } else {
4652 break;
4653 }
Chris Mason28f75a02015-02-04 06:59:29 -08004654 should_throttle = 0;
4655
Miao Xie27cdeb72014-04-02 19:51:05 +08004656 if (found_extent &&
4657 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004658 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004659 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004660 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004661 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004662 extent_num_bytes, 0,
4663 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004664 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004665 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004666 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4667 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004668 trans->delayed_ref_updates * 2,
4669 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004670 if (be_nice) {
4671 if (truncate_space_check(trans, root,
4672 extent_num_bytes)) {
4673 should_end = 1;
4674 }
4675 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004676 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004677 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004678 }
Chris Mason39279cc2007-06-12 06:35:45 -04004679 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004680
Yan, Zheng80825102009-11-12 09:35:36 +00004681 if (found_type == BTRFS_INODE_ITEM_KEY)
4682 break;
4683
4684 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004685 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004686 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004687 if (pending_del_nr) {
4688 ret = btrfs_del_items(trans, root, path,
4689 pending_del_slot,
4690 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004691 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004692 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004693 goto error;
4694 }
Yan, Zheng80825102009-11-12 09:35:36 +00004695 pending_del_nr = 0;
4696 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004697 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004698 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004699 unsigned long updates = trans->delayed_ref_updates;
4700 if (updates) {
4701 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004702 ret = btrfs_run_delayed_refs(trans,
4703 fs_info,
4704 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004705 if (ret && !err)
4706 err = ret;
4707 }
4708 }
Chris Mason28f75a02015-02-04 06:59:29 -08004709 /*
4710 * if we failed to refill our space rsv, bail out
4711 * and let the transaction restart
4712 */
4713 if (should_end) {
4714 err = -EAGAIN;
4715 goto error;
4716 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004717 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004718 } else {
4719 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004720 }
Chris Mason39279cc2007-06-12 06:35:45 -04004721 }
Yan, Zheng80825102009-11-12 09:35:36 +00004722out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004723 if (pending_del_nr) {
4724 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4725 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004727 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004729error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004730 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4731 ASSERT(last_size >= new_size);
4732 if (!err && last_size > new_size)
4733 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004734 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004735 }
Chris Mason28ed1342014-12-17 09:41:04 -08004736
Chris Mason39279cc2007-06-12 06:35:45 -04004737 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004738
Byongho Leeee221842015-12-15 01:42:10 +09004739 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004740 unsigned long updates = trans->delayed_ref_updates;
4741 if (updates) {
4742 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004743 ret = btrfs_run_delayed_refs(trans, fs_info,
4744 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004745 if (ret && !err)
4746 err = ret;
4747 }
4748 }
Yan, Zheng80825102009-11-12 09:35:36 +00004749 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004750}
4751
Chris Masona52d9a82007-08-27 16:49:44 -04004752/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004754 * @inode - inode that we're zeroing
4755 * @from - the offset to start zeroing
4756 * @len - the length to zero, 0 to zero the entire range respective to the
4757 * offset
4758 * @front - zero up to the offset instead of from the offset on
4759 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004762 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004764 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004767 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004768 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4769 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004770 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004771 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004773 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004775 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004776 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004777 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 u64 block_start;
4779 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004780
Josef Bacik2aaa6652012-08-29 14:27:18 -04004781 if ((offset & (blocksize - 1)) == 0 &&
4782 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004783 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004785 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004787 if (ret)
4788 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004789
Chris Mason211c17f2008-05-15 09:13:45 -04004790again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004791 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004792 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004793 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304794 round_down(from, blocksize),
4795 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004796 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004797 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004798 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004799
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304800 block_start = round_down(from, blocksize);
4801 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004802
Chris Masona52d9a82007-08-27 16:49:44 -04004803 if (!PageUptodate(page)) {
4804 ret = btrfs_readpage(NULL, page);
4805 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004806 if (page->mapping != mapping) {
4807 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004808 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004809 goto again;
4810 }
Chris Masona52d9a82007-08-27 16:49:44 -04004811 if (!PageUptodate(page)) {
4812 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004813 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004814 }
4815 }
Chris Mason211c17f2008-05-15 09:13:45 -04004816 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004819 set_page_extent_mapped(page);
4820
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004822 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004824 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004826 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004827 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004828 btrfs_put_ordered_extent(ordered);
4829 goto again;
4830 }
4831
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304832 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004833 EXTENT_DIRTY | EXTENT_DELALLOC |
4834 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004835 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004836
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004838 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004839 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004841 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004842 goto out_unlock;
4843 }
4844
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304845 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004846 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304847 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004848 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004849 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304850 memset(kaddr + (block_start - page_offset(page)),
4851 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004852 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304853 memset(kaddr + (block_start - page_offset(page)) + offset,
4854 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 flush_dcache_page(page);
4856 kunmap(page);
4857 }
Chris Mason247e7432008-07-17 12:53:51 -04004858 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004859 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004861 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004862
Chris Mason89642222008-07-24 09:41:53 -04004863out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004864 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304865 btrfs_delalloc_release_space(inode, block_start,
4866 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004867 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004868 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004869out:
4870 return ret;
4871}
4872
Josef Bacik16e75492013-10-22 12:18:51 -04004873static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4874 u64 offset, u64 len)
4875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004877 struct btrfs_trans_handle *trans;
4878 int ret;
4879
4880 /*
4881 * Still need to make sure the inode looks like it's been updated so
4882 * that any holes get logged if we fsync.
4883 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4885 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004886 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4887 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4888 return 0;
4889 }
4890
4891 /*
4892 * 1 - for the one we're dropping
4893 * 1 - for the one we're adding
4894 * 1 - for updating the inode.
4895 */
4896 trans = btrfs_start_transaction(root, 3);
4897 if (IS_ERR(trans))
4898 return PTR_ERR(trans);
4899
4900 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004902 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004903 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004904 return ret;
4905 }
4906
David Sterbaf85b7372017-01-20 14:54:07 +01004907 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4908 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004909 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004910 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004911 else
4912 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004913 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004914 return ret;
4915}
4916
Josef Bacik695a0d02011-03-04 15:46:53 -05004917/*
4918 * This function puts in dummy file extents for the area we're creating a hole
4919 * for. So if we are truncating this file to a larger size we need to insert
4920 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4921 * the range between oldsize and size
4922 */
Josef Bacika41ad392011-01-31 15:30:16 -05004923int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004924{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004926 struct btrfs_root *root = BTRFS_I(inode)->root;
4927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004928 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004929 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004930 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4932 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004933 u64 last_byte;
4934 u64 cur_offset;
4935 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004936 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004937
Josef Bacika71754f2013-06-17 17:14:39 -04004938 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304939 * If our size started in the middle of a block we need to zero out the
4940 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004941 * expose stale data.
4942 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304943 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004944 if (err)
4945 return err;
4946
Yan Zheng9036c102008-10-30 14:19:41 -04004947 if (size <= hole_start)
4948 return 0;
4949
Yan Zheng9036c102008-10-30 14:19:41 -04004950 while (1) {
4951 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004952
David Sterbaff13db42015-12-03 14:30:40 +01004953 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004954 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004955 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004956 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004957 if (!ordered)
4958 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004959 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4960 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004961 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004962 btrfs_put_ordered_extent(ordered);
4963 }
4964
Yan Zheng9036c102008-10-30 14:19:41 -04004965 cur_offset = hole_start;
4966 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004967 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004968 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004969 if (IS_ERR(em)) {
4970 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004971 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004972 break;
4973 }
Yan Zheng9036c102008-10-30 14:19:41 -04004974 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004975 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004976 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004977 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004978 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004979
Josef Bacik16e75492013-10-22 12:18:51 -04004980 err = maybe_insert_hole(root, inode, cur_offset,
4981 hole_size);
4982 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004983 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004984 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004985 cur_offset + hole_size - 1, 0);
4986 hole_em = alloc_extent_map();
4987 if (!hole_em) {
4988 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4989 &BTRFS_I(inode)->runtime_flags);
4990 goto next;
4991 }
4992 hole_em->start = cur_offset;
4993 hole_em->len = hole_size;
4994 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004995
Josef Bacik5dc562c2012-08-17 13:14:17 -04004996 hole_em->block_start = EXTENT_MAP_HOLE;
4997 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004998 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004999 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005000 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005001 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005002 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005003
5004 while (1) {
5005 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005006 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005007 write_unlock(&em_tree->lock);
5008 if (err != -EEXIST)
5009 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005010 btrfs_drop_extent_cache(BTRFS_I(inode),
5011 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012 cur_offset +
5013 hole_size - 1, 0);
5014 }
5015 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005016 }
Josef Bacik16e75492013-10-22 12:18:51 -04005017next:
Yan Zheng9036c102008-10-30 14:19:41 -04005018 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005019 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005020 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005021 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005022 break;
5023 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005024 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005025 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5026 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005027 return err;
5028}
5029
Eric Sandeen3972f262013-01-12 02:57:22 +00005030static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005031{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005032 struct btrfs_root *root = BTRFS_I(inode)->root;
5033 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005034 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005035 loff_t newsize = attr->ia_size;
5036 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005037 int ret;
5038
Eric Sandeen3972f262013-01-12 02:57:22 +00005039 /*
5040 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5041 * special case where we need to update the times despite not having
5042 * these flags set. For all other operations the VFS set these flags
5043 * explicitly if it wants a timestamp update.
5044 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005045 if (newsize != oldsize) {
5046 inode_inc_iversion(inode);
5047 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5048 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005049 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005050 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005051
Josef Bacika41ad392011-01-31 15:30:16 -05005052 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005053 /*
5054 * Don't do an expanding truncate while snapshoting is ongoing.
5055 * This is to ensure the snapshot captures a fully consistent
5056 * state of this file - if the snapshot captures this expanding
5057 * truncation, it must capture all writes that happened before
5058 * this truncation.
5059 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005060 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005061 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 if (ret) {
5063 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005064 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005065 }
Yan, Zheng80825102009-11-12 09:35:36 +00005066
Miao Xief4a2f4c2011-12-14 20:12:01 -05005067 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005068 if (IS_ERR(trans)) {
5069 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005070 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005071 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005072
5073 i_size_write(inode, newsize);
5074 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305075 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005076 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005077 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005078 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005079 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005080
Josef Bacika41ad392011-01-31 15:30:16 -05005081 /*
5082 * We're truncating a file that used to have good data down to
5083 * zero. Make sure it gets into the ordered flush list so that
5084 * any new writes get down to disk quickly.
5085 */
5086 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005087 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5088 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005089
Josef Bacikf3fe8202013-01-07 17:03:21 -05005090 /*
5091 * 1 for the orphan item we're going to add
5092 * 1 for the orphan item deletion.
5093 */
5094 trans = btrfs_start_transaction(root, 2);
5095 if (IS_ERR(trans))
5096 return PTR_ERR(trans);
5097
5098 /*
5099 * We need to do this in case we fail at _any_ point during the
5100 * actual truncate. Once we do the truncate_setsize we could
5101 * invalidate pages which forces any outstanding ordered io to
5102 * be instantly completed which will give us extents that need
5103 * to be truncated. If we fail to get an orphan inode down we
5104 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005105 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005106 * will be consistent.
5107 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005108 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005109 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005110 if (ret)
5111 return ret;
5112
Josef Bacika41ad392011-01-31 15:30:16 -05005113 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5114 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005115
5116 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005117 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005118 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005119 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005120
Josef Bacika41ad392011-01-31 15:30:16 -05005121 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005122 if (ret && inode->i_nlink) {
5123 int err;
5124
Liu Bo19fd2df2016-12-01 13:01:02 -08005125 /* To get a stable disk_i_size */
5126 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5127 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005128 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005129 return err;
5130 }
5131
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005132 /*
5133 * failed to truncate, disk_i_size is only adjusted down
5134 * as we remove extents, so it should represent the true
5135 * size of the inode, so reset the in memory size and
5136 * delete our orphan entry.
5137 */
5138 trans = btrfs_join_transaction(root);
5139 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005140 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005141 return ret;
5142 }
5143 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005144 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005145 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005146 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005147 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005148 }
Yan, Zheng80825102009-11-12 09:35:36 +00005149 }
5150
Josef Bacika41ad392011-01-31 15:30:16 -05005151 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005152}
5153
Chris Mason39279cc2007-06-12 06:35:45 -04005154static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5155{
David Howells2b0143b2015-03-17 22:25:59 +00005156 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005157 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005158 int err;
5159
Li Zefanb83cc962010-12-20 16:04:08 +08005160 if (btrfs_root_readonly(root))
5161 return -EROFS;
5162
Jan Kara31051c82016-05-26 16:55:18 +02005163 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005164 if (err)
5165 return err;
5166
Chris Mason5a3f23d2009-03-31 13:27:11 -04005167 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005168 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005169 if (err)
5170 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005171 }
Yan Zheng9036c102008-10-30 14:19:41 -04005172
Christoph Hellwig10257742010-06-04 11:30:02 +02005173 if (attr->ia_valid) {
5174 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005175 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005176 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005177
Josef Bacik22c44fe2011-11-30 10:45:38 -05005178 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005179 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005180 }
5181
Chris Mason39279cc2007-06-12 06:35:45 -04005182 return err;
5183}
Chris Mason61295eb2008-01-14 16:24:38 -05005184
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005185/*
5186 * While truncating the inode pages during eviction, we get the VFS calling
5187 * btrfs_invalidatepage() against each page of the inode. This is slow because
5188 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5189 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5190 * extent_state structures over and over, wasting lots of time.
5191 *
5192 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5193 * those expensive operations on a per page basis and do only the ordered io
5194 * finishing, while we release here the extent_map and extent_state structures,
5195 * without the excessive merging and splitting.
5196 */
5197static void evict_inode_truncate_pages(struct inode *inode)
5198{
5199 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5200 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5201 struct rb_node *node;
5202
5203 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005204 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205
5206 write_lock(&map_tree->lock);
5207 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5208 struct extent_map *em;
5209
5210 node = rb_first(&map_tree->map);
5211 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005212 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5213 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005214 remove_extent_mapping(map_tree, em);
5215 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005216 if (need_resched()) {
5217 write_unlock(&map_tree->lock);
5218 cond_resched();
5219 write_lock(&map_tree->lock);
5220 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 }
5222 write_unlock(&map_tree->lock);
5223
Filipe Manana6ca07092015-05-26 00:55:42 +01005224 /*
5225 * Keep looping until we have no more ranges in the io tree.
5226 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005227 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5228 * still in progress (unlocked the pages in the bio but did not yet
5229 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005230 * ranges can still be locked and eviction started because before
5231 * submitting those bios, which are executed by a separate task (work
5232 * queue kthread), inode references (inode->i_count) were not taken
5233 * (which would be dropped in the end io callback of each bio).
5234 * Therefore here we effectively end up waiting for those bios and
5235 * anyone else holding locked ranges without having bumped the inode's
5236 * reference count - if we don't do it, when they access the inode's
5237 * io_tree to unlock a range it may be too late, leading to an
5238 * use-after-free issue.
5239 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005240 spin_lock(&io_tree->lock);
5241 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5242 struct extent_state *state;
5243 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005244 u64 start;
5245 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005246
5247 node = rb_first(&io_tree->state);
5248 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005249 start = state->start;
5250 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005251 spin_unlock(&io_tree->lock);
5252
David Sterbaff13db42015-12-03 14:30:40 +01005253 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005254
5255 /*
5256 * If still has DELALLOC flag, the extent didn't reach disk,
5257 * and its reserved space won't be freed by delayed_ref.
5258 * So we need to free its reserved space here.
5259 * (Refer to comment in btrfs_invalidatepage, case 2)
5260 *
5261 * Note, end is the bytenr of last byte, so we need + 1 here.
5262 */
5263 if (state->state & EXTENT_DELALLOC)
5264 btrfs_qgroup_free_data(inode, start, end - start + 1);
5265
Filipe Manana6ca07092015-05-26 00:55:42 +01005266 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005267 EXTENT_LOCKED | EXTENT_DIRTY |
5268 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5269 EXTENT_DEFRAG, 1, 1,
5270 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005271
Filipe Manana7064dd52014-08-08 02:47:05 +01005272 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005273 spin_lock(&io_tree->lock);
5274 }
5275 spin_unlock(&io_tree->lock);
5276}
5277
Al Virobd555972010-06-07 11:35:40 -04005278void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005281 struct btrfs_trans_handle *trans;
5282 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005283 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005284 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005285 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005286 int ret;
5287
liubo1abe9b82011-03-24 11:18:59 +00005288 trace_btrfs_inode_evict(inode);
5289
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005290 if (!root) {
5291 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5292 return;
5293 }
5294
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005295 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005296
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005297 evict_inode_truncate_pages(inode);
5298
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005299 if (inode->i_nlink &&
5300 ((btrfs_root_refs(&root->root_item) != 0 &&
5301 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005302 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005303 goto no_delete;
5304
Chris Mason39279cc2007-06-12 06:35:45 -04005305 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005306 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005307 goto no_delete;
5308 }
Al Virobd555972010-06-07 11:35:40 -04005309 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005310 if (!special_file(inode->i_mode))
5311 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005312
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005313 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005314
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005315 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005316 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005317 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005318 goto no_delete;
5319 }
5320
Yan, Zheng76dda932009-09-21 16:00:26 -04005321 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005322 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5323 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005324 goto no_delete;
5325 }
5326
Nikolay Borisovaa790212017-01-10 20:35:40 +02005327 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005328 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005329 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005330 goto no_delete;
5331 }
5332
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005333 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005334 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005335 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005336 goto no_delete;
5337 }
Josef Bacik4a338542011-08-29 11:01:31 -04005338 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005339 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005340 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005341
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005342 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005343
Josef Bacik4289a662011-08-05 13:22:24 -04005344 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005345 * This is a bit simpler than btrfs_truncate since we've already
5346 * reserved our space for our orphan item in the unlink, so we just
5347 * need to reserve some slack space in case we add bytes and update
5348 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005349 */
Yan, Zheng80825102009-11-12 09:35:36 +00005350 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005351 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5352 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005353
Josef Bacik726c35f2011-09-26 15:46:06 -04005354 /*
5355 * Try and steal from the global reserve since we will
5356 * likely not use this space anyway, we want to try as
5357 * hard as possible to get this to work.
5358 */
5359 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005360 steal_from_global++;
5361 else
5362 steal_from_global = 0;
5363 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005364
Josef Bacik3bce8762015-02-24 12:35:51 -08005365 /*
5366 * steal_from_global == 0: we reserved stuff, hooray!
5367 * steal_from_global == 1: we didn't reserve stuff, boo!
5368 * steal_from_global == 2: we've committed, still not a lot of
5369 * room but maybe we'll have room in the global reserve this
5370 * time.
5371 * steal_from_global == 3: abandon all hope!
5372 */
5373 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005374 btrfs_warn(fs_info,
5375 "Could not get space for a delete, will truncate on mount %d",
5376 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005377 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005378 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005379 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005380 }
5381
Miao Xie0e8c36a2012-12-19 06:59:51 +00005382 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005383 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005384 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005386 goto no_delete;
5387 }
5388
Josef Bacik3bce8762015-02-24 12:35:51 -08005389 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005390 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005391 * sure there is room to do it, if not we need to commit and try
5392 * again.
5393 */
5394 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005395 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005396 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005397 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 else
5399 ret = -ENOSPC;
5400 }
5401
5402 /*
5403 * Couldn't steal from the global reserve, we have too much
5404 * pending stuff built up, commit the transaction and try it
5405 * again.
5406 */
5407 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005408 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005409 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005410 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005411 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005412 goto no_delete;
5413 }
5414 continue;
5415 } else {
5416 steal_from_global = 0;
5417 }
5418
Josef Bacik4289a662011-08-05 13:22:24 -04005419 trans->block_rsv = rsv;
5420
Yan, Zhengd68fc572010-05-16 10:49:58 -04005421 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005422 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005423 break;
5424
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005425 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005426 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005427 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005428 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005429 }
5430
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005431 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005432
Josef Bacik4ef31a42013-08-13 14:10:08 -04005433 /*
5434 * Errors here aren't a big deal, it just means we leave orphan items
5435 * in the tree. They will be cleaned up on the next mount.
5436 */
Yan, Zheng80825102009-11-12 09:35:36 +00005437 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005438 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005439 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005440 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005441 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005442 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005444 trans->block_rsv = &fs_info->trans_block_rsv;
5445 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005446 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005447 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005448
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005449 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005451no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005452 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005453 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005454}
5455
5456/*
5457 * this returns the key found in the dir entry in the location pointer.
5458 * If no dir entries were found, location->objectid is 0.
5459 */
5460static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5461 struct btrfs_key *location)
5462{
5463 const char *name = dentry->d_name.name;
5464 int namelen = dentry->d_name.len;
5465 struct btrfs_dir_item *di;
5466 struct btrfs_path *path;
5467 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005468 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005469
5470 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005471 if (!path)
5472 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005473
David Sterbaf85b7372017-01-20 14:54:07 +01005474 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5475 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005476 if (IS_ERR(di))
5477 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005478
David Sterbac7040052011-04-19 18:00:01 +02005479 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005480 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005481
Chris Mason5f39d392007-10-15 16:14:19 -04005482 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005483out:
Chris Mason39279cc2007-06-12 06:35:45 -04005484 btrfs_free_path(path);
5485 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005486out_err:
5487 location->objectid = 0;
5488 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005489}
5490
5491/*
5492 * when we hit a tree root in a directory, the btrfs part of the inode
5493 * needs to be changed to reflect the root directory of the tree root. This
5494 * is kind of like crossing a mount point.
5495 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005496static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 struct inode *dir,
5498 struct dentry *dentry,
5499 struct btrfs_key *location,
5500 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005501{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 struct btrfs_path *path;
5503 struct btrfs_root *new_root;
5504 struct btrfs_root_ref *ref;
5505 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005506 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 int ret;
5508 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 path = btrfs_alloc_path();
5511 if (!path) {
5512 err = -ENOMEM;
5513 goto out;
5514 }
Chris Mason39279cc2007-06-12 06:35:45 -04005515
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005517 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5518 key.type = BTRFS_ROOT_REF_KEY;
5519 key.offset = location->objectid;
5520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005521 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 if (ret) {
5523 if (ret < 0)
5524 err = ret;
5525 goto out;
5526 }
Chris Mason39279cc2007-06-12 06:35:45 -04005527
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005528 leaf = path->nodes[0];
5529 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5532 goto out;
5533
5534 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5535 (unsigned long)(ref + 1),
5536 dentry->d_name.len);
5537 if (ret)
5538 goto out;
5539
David Sterbab3b4aa72011-04-21 01:20:15 +02005540 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005542 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543 if (IS_ERR(new_root)) {
5544 err = PTR_ERR(new_root);
5545 goto out;
5546 }
5547
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 *sub_root = new_root;
5549 location->objectid = btrfs_root_dirid(&new_root->root_item);
5550 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005551 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005552 err = 0;
5553out:
5554 btrfs_free_path(path);
5555 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005556}
5557
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558static void inode_tree_add(struct inode *inode)
5559{
5560 struct btrfs_root *root = BTRFS_I(inode)->root;
5561 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005562 struct rb_node **p;
5563 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005564 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005565 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566
Al Viro1d3382cb2010-10-23 15:19:20 -04005567 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005569 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005571 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 while (*p) {
5573 parent = *p;
5574 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5575
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005576 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005578 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 else {
5581 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005582 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005583 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005584 RB_CLEAR_NODE(parent);
5585 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005586 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 }
5588 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005589 rb_link_node(new, parent, p);
5590 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 spin_unlock(&root->inode_lock);
5592}
5593
5594static void inode_tree_del(struct inode *inode)
5595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005604 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005606 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005607
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005608 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005609 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005610 spin_lock(&root->inode_lock);
5611 empty = RB_EMPTY_ROOT(&root->inode_tree);
5612 spin_unlock(&root->inode_lock);
5613 if (empty)
5614 btrfs_add_dead_root(root);
5615 }
5616}
5617
Jeff Mahoney143bede2012-03-01 14:56:26 +01005618void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005619{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005620 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005621 struct rb_node *node;
5622 struct rb_node *prev;
5623 struct btrfs_inode *entry;
5624 struct inode *inode;
5625 u64 objectid = 0;
5626
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005628 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005629
5630 spin_lock(&root->inode_lock);
5631again:
5632 node = root->inode_tree.rb_node;
5633 prev = NULL;
5634 while (node) {
5635 prev = node;
5636 entry = rb_entry(node, struct btrfs_inode, rb_node);
5637
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005638 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005639 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005640 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 node = node->rb_right;
5642 else
5643 break;
5644 }
5645 if (!node) {
5646 while (prev) {
5647 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005648 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005649 node = prev;
5650 break;
5651 }
5652 prev = rb_next(prev);
5653 }
5654 }
5655 while (node) {
5656 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005657 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005658 inode = igrab(&entry->vfs_inode);
5659 if (inode) {
5660 spin_unlock(&root->inode_lock);
5661 if (atomic_read(&inode->i_count) > 1)
5662 d_prune_aliases(inode);
5663 /*
Al Viro45321ac2010-06-07 13:43:19 -04005664 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 * the inode cache when its usage count
5666 * hits zero.
5667 */
5668 iput(inode);
5669 cond_resched();
5670 spin_lock(&root->inode_lock);
5671 goto again;
5672 }
5673
5674 if (cond_resched_lock(&root->inode_lock))
5675 goto again;
5676
5677 node = rb_next(node);
5678 }
5679 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680}
5681
Chris Masone02119d2008-09-05 16:13:11 -04005682static int btrfs_init_locked_inode(struct inode *inode, void *p)
5683{
5684 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005685 inode->i_ino = args->location->objectid;
5686 memcpy(&BTRFS_I(inode)->location, args->location,
5687 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005688 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005689 return 0;
5690}
5691
5692static int btrfs_find_actor(struct inode *inode, void *opaque)
5693{
5694 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005695 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005696 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005697}
5698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005700 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005702{
5703 struct inode *inode;
5704 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005705 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005706
Chris Mason90d3e592014-01-09 17:28:00 -08005707 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 args.root = root;
5709
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005710 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005711 btrfs_init_locked_inode,
5712 (void *)&args);
5713 return inode;
5714}
5715
Balaji Rao1a54ef82008-07-21 02:01:04 +05305716/* Get an inode object given its location and corresponding root.
5717 * Returns in *is_new if the inode was read from disk
5718 */
5719struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005720 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305721{
5722 struct inode *inode;
5723
Chris Mason90d3e592014-01-09 17:28:00 -08005724 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305725 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005726 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305727
5728 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005729 int ret;
5730
5731 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005732 if (!is_bad_inode(inode)) {
5733 inode_tree_add(inode);
5734 unlock_new_inode(inode);
5735 if (new)
5736 *new = 1;
5737 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005738 unlock_new_inode(inode);
5739 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005740 ASSERT(ret < 0);
5741 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005742 }
5743 }
5744
Balaji Rao1a54ef82008-07-21 02:01:04 +05305745 return inode;
5746}
5747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748static struct inode *new_simple_dir(struct super_block *s,
5749 struct btrfs_key *key,
5750 struct btrfs_root *root)
5751{
5752 struct inode *inode = new_inode(s);
5753
5754 if (!inode)
5755 return ERR_PTR(-ENOMEM);
5756
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 BTRFS_I(inode)->root = root;
5758 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005759 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760
5761 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005762 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005763 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 inode->i_fop = &simple_dir_operations;
5765 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005766 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305767 inode->i_atime = inode->i_mtime;
5768 inode->i_ctime = inode->i_mtime;
5769 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770
5771 return inode;
5772}
5773
Chris Mason3de45862008-11-17 21:02:50 -05005774struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005777 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005779 struct btrfs_root *sub_root = root;
5780 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005781 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005782 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005783
5784 if (dentry->d_name.len > BTRFS_NAME_LEN)
5785 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005786
Jeff Layton39e3c952012-11-28 11:30:53 -05005787 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005788 if (ret < 0)
5789 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005790
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005792 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793
5794 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005795 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005797 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798
5799 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5800
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005802 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803 &location, &sub_root);
5804 if (ret < 0) {
5805 if (ret != -ENOENT)
5806 inode = ERR_PTR(ret);
5807 else
5808 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5809 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005810 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005811 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005812 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005813
Julia Lawall34d19ba2011-01-24 19:55:19 +00005814 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005815 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005816 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005817 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005818 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005819 if (ret) {
5820 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005821 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005822 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005823 }
5824
Chris Mason3de45862008-11-17 21:02:50 -05005825 return inode;
5826}
5827
Nick Pigginfe15ce42011-01-07 17:49:23 +11005828static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005829{
5830 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005831 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005832
Li Zefan848cce02012-02-21 17:04:28 +08005833 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005834 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005835
Li Zefan848cce02012-02-21 17:04:28 +08005836 if (inode) {
5837 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005838 if (btrfs_root_refs(&root->root_item) == 0)
5839 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005840
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005841 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005842 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005843 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005844 return 0;
5845}
5846
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005847static void btrfs_dentry_release(struct dentry *dentry)
5848{
Daeseok Youn944a4512014-04-14 15:37:02 +09005849 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005850}
5851
Chris Mason3de45862008-11-17 21:02:50 -05005852static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005853 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005854{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005855 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005856
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005857 inode = btrfs_lookup_dentry(dir, dentry);
5858 if (IS_ERR(inode)) {
5859 if (PTR_ERR(inode) == -ENOENT)
5860 inode = NULL;
5861 else
5862 return ERR_CAST(inode);
5863 }
5864
Al Viro41d28bc2014-10-12 22:24:21 -04005865 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005866}
5867
Miao Xie16cdcec2011-04-22 18:12:22 +08005868unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005869 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5870};
5871
Al Viro9cdda8d2013-05-22 16:48:09 -04005872static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005873{
Al Viro9cdda8d2013-05-22 16:48:09 -04005874 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005876 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005877 struct btrfs_dir_item *di;
5878 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005879 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005880 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005881 struct list_head ins_list;
5882 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005883 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005884 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005886 unsigned char d_type;
5887 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005888 char tmp_name[32];
5889 char *name_ptr;
5890 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005891 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005892 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005893
Al Viro9cdda8d2013-05-22 16:48:09 -04005894 if (!dir_emit_dots(file, ctx))
5895 return 0;
5896
David Woodhouse49593bf2008-08-17 17:08:36 +01005897 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005898 if (!path)
5899 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005900
David Sterbae4058b52015-11-27 16:31:35 +01005901 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005902
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005903 INIT_LIST_HEAD(&ins_list);
5904 INIT_LIST_HEAD(&del_list);
5905 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005906
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005907 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005908 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005909 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005910
Chris Mason39279cc2007-06-12 06:35:45 -04005911 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5912 if (ret < 0)
5913 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005914
5915 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005916 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005917 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005918 if (slot >= btrfs_header_nritems(leaf)) {
5919 ret = btrfs_next_leaf(root, path);
5920 if (ret < 0)
5921 goto err;
5922 else if (ret > 0)
5923 break;
5924 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005925 }
Chris Mason3de45862008-11-17 21:02:50 -05005926
Chris Mason5f39d392007-10-15 16:14:19 -04005927 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5928
5929 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005930 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005931 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005932 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005933 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005934 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005935 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005936 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005937
Al Viro9cdda8d2013-05-22 16:48:09 -04005938 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005939
Chris Mason39279cc2007-06-12 06:35:45 -04005940 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005941 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005942 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005943
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005944 name_len = btrfs_dir_name_len(leaf, di);
5945 if (name_len <= sizeof(tmp_name)) {
5946 name_ptr = tmp_name;
5947 } else {
5948 name_ptr = kmalloc(name_len, GFP_KERNEL);
5949 if (!name_ptr) {
5950 ret = -ENOMEM;
5951 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005952 }
Chris Mason39279cc2007-06-12 06:35:45 -04005953 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005954 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5955 name_len);
5956
5957 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5958 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5959
5960 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5961 d_type);
5962
5963 if (name_ptr != tmp_name)
5964 kfree(name_ptr);
5965
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 if (over)
5967 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005968 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005969next:
5970 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005971 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005972
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005973 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005974 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005975 goto nopos;
5976
Zach Browndb62efb2013-07-11 16:19:42 -07005977 /*
5978 * Stop new entries from being returned after we return the last
5979 * entry.
5980 *
5981 * New directory entries are assigned a strictly increasing
5982 * offset. This means that new entries created during readdir
5983 * are *guaranteed* to be seen in the future by that readdir.
5984 * This has broken buggy programs which operate on names as
5985 * they're returned by readdir. Until we re-use freed offsets
5986 * we have this hack to stop new entries from being returned
5987 * under the assumption that they'll never reach this huge
5988 * offset.
5989 *
5990 * This is being careful not to overflow 32bit loff_t unless the
5991 * last entry requires it because doing so has broken 32bit apps
5992 * in the past.
5993 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005994 if (ctx->pos >= INT_MAX)
5995 ctx->pos = LLONG_MAX;
5996 else
5997 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005998nopos:
5999 ret = 0;
6000err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006001 if (put)
6002 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006003 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006004 return ret;
6005}
6006
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006007int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006008{
6009 struct btrfs_root *root = BTRFS_I(inode)->root;
6010 struct btrfs_trans_handle *trans;
6011 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006012 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006013
Josef Bacik72ac3c02012-05-23 14:13:11 -04006014 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006015 return 0;
6016
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006017 if (btrfs_fs_closing(root->fs_info) &&
6018 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006019 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006020
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006021 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006022 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006023 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006024 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006025 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006026 if (IS_ERR(trans))
6027 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006028 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006029 }
6030 return ret;
6031}
6032
6033/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006034 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006035 * inode changes. But, it is most likely to find the inode in cache.
6036 * FIXME, needs more benchmarking...there are no reasons other than performance
6037 * to keep or drop this code.
6038 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006039static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006040{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006041 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006042 struct btrfs_root *root = BTRFS_I(inode)->root;
6043 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006044 int ret;
6045
Josef Bacik72ac3c02012-05-23 14:13:11 -04006046 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006047 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006048
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006049 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006050 if (IS_ERR(trans))
6051 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006052
6053 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006054 if (ret && ret == -ENOSPC) {
6055 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006056 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006057 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006058 if (IS_ERR(trans))
6059 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006060
Chris Mason94b60442010-05-26 11:02:00 -04006061 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006062 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006063 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006064 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006065 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006066
6067 return ret;
6068}
6069
6070/*
6071 * This is a copy of file_update_time. We need this so we can return error on
6072 * ENOSPC for updating the inode in the case of file write and mmap writes.
6073 */
Josef Bacike41f9412012-03-26 09:46:47 -04006074static int btrfs_update_time(struct inode *inode, struct timespec *now,
6075 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006076{
Alexander Block2bc5565282012-06-15 09:49:33 +02006077 struct btrfs_root *root = BTRFS_I(inode)->root;
6078
6079 if (btrfs_root_readonly(root))
6080 return -EROFS;
6081
Josef Bacike41f9412012-03-26 09:46:47 -04006082 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006083 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006084 if (flags & S_CTIME)
6085 inode->i_ctime = *now;
6086 if (flags & S_MTIME)
6087 inode->i_mtime = *now;
6088 if (flags & S_ATIME)
6089 inode->i_atime = *now;
6090 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006091}
6092
Chris Masond352ac62008-09-29 15:18:18 -04006093/*
6094 * find the highest existing sequence number in a directory
6095 * and then set the in-memory index_cnt variable to reflect
6096 * free sequence numbers
6097 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006098static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006099{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006100 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006101 struct btrfs_key key, found_key;
6102 struct btrfs_path *path;
6103 struct extent_buffer *leaf;
6104 int ret;
6105
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006106 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006107 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006108 key.offset = (u64)-1;
6109
6110 path = btrfs_alloc_path();
6111 if (!path)
6112 return -ENOMEM;
6113
6114 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6115 if (ret < 0)
6116 goto out;
6117 /* FIXME: we should be able to handle this */
6118 if (ret == 0)
6119 goto out;
6120 ret = 0;
6121
6122 /*
6123 * MAGIC NUMBER EXPLANATION:
6124 * since we search a directory based on f_pos we have to start at 2
6125 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6126 * else has to start at 2
6127 */
6128 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006129 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006130 goto out;
6131 }
6132
6133 path->slots[0]--;
6134
6135 leaf = path->nodes[0];
6136 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6137
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006138 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006139 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006140 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006141 goto out;
6142 }
6143
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006144 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006145out:
6146 btrfs_free_path(path);
6147 return ret;
6148}
6149
Chris Masond352ac62008-09-29 15:18:18 -04006150/*
6151 * helper to find a free sequence number in a given directory. This current
6152 * code is very simple, later versions will do smarter things in the btree
6153 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006154int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006155{
6156 int ret = 0;
6157
Nikolay Borisov877574e2017-02-20 13:50:33 +02006158 if (dir->index_cnt == (u64)-1) {
6159 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006160 if (ret) {
6161 ret = btrfs_set_inode_index_count(dir);
6162 if (ret)
6163 return ret;
6164 }
Josef Bacikaec74772008-07-24 12:12:38 -04006165 }
6166
Nikolay Borisov877574e2017-02-20 13:50:33 +02006167 *index = dir->index_cnt;
6168 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006169
6170 return ret;
6171}
6172
Chris Masonb0d5d102014-09-08 13:08:51 -07006173static int btrfs_insert_inode_locked(struct inode *inode)
6174{
6175 struct btrfs_iget_args args;
6176 args.location = &BTRFS_I(inode)->location;
6177 args.root = BTRFS_I(inode)->root;
6178
6179 return insert_inode_locked4(inode,
6180 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6181 btrfs_find_actor, &args);
6182}
6183
Chris Mason39279cc2007-06-12 06:35:45 -04006184static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6185 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006186 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006187 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006188 u64 ref_objectid, u64 objectid,
6189 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006190{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006191 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006192 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006193 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006194 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006195 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006196 struct btrfs_inode_ref *ref;
6197 struct btrfs_key key[2];
6198 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006199 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006200 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006201 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006202
Chris Mason5f39d392007-10-15 16:14:19 -04006203 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006204 if (!path)
6205 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006206
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006207 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006208 if (!inode) {
6209 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006210 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006211 }
Chris Mason39279cc2007-06-12 06:35:45 -04006212
Li Zefan581bb052011-04-20 10:06:11 +08006213 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006214 * O_TMPFILE, set link count to 0, so that after this point,
6215 * we fill in an inode item with the correct link count.
6216 */
6217 if (!name)
6218 set_nlink(inode, 0);
6219
6220 /*
Li Zefan581bb052011-04-20 10:06:11 +08006221 * we have to initialize this early, so we can reclaim the inode
6222 * number if we fail afterwards in this function.
6223 */
6224 inode->i_ino = objectid;
6225
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006226 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006227 trace_btrfs_inode_request(dir);
6228
Nikolay Borisov877574e2017-02-20 13:50:33 +02006229 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006230 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006231 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006232 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006233 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006234 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006235 } else if (dir) {
6236 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006237 }
6238 /*
6239 * index_cnt is ignored for everything but a dir,
6240 * btrfs_get_inode_index_count has an explanation for the magic
6241 * number
6242 */
6243 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006244 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006245 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006246 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006247 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006248
Josef Bacik5dc562c2012-08-17 13:14:17 -04006249 /*
6250 * We could have gotten an inode number from somebody who was fsynced
6251 * and then removed in this same transaction, so let's just set full
6252 * sync since it will be a full sync anyway and this will blow away the
6253 * old info in the log.
6254 */
6255 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6256
Chris Mason9c583092008-01-29 15:15:18 -05006257 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006258 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006259 key[0].offset = 0;
6260
Chris Mason9c583092008-01-29 15:15:18 -05006261 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006262
6263 if (name) {
6264 /*
6265 * Start new inodes with an inode_ref. This is slightly more
6266 * efficient for small numbers of hard links since they will
6267 * be packed into one item. Extended refs will kick in if we
6268 * add more hard links than can fit in the ref item.
6269 */
6270 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006271 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006272 key[1].offset = ref_objectid;
6273
6274 sizes[1] = name_len + sizeof(*ref);
6275 }
Chris Mason9c583092008-01-29 15:15:18 -05006276
Chris Masonb0d5d102014-09-08 13:08:51 -07006277 location = &BTRFS_I(inode)->location;
6278 location->objectid = objectid;
6279 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006280 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006281
6282 ret = btrfs_insert_inode_locked(inode);
6283 if (ret < 0)
6284 goto fail;
6285
Chris Masonb9473432009-03-13 11:00:37 -04006286 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006287 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006288 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006289 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006290
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006291 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006292 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306293
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006294 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306295 inode->i_atime = inode->i_mtime;
6296 inode->i_ctime = inode->i_mtime;
6297 BTRFS_I(inode)->i_otime = inode->i_mtime;
6298
Chris Mason5f39d392007-10-15 16:14:19 -04006299 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6300 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006301 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006302 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006303 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006304
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006305 if (name) {
6306 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6307 struct btrfs_inode_ref);
6308 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6309 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6310 ptr = (unsigned long)(ref + 1);
6311 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6312 }
Chris Mason9c583092008-01-29 15:15:18 -05006313
Chris Mason5f39d392007-10-15 16:14:19 -04006314 btrfs_mark_buffer_dirty(path->nodes[0]);
6315 btrfs_free_path(path);
6316
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006317 btrfs_inherit_iflags(inode, dir);
6318
Al Viro569254b2011-07-24 17:08:40 -04006319 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006320 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006321 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006322 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006323 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6324 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006325 }
6326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006327 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006328
6329 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006330 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006331
Alexander Block8ea05e32012-07-25 17:35:53 +02006332 btrfs_update_root_times(trans, root);
6333
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006334 ret = btrfs_inode_inherit_props(trans, inode, dir);
6335 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006336 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006337 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006338 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006339
Chris Mason39279cc2007-06-12 06:35:45 -04006340 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006341
6342fail_unlock:
6343 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006344fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006345 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006346 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006347 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006348 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006349 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006350}
6351
6352static inline u8 btrfs_inode_type(struct inode *inode)
6353{
6354 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6355}
6356
Chris Masond352ac62008-09-29 15:18:18 -04006357/*
6358 * utility function to add 'inode' into 'parent_inode' with
6359 * a give name and a given sequence number.
6360 * if 'add_backref' is true, also insert a backref from the
6361 * inode to the parent directory.
6362 */
Chris Masone02119d2008-09-05 16:13:11 -04006363int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006364 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006365 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006366{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006367 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006368 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006369 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006370 struct btrfs_root *root = parent_inode->root;
6371 u64 ino = btrfs_ino(inode);
6372 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006373
Li Zefan33345d012011-04-20 10:31:50 +08006374 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006375 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006376 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006377 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006378 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006379 key.offset = 0;
6380 }
Chris Mason39279cc2007-06-12 06:35:45 -04006381
Li Zefan33345d012011-04-20 10:31:50 +08006382 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006383 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6384 root->root_key.objectid, parent_ino,
6385 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006386 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006387 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6388 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006389 }
6390
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006391 /* Nothing to clean up yet */
6392 if (ret)
6393 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006394
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006395 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6396 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006397 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006398 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006399 goto fail_dir_item;
6400 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006401 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006402 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006403 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006404
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006405 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006406 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006407 inode_inc_iversion(&parent_inode->vfs_inode);
6408 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6409 current_time(&parent_inode->vfs_inode);
6410 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006411 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006412 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006413 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006414
6415fail_dir_item:
6416 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6417 u64 local_index;
6418 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006419 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6420 root->root_key.objectid, parent_ino,
6421 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006422
6423 } else if (add_backref) {
6424 u64 local_index;
6425 int err;
6426
6427 err = btrfs_del_inode_ref(trans, root, name, name_len,
6428 ino, parent_ino, &local_index);
6429 }
6430 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006431}
6432
6433static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006434 struct btrfs_inode *dir, struct dentry *dentry,
6435 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006436{
Josef Bacika1b075d2010-11-19 20:36:11 +00006437 int err = btrfs_add_link(trans, dir, inode,
6438 dentry->d_name.name, dentry->d_name.len,
6439 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006440 if (err > 0)
6441 err = -EEXIST;
6442 return err;
6443}
6444
Josef Bacik618e21d2007-07-11 10:18:17 -04006445static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006446 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006447{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006448 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006449 struct btrfs_trans_handle *trans;
6450 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006451 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006452 int err;
6453 int drop_inode = 0;
6454 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006455 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006456
Josef Bacik9ed74f22009-09-11 16:12:44 -04006457 /*
6458 * 2 for inode item and ref
6459 * 2 for dir items
6460 * 1 for xattr if selinux is on
6461 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006462 trans = btrfs_start_transaction(root, 5);
6463 if (IS_ERR(trans))
6464 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006465
Li Zefan581bb052011-04-20 10:06:11 +08006466 err = btrfs_find_free_ino(root, &objectid);
6467 if (err)
6468 goto out_unlock;
6469
Josef Bacikaec74772008-07-24 12:12:38 -04006470 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006471 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6472 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006473 if (IS_ERR(inode)) {
6474 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006475 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006476 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006477
Casey Schauflerad19db72011-12-15 10:09:07 -05006478 /*
6479 * If the active LSM wants to access the inode during
6480 * d_instantiate it needs these. Smack checks to see
6481 * if the filesystem supports xattrs by looking at the
6482 * ops vector.
6483 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006484 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006485 init_special_inode(inode, inode->i_mode, rdev);
6486
6487 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006488 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006489 goto out_unlock_inode;
6490
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006491 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6492 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006493 if (err) {
6494 goto out_unlock_inode;
6495 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006496 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006497 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006498 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006499 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006500
Josef Bacik618e21d2007-07-11 10:18:17 -04006501out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006502 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006503 btrfs_balance_delayed_items(fs_info);
6504 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006505 if (drop_inode) {
6506 inode_dec_link_count(inode);
6507 iput(inode);
6508 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006509 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006510
6511out_unlock_inode:
6512 drop_inode = 1;
6513 unlock_new_inode(inode);
6514 goto out_unlock;
6515
Josef Bacik618e21d2007-07-11 10:18:17 -04006516}
6517
Chris Mason39279cc2007-06-12 06:35:45 -04006518static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006519 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006520{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006521 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006522 struct btrfs_trans_handle *trans;
6523 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006524 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006525 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006526 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006527 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006528 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006529
Josef Bacik9ed74f22009-09-11 16:12:44 -04006530 /*
6531 * 2 for inode item and ref
6532 * 2 for dir items
6533 * 1 for xattr if selinux is on
6534 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006535 trans = btrfs_start_transaction(root, 5);
6536 if (IS_ERR(trans))
6537 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006538
Li Zefan581bb052011-04-20 10:06:11 +08006539 err = btrfs_find_free_ino(root, &objectid);
6540 if (err)
6541 goto out_unlock;
6542
Josef Bacikaec74772008-07-24 12:12:38 -04006543 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006544 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6545 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006546 if (IS_ERR(inode)) {
6547 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006548 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006549 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006550 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006551 /*
6552 * If the active LSM wants to access the inode during
6553 * d_instantiate it needs these. Smack checks to see
6554 * if the filesystem supports xattrs by looking at the
6555 * ops vector.
6556 */
6557 inode->i_fop = &btrfs_file_operations;
6558 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006559 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006560
6561 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6562 if (err)
6563 goto out_unlock_inode;
6564
6565 err = btrfs_update_inode(trans, root, inode);
6566 if (err)
6567 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006568
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006569 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6570 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006571 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006572 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006573
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006574 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006575 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006576 d_instantiate(dentry, inode);
6577
Chris Mason39279cc2007-06-12 06:35:45 -04006578out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006579 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006580 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006581 inode_dec_link_count(inode);
6582 iput(inode);
6583 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006584 btrfs_balance_delayed_items(fs_info);
6585 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006586 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006587
6588out_unlock_inode:
6589 unlock_new_inode(inode);
6590 goto out_unlock;
6591
Chris Mason39279cc2007-06-12 06:35:45 -04006592}
6593
6594static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6595 struct dentry *dentry)
6596{
Filipe Manana271dba452016-01-05 16:24:05 +00006597 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006598 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006599 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006600 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006601 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006602 int err;
6603 int drop_inode = 0;
6604
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006605 /* do not allow sys_link's with other subvols of the same device */
6606 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006607 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006608
Mark Fashehf1863732012-08-08 11:32:27 -07006609 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006610 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006611
Nikolay Borisov877574e2017-02-20 13:50:33 +02006612 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006613 if (err)
6614 goto fail;
6615
Yan, Zhenga22285a2010-05-16 10:48:46 -04006616 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006617 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006618 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006619 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006620 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006621 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006622 if (IS_ERR(trans)) {
6623 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006624 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006625 goto fail;
6626 }
Chris Mason5f39d392007-10-15 16:14:19 -04006627
Miao Xie67de1172013-12-26 13:07:06 +08006628 /* There are several dir indexes for this inode, clear the cache. */
6629 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006630 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006631 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006632 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006633 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006634 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006635
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006636 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6637 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006638
Yan, Zhenga5719522009-09-24 09:17:31 -04006639 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006640 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006641 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006642 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006643 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006644 if (err)
6645 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006646 if (inode->i_nlink == 1) {
6647 /*
6648 * If new hard link count is 1, it's a file created
6649 * with open(2) O_TMPFILE flag.
6650 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006651 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006652 if (err)
6653 goto fail;
6654 }
Al Viro08c422c2011-12-23 07:58:13 -05006655 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006656 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006657 }
Chris Mason39279cc2007-06-12 06:35:45 -04006658
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006659 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006660fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006661 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006662 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006663 if (drop_inode) {
6664 inode_dec_link_count(inode);
6665 iput(inode);
6666 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006667 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 return err;
6669}
6670
Al Viro18bb1db2011-07-26 01:41:39 -04006671static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006672{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006673 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006674 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006675 struct btrfs_trans_handle *trans;
6676 struct btrfs_root *root = BTRFS_I(dir)->root;
6677 int err = 0;
6678 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006679 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006680 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006681
Josef Bacik9ed74f22009-09-11 16:12:44 -04006682 /*
6683 * 2 items for inode and ref
6684 * 2 items for dir items
6685 * 1 for xattr if selinux is on
6686 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006687 trans = btrfs_start_transaction(root, 5);
6688 if (IS_ERR(trans))
6689 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006690
Li Zefan581bb052011-04-20 10:06:11 +08006691 err = btrfs_find_free_ino(root, &objectid);
6692 if (err)
6693 goto out_fail;
6694
Josef Bacikaec74772008-07-24 12:12:38 -04006695 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006696 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6697 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006698 if (IS_ERR(inode)) {
6699 err = PTR_ERR(inode);
6700 goto out_fail;
6701 }
Chris Mason5f39d392007-10-15 16:14:19 -04006702
Chris Mason39279cc2007-06-12 06:35:45 -04006703 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006704 /* these must be set before we unlock the inode */
6705 inode->i_op = &btrfs_dir_inode_operations;
6706 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006707
Eric Paris2a7dba32011-02-01 11:05:39 -05006708 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006709 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006710 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006711
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006712 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006713 err = btrfs_update_inode(trans, root, inode);
6714 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006715 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006716
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006717 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6718 dentry->d_name.name,
6719 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006720 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006721 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006722
Chris Mason39279cc2007-06-12 06:35:45 -04006723 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006724 /*
6725 * mkdir is special. We're unlocking after we call d_instantiate
6726 * to avoid a race with nfsd calling d_instantiate.
6727 */
6728 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006729 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006730
6731out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006732 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006733 if (drop_on_err) {
6734 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006735 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006736 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006737 btrfs_balance_delayed_items(fs_info);
6738 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006740
6741out_fail_inode:
6742 unlock_new_inode(inode);
6743 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006744}
6745
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006746/* Find next extent map of a given extent map, caller needs to ensure locks */
6747static struct extent_map *next_extent_map(struct extent_map *em)
6748{
6749 struct rb_node *next;
6750
6751 next = rb_next(&em->rb_node);
6752 if (!next)
6753 return NULL;
6754 return container_of(next, struct extent_map, rb_node);
6755}
6756
6757static struct extent_map *prev_extent_map(struct extent_map *em)
6758{
6759 struct rb_node *prev;
6760
6761 prev = rb_prev(&em->rb_node);
6762 if (!prev)
6763 return NULL;
6764 return container_of(prev, struct extent_map, rb_node);
6765}
6766
Chris Masond352ac62008-09-29 15:18:18 -04006767/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006768 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006769 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006770 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006771 */
Chris Mason3b951512008-04-17 11:29:12 -04006772static int merge_extent_mapping(struct extent_map_tree *em_tree,
6773 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006774 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006775 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006776{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006777 struct extent_map *prev;
6778 struct extent_map *next;
6779 u64 start;
6780 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006781 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006782
Chris Masone6dcd2d2008-07-17 12:53:50 -04006783 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006784
6785 if (existing->start > map_start) {
6786 next = existing;
6787 prev = prev_extent_map(next);
6788 } else {
6789 prev = existing;
6790 next = next_extent_map(prev);
6791 }
6792
6793 start = prev ? extent_map_end(prev) : em->start;
6794 start = max_t(u64, start, em->start);
6795 end = next ? next->start : extent_map_end(em);
6796 end = min_t(u64, end, extent_map_end(em));
6797 start_diff = start - em->start;
6798 em->start = start;
6799 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006800 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6801 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006802 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006803 em->block_len -= start_diff;
6804 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006805 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006806}
6807
Chris Masonc8b97812008-10-29 14:49:59 -04006808static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006809 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006810 size_t pg_offset, u64 extent_offset,
6811 struct btrfs_file_extent_item *item)
6812{
6813 int ret;
6814 struct extent_buffer *leaf = path->nodes[0];
6815 char *tmp;
6816 size_t max_size;
6817 unsigned long inline_size;
6818 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006819 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006820
6821 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006822 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006823 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6824 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006825 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006826 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006827 if (!tmp)
6828 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006829 ptr = btrfs_file_extent_inline_start(item);
6830
6831 read_extent_buffer(leaf, tmp, ptr, inline_size);
6832
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006833 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006834 ret = btrfs_decompress(compress_type, tmp, page,
6835 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006836
6837 /*
6838 * decompression code contains a memset to fill in any space between the end
6839 * of the uncompressed data and the end of max_size in case the decompressed
6840 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6841 * the end of an inline extent and the beginning of the next block, so we
6842 * cover that region here.
6843 */
6844
6845 if (max_size + pg_offset < PAGE_SIZE) {
6846 char *map = kmap(page);
6847 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6848 kunmap(page);
6849 }
Chris Masonc8b97812008-10-29 14:49:59 -04006850 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006851 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006852}
6853
Chris Masond352ac62008-09-29 15:18:18 -04006854/*
6855 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006856 * the ugly parts come from merging extents from the disk with the in-ram
6857 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006858 * where the in-ram extents might be locked pending data=ordered completion.
6859 *
6860 * This also copies inline extents directly into the page.
6861 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006862struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6863 struct page *page,
6864 size_t pg_offset, u64 start, u64 len,
6865 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006866{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006868 int ret;
6869 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006870 u64 extent_start = 0;
6871 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006872 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006873 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006874 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006875 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006876 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006877 struct extent_buffer *leaf;
6878 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006880 struct extent_map_tree *em_tree = &inode->extent_tree;
6881 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006882 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006883 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006884
Chris Masona52d9a82007-08-27 16:49:44 -04006885again:
Chris Mason890871b2009-09-02 16:24:52 -04006886 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006887 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006888 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006889 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006890 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006891
Chris Masona52d9a82007-08-27 16:49:44 -04006892 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006893 if (em->start > start || em->start + em->len <= start)
6894 free_extent_map(em);
6895 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006896 free_extent_map(em);
6897 else
6898 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006899 }
David Sterba172ddd62011-04-21 00:48:27 +02006900 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006901 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006902 err = -ENOMEM;
6903 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006904 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006905 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006906 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006907 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006908 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006909 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006910
6911 if (!path) {
6912 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006913 if (!path) {
6914 err = -ENOMEM;
6915 goto out;
6916 }
6917 /*
6918 * Chances are we'll be called again, so go ahead and do
6919 * readahead
6920 */
David Sterbae4058b52015-11-27 16:31:35 +01006921 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006922 }
6923
Chris Mason179e29e2007-11-01 11:28:41 -04006924 ret = btrfs_lookup_file_extent(trans, root, path,
6925 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006926 if (ret < 0) {
6927 err = ret;
6928 goto out;
6929 }
6930
6931 if (ret != 0) {
6932 if (path->slots[0] == 0)
6933 goto not_found;
6934 path->slots[0]--;
6935 }
6936
Chris Mason5f39d392007-10-15 16:14:19 -04006937 leaf = path->nodes[0];
6938 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006939 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006940 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006941 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006942 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006943 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006944 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006945 /*
6946 * If we backup past the first extent we want to move forward
6947 * and see if there is an extent in front of us, otherwise we'll
6948 * say there is a hole for our whole search range which can
6949 * cause problems.
6950 */
6951 extent_end = start;
6952 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006953 }
6954
Chris Mason5f39d392007-10-15 16:14:19 -04006955 found_type = btrfs_file_extent_type(leaf, item);
6956 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006957 if (found_type == BTRFS_FILE_EXTENT_REG ||
6958 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006959 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006960 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006961
6962 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6963 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006964 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6965 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006966 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006967 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006968 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006969
6970 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6971 path->slots[0],
6972 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006973 }
Josef Bacik25a50342013-10-14 12:08:38 -04006974next:
Yan Zheng9036c102008-10-30 14:19:41 -04006975 if (start >= extent_end) {
6976 path->slots[0]++;
6977 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6978 ret = btrfs_next_leaf(root, path);
6979 if (ret < 0) {
6980 err = ret;
6981 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006982 }
Yan Zheng9036c102008-10-30 14:19:41 -04006983 if (ret > 0)
6984 goto not_found;
6985 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006986 }
Yan Zheng9036c102008-10-30 14:19:41 -04006987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6988 if (found_key.objectid != objectid ||
6989 found_key.type != BTRFS_EXTENT_DATA_KEY)
6990 goto not_found;
6991 if (start + len <= found_key.offset)
6992 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006993 if (start > found_key.offset)
6994 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006995 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006996 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006997 em->len = found_key.offset - start;
6998 goto not_found_em;
6999 }
7000
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007001 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007002 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007003
Yan Zhengd899e052008-10-30 14:25:28 -04007004 if (found_type == BTRFS_FILE_EXTENT_REG ||
7005 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007006 goto insert;
7007 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007008 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007009 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007010 size_t size;
7011 size_t extent_offset;
7012 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007013
Filipe Manana7ffbb592014-06-09 03:48:05 +01007014 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007015 goto out;
Yan689f9342007-10-29 11:41:07 -04007016
Chris Mason514ac8a2014-01-03 21:07:00 -08007017 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007018 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007019 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7020 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007021 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007022 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007023 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007024 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007025 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007026 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007027 if (btrfs_file_extent_compression(leaf, item) !=
7028 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007029 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007030 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007031 if (ret) {
7032 err = ret;
7033 goto out;
7034 }
Chris Masonc8b97812008-10-29 14:49:59 -04007035 } else {
7036 map = kmap(page);
7037 read_extent_buffer(leaf, map + pg_offset, ptr,
7038 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007039 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007040 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007041 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007042 copy_size);
7043 }
Chris Masonc8b97812008-10-29 14:49:59 -04007044 kunmap(page);
7045 }
Chris Mason179e29e2007-11-01 11:28:41 -04007046 flush_dcache_page(page);
7047 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007048 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007049 if (!trans) {
7050 kunmap(page);
7051 free_extent_map(em);
7052 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007053
David Sterbab3b4aa72011-04-21 01:20:15 +02007054 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007055 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007056
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007057 if (IS_ERR(trans))
7058 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007059 goto again;
7060 }
Chris Masonc8b97812008-10-29 14:49:59 -04007061 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007062 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007063 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007064 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007065 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
Chris Masond1310b22008-01-24 16:13:08 -05007067 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007068 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007069 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007070 }
7071not_found:
7072 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007073 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007074 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007075not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007076 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007077 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007078insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007079 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007080 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007081 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007082 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7083 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007084 err = -EIO;
7085 goto out;
7086 }
Chris Masond1310b22008-01-24 16:13:08 -05007087
7088 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007089 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007090 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007091 /* it is possible that someone inserted the extent into the tree
7092 * while we had the lock dropped. It is also possible that
7093 * an overlapping map exists in the tree
7094 */
Chris Masona52d9a82007-08-27 16:49:44 -04007095 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007096 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007097
7098 ret = 0;
7099
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007100 existing = search_extent_mapping(em_tree, start, len);
7101 /*
7102 * existing will always be non-NULL, since there must be
7103 * extent causing the -EEXIST.
7104 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007105 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007106 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007107 em->block_start == existing->block_start) {
7108 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007109 * The existing extent map already encompasses the
7110 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007111 */
7112 free_extent_map(em);
7113 em = existing;
7114 err = 0;
7115
7116 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007117 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007118 /*
7119 * The existing extent map is the one nearest to
7120 * the [start, start + len) range which overlaps
7121 */
7122 err = merge_extent_mapping(em_tree, existing,
7123 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007124 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007125 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007126 free_extent_map(em);
7127 em = NULL;
7128 }
7129 } else {
7130 free_extent_map(em);
7131 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007132 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007133 }
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
Chris Mason890871b2009-09-02 16:24:52 -04007135 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007136out:
liubo1abe9b82011-03-24 11:18:59 +00007137
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007138 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007139
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007140 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007141 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007142 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007143 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007144 err = ret;
7145 }
Chris Masona52d9a82007-08-27 16:49:44 -04007146 if (err) {
7147 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007148 return ERR_PTR(err);
7149 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007150 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007151 return em;
7152}
7153
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007154struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7155 struct page *page,
7156 size_t pg_offset, u64 start, u64 len,
7157 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007158{
7159 struct extent_map *em;
7160 struct extent_map *hole_em = NULL;
7161 u64 range_start = start;
7162 u64 end;
7163 u64 found;
7164 u64 found_end;
7165 int err = 0;
7166
7167 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7168 if (IS_ERR(em))
7169 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007170 /*
7171 * If our em maps to:
7172 * - a hole or
7173 * - a pre-alloc extent,
7174 * there might actually be delalloc bytes behind it.
7175 */
7176 if (em->block_start != EXTENT_MAP_HOLE &&
7177 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7178 return em;
7179 else
7180 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007181
7182 /* check to see if we've wrapped (len == -1 or similar) */
7183 end = start + len;
7184 if (end < start)
7185 end = (u64)-1;
7186 else
7187 end -= 1;
7188
7189 em = NULL;
7190
7191 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007192 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007193 end, len, EXTENT_DELALLOC, 1);
7194 found_end = range_start + found;
7195 if (found_end < range_start)
7196 found_end = (u64)-1;
7197
7198 /*
7199 * we didn't find anything useful, return
7200 * the original results from get_extent()
7201 */
7202 if (range_start > end || found_end <= start) {
7203 em = hole_em;
7204 hole_em = NULL;
7205 goto out;
7206 }
7207
7208 /* adjust the range_start to make sure it doesn't
7209 * go backwards from the start they passed in
7210 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307211 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007212 found = found_end - range_start;
7213
7214 if (found > 0) {
7215 u64 hole_start = start;
7216 u64 hole_len = len;
7217
David Sterba172ddd62011-04-21 00:48:27 +02007218 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007219 if (!em) {
7220 err = -ENOMEM;
7221 goto out;
7222 }
7223 /*
7224 * when btrfs_get_extent can't find anything it
7225 * returns one huge hole
7226 *
7227 * make sure what it found really fits our range, and
7228 * adjust to make sure it is based on the start from
7229 * the caller
7230 */
7231 if (hole_em) {
7232 u64 calc_end = extent_map_end(hole_em);
7233
7234 if (calc_end <= start || (hole_em->start > end)) {
7235 free_extent_map(hole_em);
7236 hole_em = NULL;
7237 } else {
7238 hole_start = max(hole_em->start, start);
7239 hole_len = calc_end - hole_start;
7240 }
7241 }
7242 em->bdev = NULL;
7243 if (hole_em && range_start > hole_start) {
7244 /* our hole starts before our delalloc, so we
7245 * have to return just the parts of the hole
7246 * that go until the delalloc starts
7247 */
7248 em->len = min(hole_len,
7249 range_start - hole_start);
7250 em->start = hole_start;
7251 em->orig_start = hole_start;
7252 /*
7253 * don't adjust block start at all,
7254 * it is fixed at EXTENT_MAP_HOLE
7255 */
7256 em->block_start = hole_em->block_start;
7257 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007258 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7259 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007260 } else {
7261 em->start = range_start;
7262 em->len = found;
7263 em->orig_start = range_start;
7264 em->block_start = EXTENT_MAP_DELALLOC;
7265 em->block_len = found;
7266 }
7267 } else if (hole_em) {
7268 return hole_em;
7269 }
7270out:
7271
7272 free_extent_map(hole_em);
7273 if (err) {
7274 free_extent_map(em);
7275 return ERR_PTR(err);
7276 }
7277 return em;
7278}
7279
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007280static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7281 const u64 start,
7282 const u64 len,
7283 const u64 orig_start,
7284 const u64 block_start,
7285 const u64 block_len,
7286 const u64 orig_block_len,
7287 const u64 ram_bytes,
7288 const int type)
7289{
7290 struct extent_map *em = NULL;
7291 int ret;
7292
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007293 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007294 em = create_io_em(inode, start, len, orig_start,
7295 block_start, block_len, orig_block_len,
7296 ram_bytes,
7297 BTRFS_COMPRESS_NONE, /* compress_type */
7298 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007299 if (IS_ERR(em))
7300 goto out;
7301 }
7302 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7303 len, block_len, type);
7304 if (ret) {
7305 if (em) {
7306 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007307 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007308 start + len - 1, 0);
7309 }
7310 em = ERR_PTR(ret);
7311 }
7312 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007313
7314 return em;
7315}
7316
Josef Bacik4b46fce2010-05-23 11:00:55 -04007317static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7318 u64 start, u64 len)
7319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007322 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 struct btrfs_key ins;
7324 u64 alloc_hint;
7325 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326
Josef Bacik4b46fce2010-05-23 11:00:55 -04007327 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007328 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007329 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007330 if (ret)
7331 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007332
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007333 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7334 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007335 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007336 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007337 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007338 btrfs_free_reserved_extent(fs_info, ins.objectid,
7339 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007340
Josef Bacik4b46fce2010-05-23 11:00:55 -04007341 return em;
7342}
7343
Chris Mason46bfbb52010-05-26 11:04:10 -04007344/*
7345 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7346 * block must be cow'd
7347 */
Josef Bacik00361582013-08-14 14:02:47 -04007348noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007349 u64 *orig_start, u64 *orig_block_len,
7350 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007351{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007353 struct btrfs_path *path;
7354 int ret;
7355 struct extent_buffer *leaf;
7356 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007357 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007358 struct btrfs_file_extent_item *fi;
7359 struct btrfs_key key;
7360 u64 disk_bytenr;
7361 u64 backref_offset;
7362 u64 extent_end;
7363 u64 num_bytes;
7364 int slot;
7365 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007366 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007367
Chris Mason46bfbb52010-05-26 11:04:10 -04007368 path = btrfs_alloc_path();
7369 if (!path)
7370 return -ENOMEM;
7371
David Sterbaf85b7372017-01-20 14:54:07 +01007372 ret = btrfs_lookup_file_extent(NULL, root, path,
7373 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007374 if (ret < 0)
7375 goto out;
7376
7377 slot = path->slots[0];
7378 if (ret == 1) {
7379 if (slot == 0) {
7380 /* can't find the item, must cow */
7381 ret = 0;
7382 goto out;
7383 }
7384 slot--;
7385 }
7386 ret = 0;
7387 leaf = path->nodes[0];
7388 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007389 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 key.type != BTRFS_EXTENT_DATA_KEY) {
7391 /* not our file or wrong item type, must cow */
7392 goto out;
7393 }
7394
7395 if (key.offset > offset) {
7396 /* Wrong offset, must cow */
7397 goto out;
7398 }
7399
7400 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7401 found_type = btrfs_file_extent_type(leaf, fi);
7402 if (found_type != BTRFS_FILE_EXTENT_REG &&
7403 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7404 /* not a regular extent, must cow */
7405 goto out;
7406 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007407
7408 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7409 goto out;
7410
Miao Xiee77751a2013-12-27 21:11:50 +08007411 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7412 if (extent_end <= offset)
7413 goto out;
7414
Chris Mason46bfbb52010-05-26 11:04:10 -04007415 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007416 if (disk_bytenr == 0)
7417 goto out;
7418
7419 if (btrfs_file_extent_compression(leaf, fi) ||
7420 btrfs_file_extent_encryption(leaf, fi) ||
7421 btrfs_file_extent_other_encoding(leaf, fi))
7422 goto out;
7423
Chris Mason46bfbb52010-05-26 11:04:10 -04007424 backref_offset = btrfs_file_extent_offset(leaf, fi);
7425
Josef Bacik7ee9e442013-06-21 16:37:03 -04007426 if (orig_start) {
7427 *orig_start = key.offset - backref_offset;
7428 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7429 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7430 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007431
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007432 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007433 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007434
7435 num_bytes = min(offset + *len, extent_end) - offset;
7436 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7437 u64 range_end;
7438
Jeff Mahoneyda170662016-06-15 09:22:56 -04007439 range_end = round_up(offset + num_bytes,
7440 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007441 ret = test_range_bit(io_tree, offset, range_end,
7442 EXTENT_DELALLOC, 0, NULL);
7443 if (ret) {
7444 ret = -EAGAIN;
7445 goto out;
7446 }
7447 }
7448
Josef Bacik1bda19e2013-10-18 12:10:36 -04007449 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007450
7451 /*
7452 * look for other files referencing this extent, if we
7453 * find any we must cow
7454 */
Josef Bacik00361582013-08-14 14:02:47 -04007455
Liu Boe4c3b2d2017-01-30 12:25:28 -08007456 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007457 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007458 if (ret) {
7459 ret = 0;
7460 goto out;
7461 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007462
7463 /*
7464 * adjust disk_bytenr and num_bytes to cover just the bytes
7465 * in this extent we are about to write. If there
7466 * are any csums in that range we have to cow in order
7467 * to keep the csums correct
7468 */
7469 disk_bytenr += backref_offset;
7470 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007471 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7472 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007473 /*
7474 * all of the above have passed, it is safe to overwrite this extent
7475 * without cow
7476 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007477 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007478 ret = 1;
7479out:
7480 btrfs_free_path(path);
7481 return ret;
7482}
7483
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007484bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7485{
7486 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007487 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007488 void **pagep = NULL;
7489 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007490 unsigned long start_idx;
7491 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007492
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007493 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007494
7495 /*
7496 * end is the last byte in the last page. end == start is legal
7497 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007498 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007499
7500 rcu_read_lock();
7501
7502 /* Most of the code in this while loop is lifted from
7503 * find_get_page. It's been modified to begin searching from a
7504 * page and return just the first page found in that range. If the
7505 * found idx is less than or equal to the end idx then we know that
7506 * a page exists. If no pages are found or if those pages are
7507 * outside of the range then we're fine (yay!) */
7508 while (page == NULL &&
7509 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7510 page = radix_tree_deref_slot(pagep);
7511 if (unlikely(!page))
7512 break;
7513
7514 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007515 if (radix_tree_deref_retry(page)) {
7516 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007517 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007518 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007519 /*
7520 * Otherwise, shmem/tmpfs must be storing a swap entry
7521 * here as an exceptional entry: so return it without
7522 * attempting to raise page count.
7523 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007524 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007525 break; /* TODO: Is this relevant for this use case? */
7526 }
7527
Filipe Manana91405152014-06-05 13:22:24 +01007528 if (!page_cache_get_speculative(page)) {
7529 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007530 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007531 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007532
7533 /*
7534 * Has the page moved?
7535 * This is part of the lockless pagecache protocol. See
7536 * include/linux/pagemap.h for details.
7537 */
7538 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007539 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007540 page = NULL;
7541 }
7542 }
7543
7544 if (page) {
7545 if (page->index <= end_idx)
7546 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007547 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007548 }
7549
7550 rcu_read_unlock();
7551 return found;
7552}
7553
Josef Bacikeb838e72012-07-31 16:28:48 -04007554static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7555 struct extent_state **cached_state, int writing)
7556{
7557 struct btrfs_ordered_extent *ordered;
7558 int ret = 0;
7559
7560 while (1) {
7561 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007562 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007563 /*
7564 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007565 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 * extents in this range.
7567 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007568 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007569 lockend - lockstart + 1);
7570
7571 /*
7572 * We need to make sure there are no buffered pages in this
7573 * range either, we could have raced between the invalidate in
7574 * generic_file_direct_write and locking the extent. The
7575 * invalidate needs to happen so that reads after a write do not
7576 * get stale data.
7577 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007578 if (!ordered &&
7579 (!writing ||
7580 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007581 break;
7582
7583 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7584 cached_state, GFP_NOFS);
7585
7586 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007587 /*
7588 * If we are doing a DIO read and the ordered extent we
7589 * found is for a buffered write, we can not wait for it
7590 * to complete and retry, because if we do so we can
7591 * deadlock with concurrent buffered writes on page
7592 * locks. This happens only if our DIO read covers more
7593 * than one extent map, if at this point has already
7594 * created an ordered extent for a previous extent map
7595 * and locked its range in the inode's io tree, and a
7596 * concurrent write against that previous extent map's
7597 * range and this range started (we unlock the ranges
7598 * in the io tree only when the bios complete and
7599 * buffered writes always lock pages before attempting
7600 * to lock range in the io tree).
7601 */
7602 if (writing ||
7603 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7604 btrfs_start_ordered_extent(inode, ordered, 1);
7605 else
7606 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 btrfs_put_ordered_extent(ordered);
7608 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007610 * We could trigger writeback for this range (and wait
7611 * for it to complete) and then invalidate the pages for
7612 * this range (through invalidate_inode_pages2_range()),
7613 * but that can lead us to a deadlock with a concurrent
7614 * call to readpages() (a buffered read or a defrag call
7615 * triggered a readahead) on a page lock due to an
7616 * ordered dio extent we created before but did not have
7617 * yet a corresponding bio submitted (whence it can not
7618 * complete), which makes readpages() wait for that
7619 * ordered extent to complete while holding a lock on
7620 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007621 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007622 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 }
7624
Filipe Mananaade77022016-02-18 14:28:55 +00007625 if (ret)
7626 break;
7627
Josef Bacikeb838e72012-07-31 16:28:48 -04007628 cond_resched();
7629 }
7630
7631 return ret;
7632}
7633
Liu Bo6f9994d2017-01-31 07:50:22 -08007634/* The callers of this must take lock_extent() */
7635static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7636 u64 orig_start, u64 block_start,
7637 u64 block_len, u64 orig_block_len,
7638 u64 ram_bytes, int compress_type,
7639 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007640{
7641 struct extent_map_tree *em_tree;
7642 struct extent_map *em;
7643 struct btrfs_root *root = BTRFS_I(inode)->root;
7644 int ret;
7645
Liu Bo6f9994d2017-01-31 07:50:22 -08007646 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7647 type == BTRFS_ORDERED_COMPRESSED ||
7648 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007649 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007650
Josef Bacik69ffb542012-09-11 15:40:07 -04007651 em_tree = &BTRFS_I(inode)->extent_tree;
7652 em = alloc_extent_map();
7653 if (!em)
7654 return ERR_PTR(-ENOMEM);
7655
7656 em->start = start;
7657 em->orig_start = orig_start;
7658 em->len = len;
7659 em->block_len = block_len;
7660 em->block_start = block_start;
7661 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007662 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007663 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007664 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007665 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007666 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007667 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007668 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007669 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7670 em->compress_type = compress_type;
7671 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007672
7673 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007674 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007675 em->start + em->len - 1, 0);
7676 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007677 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007678 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007679 /*
7680 * The caller has taken lock_extent(), who could race with us
7681 * to add em?
7682 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007683 } while (ret == -EEXIST);
7684
7685 if (ret) {
7686 free_extent_map(em);
7687 return ERR_PTR(ret);
7688 }
7689
Liu Bo6f9994d2017-01-31 07:50:22 -08007690 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007691 return em;
7692}
7693
Filipe Manana9c9464c2015-11-04 09:52:04 +00007694static void adjust_dio_outstanding_extents(struct inode *inode,
7695 struct btrfs_dio_data *dio_data,
7696 const u64 len)
7697{
David Sterba823bb202017-01-04 11:09:51 +01007698 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007699
Filipe Manana9c9464c2015-11-04 09:52:04 +00007700 /*
7701 * If we have an outstanding_extents count still set then we're
7702 * within our reservation, otherwise we need to adjust our inode
7703 * counter appropriately.
7704 */
Liu Boc2931662016-12-22 17:13:54 -08007705 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007706 dio_data->outstanding_extents -= num_extents;
7707 } else {
Liu Boc2931662016-12-22 17:13:54 -08007708 /*
7709 * If dio write length has been split due to no large enough
7710 * contiguous space, we need to compensate our inode counter
7711 * appropriately.
7712 */
7713 u64 num_needed = num_extents - dio_data->outstanding_extents;
7714
Filipe Manana9c9464c2015-11-04 09:52:04 +00007715 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007716 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007717 spin_unlock(&BTRFS_I(inode)->lock);
7718 }
7719}
7720
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7722 struct buffer_head *bh_result, int create)
7723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307727 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007729 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007732 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007733
Miao Xie172a5042013-02-21 02:48:22 -07007734 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007735 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007736 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007737 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007738
Josef Bacikc3298612012-08-03 16:49:19 -04007739 lockstart = start;
7740 lockend = start + len - 1;
7741
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007742 if (current->journal_info) {
7743 /*
7744 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007745 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007746 * confused.
7747 */
chandan50745b02015-08-28 21:10:13 +05307748 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007749 current->journal_info = NULL;
7750 }
7751
Josef Bacikeb838e72012-07-31 16:28:48 -04007752 /*
7753 * If this errors out it's because we couldn't invalidate pagecache for
7754 * this range and we need to fallback to buffered.
7755 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007756 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7757 create)) {
7758 ret = -ENOTBLK;
7759 goto err;
7760 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007761
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007762 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007763 if (IS_ERR(em)) {
7764 ret = PTR_ERR(em);
7765 goto unlock_err;
7766 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767
7768 /*
7769 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7770 * io. INLINE is special, and we could probably kludge it in here, but
7771 * it's still buffered so for safety lets just fall back to the generic
7772 * buffered path.
7773 *
7774 * For COMPRESSED we _have_ to read the entire extent in so we can
7775 * decompress it, so there will be buffering required no matter what we
7776 * do, so go ahead and fallback to buffered.
7777 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007778 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779 * to buffered IO. Don't blame me, this is the price we pay for using
7780 * the generic code.
7781 */
7782 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7783 em->block_start == EXTENT_MAP_INLINE) {
7784 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007785 ret = -ENOTBLK;
7786 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007787 }
7788
7789 /* Just a good old fashioned hole, return */
7790 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7791 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7792 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007793 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007794 }
7795
7796 /*
7797 * We don't allocate a new extent in the following cases
7798 *
7799 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7800 * existing extent.
7801 * 2) The extent is marked as PREALLOC. We're good to go here and can
7802 * just use the extent.
7803 *
7804 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007805 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007806 len = min(len, em->len - (start - em->start));
7807 lockstart = start + len;
7808 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007809 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007810
7811 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7812 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7813 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007815 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816
7817 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7818 type = BTRFS_ORDERED_PREALLOC;
7819 else
7820 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007821 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007823
Josef Bacik00361582013-08-14 14:02:47 -04007824 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007825 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007826 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007827 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007828
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007829 em2 = btrfs_create_dio_extent(inode, start, len,
7830 orig_start, block_start,
7831 len, orig_block_len,
7832 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007833 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007834 if (type == BTRFS_ORDERED_PREALLOC) {
7835 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007836 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007837 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007838 if (em2 && IS_ERR(em2)) {
7839 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007840 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007841 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007842 /*
7843 * For inode marked NODATACOW or extent marked PREALLOC,
7844 * use the existing or preallocated extent, so does not
7845 * need to adjust btrfs_space_info's bytes_may_use.
7846 */
7847 btrfs_free_reserved_data_space_noquota(inode,
7848 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007849 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007850 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851 }
Josef Bacik00361582013-08-14 14:02:47 -04007852
Chris Mason46bfbb52010-05-26 11:04:10 -04007853 /*
7854 * this will cow the extent, reset the len in case we changed
7855 * it above
7856 */
7857 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007858 free_extent_map(em);
7859 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007860 if (IS_ERR(em)) {
7861 ret = PTR_ERR(em);
7862 goto unlock_err;
7863 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007864 len = min(len, em->len - (start - em->start));
7865unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007866 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7867 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007868 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007869 bh_result->b_bdev = em->bdev;
7870 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007871 if (create) {
7872 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7873 set_buffer_new(bh_result);
7874
7875 /*
7876 * Need to update the i_size under the extent lock so buffered
7877 * readers will get the updated i_size when we unlock.
7878 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007879 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007880 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007881
Filipe Manana9c9464c2015-11-04 09:52:04 +00007882 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307883 WARN_ON(dio_data->reserve < len);
7884 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007885 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307886 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007887 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888
Josef Bacikeb838e72012-07-31 16:28:48 -04007889 /*
7890 * In the case of write we need to clear and unlock the entire range,
7891 * in the case of read we need to unlock only the end area that we
7892 * aren't using if there is any left over space.
7893 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007894 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007895 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7896 lockend, unlock_bits, 1, 0,
7897 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007898 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007899 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007900 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007901
Josef Bacik4b46fce2010-05-23 11:00:55 -04007902 free_extent_map(em);
7903
7904 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007905
7906unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007907 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7908 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007909err:
chandan50745b02015-08-28 21:10:13 +05307910 if (dio_data)
7911 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007912 /*
7913 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7914 * write less data then expected, so that we don't underflow our inode's
7915 * outstanding extents counter.
7916 */
7917 if (create && dio_data)
7918 adjust_dio_outstanding_extents(inode, dio_data, len);
7919
Josef Bacikeb838e72012-07-31 16:28:48 -04007920 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007921}
7922
Miao Xie8b110e32014-09-12 18:44:03 +08007923static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007924 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007925{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007927 int ret;
7928
Mike Christie37226b22016-06-05 14:31:52 -05007929 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007930
7931 bio_get(bio);
7932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007933 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007934 if (ret)
7935 goto err;
7936
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007937 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007938err:
7939 bio_put(bio);
7940 return ret;
7941}
7942
7943static int btrfs_check_dio_repairable(struct inode *inode,
7944 struct bio *failed_bio,
7945 struct io_failure_record *failrec,
7946 int failed_mirror)
7947{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007949 int num_copies;
7950
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007951 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007952 if (num_copies == 1) {
7953 /*
7954 * we only have a single copy of the data, so don't bother with
7955 * all the retry and error correction code that follows. no
7956 * matter what the error is, it is very likely to persist.
7957 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007958 btrfs_debug(fs_info,
7959 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7960 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007961 return 0;
7962 }
7963
7964 failrec->failed_mirror = failed_mirror;
7965 failrec->this_mirror++;
7966 if (failrec->this_mirror == failed_mirror)
7967 failrec->this_mirror++;
7968
7969 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007970 btrfs_debug(fs_info,
7971 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7972 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007973 return 0;
7974 }
7975
7976 return 1;
7977}
7978
7979static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307980 struct page *page, unsigned int pgoff,
7981 u64 start, u64 end, int failed_mirror,
7982 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007983{
7984 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007985 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7986 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007987 struct bio *bio;
7988 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007989 int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007990 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007991 int ret;
7992
Mike Christie37226b22016-06-05 14:31:52 -05007993 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007994
7995 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7996 if (ret)
7997 return ret;
7998
7999 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8000 failed_mirror);
8001 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008002 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008003 return -EIO;
8004 }
8005
Liu Bo17347ce2017-05-15 15:33:27 -07008006 segs = bio_segments(failed_bio);
8007 if (segs > 1 ||
8008 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008009 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008010
8011 isector = start - btrfs_io_bio(failed_bio)->logical;
8012 isector >>= inode->i_sb->s_blocksize_bits;
8013 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008015 if (!bio) {
Josef Bacik7870d082017-05-05 11:57:15 -04008016 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 return -EIO;
8018 }
Mike Christie37226b22016-06-05 14:31:52 -05008019 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008020
8021 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8022 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8023 read_mode, failrec->this_mirror, failrec->in_validation);
8024
Mike Christie81a75f672016-06-05 14:31:54 -05008025 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008026 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008027 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008028 bio_put(bio);
8029 }
8030
8031 return ret;
8032}
8033
8034struct btrfs_retry_complete {
8035 struct completion done;
8036 struct inode *inode;
8037 u64 start;
8038 int uptodate;
8039};
8040
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008041static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008042{
8043 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008044 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008045 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008046 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008047 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
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008053 io_tree = &BTRFS_I(inode)->io_tree;
8054 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8055 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056
Miao Xie8b110e32014-09-12 18:44:03 +08008057 done->uptodate = 1;
8058 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008059 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8060 io_tree, done->start, bvec->bv_page,
8061 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008062end:
8063 complete(&done->done);
8064 bio_put(bio);
8065}
8066
8067static int __btrfs_correct_data_nocsum(struct inode *inode,
8068 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008069{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008071 struct bio_vec bvec;
8072 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008073 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008074 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075 unsigned int pgoff;
8076 u32 sectorsize;
8077 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008078 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008079 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008080
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308081 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008082 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083
Miao Xiec1dc0892014-09-12 18:43:56 +08008084 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008086 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008087
Liu Bo17347ce2017-05-15 15:33:27 -07008088 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8089 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8090 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091
8092next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008093 done.uptodate = 0;
8094 done.start = start;
8095 init_completion(&done.done);
8096
Liu Bo17347ce2017-05-15 15:33:27 -07008097 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098 pgoff, start, start + sectorsize - 1,
8099 io_bio->mirror_num,
8100 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008101 if (ret) {
8102 err = ret;
8103 goto next;
8104 }
Miao Xie8b110e32014-09-12 18:44:03 +08008105
8106 wait_for_completion(&done.done);
8107
8108 if (!done.uptodate) {
8109 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008111 }
8112
Liu Bo629ebf42017-05-15 17:20:07 -07008113next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 start += sectorsize;
8115
Liu Bo97bf5a52017-04-07 13:11:10 -07008116 nr_sectors--;
8117 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008119 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308120 goto next_block_or_try_again;
8121 }
Miao Xie8b110e32014-09-12 18:44:03 +08008122 }
8123
Liu Bo629ebf42017-05-15 17:20:07 -07008124 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008125}
8126
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008127static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008128{
8129 struct btrfs_retry_complete *done = bio->bi_private;
8130 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008131 struct extent_io_tree *io_tree, *failure_tree;
8132 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008133 struct bio_vec *bvec;
8134 int uptodate;
8135 int ret;
8136 int i;
8137
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008138 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008139 goto end;
8140
8141 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308142
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308143 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008144 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145
Josef Bacik7870d082017-05-05 11:57:15 -04008146 io_tree = &BTRFS_I(inode)->io_tree;
8147 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8148
Miao Xie8b110e32014-09-12 18:44:03 +08008149 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008150 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8151 bvec->bv_offset, done->start,
8152 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008153 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008154 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8155 failure_tree, io_tree, done->start,
8156 bvec->bv_page,
8157 btrfs_ino(BTRFS_I(inode)),
8158 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008159 else
8160 uptodate = 0;
8161 }
8162
8163 done->uptodate = uptodate;
8164end:
8165 complete(&done->done);
8166 bio_put(bio);
8167}
8168
8169static int __btrfs_subio_endio_read(struct inode *inode,
8170 struct btrfs_io_bio *io_bio, int err)
8171{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008173 struct bio_vec bvec;
8174 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008175 struct btrfs_retry_complete done;
8176 u64 start;
8177 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308178 u32 sectorsize;
8179 int nr_sectors;
8180 unsigned int pgoff;
8181 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008182 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008183 int ret;
8184
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308185 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008186 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308187
Miao Xie8b110e32014-09-12 18:44:03 +08008188 err = 0;
8189 start = io_bio->logical;
8190 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008191 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008192
Liu Bo17347ce2017-05-15 15:33:27 -07008193 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8194 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308195
Liu Bo17347ce2017-05-15 15:33:27 -07008196 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308197next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008198 if (uptodate) {
8199 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8200 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8201 bvec.bv_page, pgoff, start, sectorsize);
8202 if (likely(!ret))
8203 goto next;
8204 }
Miao Xie8b110e32014-09-12 18:44:03 +08008205try_again:
8206 done.uptodate = 0;
8207 done.start = start;
8208 init_completion(&done.done);
8209
Liu Bo17347ce2017-05-15 15:33:27 -07008210 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308211 pgoff, start, start + sectorsize - 1,
8212 io_bio->mirror_num,
8213 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008214 if (ret) {
8215 err = ret;
8216 goto next;
8217 }
8218
8219 wait_for_completion(&done.done);
8220
8221 if (!done.uptodate) {
8222 /* We might have another mirror, so try again */
8223 goto try_again;
8224 }
8225next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226 offset += sectorsize;
8227 start += sectorsize;
8228
8229 ASSERT(nr_sectors);
8230
Liu Bo97bf5a52017-04-07 13:11:10 -07008231 nr_sectors--;
8232 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308233 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008234 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308235 goto next_block;
8236 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008237 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008238
8239 return err;
8240}
8241
Miao Xie8b110e32014-09-12 18:44:03 +08008242static int btrfs_subio_endio_read(struct inode *inode,
8243 struct btrfs_io_bio *io_bio, int err)
8244{
8245 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8246
8247 if (skip_csum) {
8248 if (unlikely(err))
8249 return __btrfs_correct_data_nocsum(inode, io_bio);
8250 else
8251 return 0;
8252 } else {
8253 return __btrfs_subio_endio_read(inode, io_bio, err);
8254 }
8255}
8256
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008257static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008258{
8259 struct btrfs_dio_private *dip = bio->bi_private;
8260 struct inode *inode = dip->inode;
8261 struct bio *dio_bio;
8262 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008263 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008264
Liu Boe3d37fa2017-05-17 15:42:00 -06008265 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008266 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008267 if (!err)
8268 bio->bi_error = 0;
8269 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008270
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008272 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008273 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008274
Josef Bacik4b46fce2010-05-23 11:00:55 -04008275 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008276
Filipe Manana1636d1d2016-02-15 16:20:26 +00008277 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008278 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008279
8280 if (io_bio->end_io)
8281 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008282 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008283}
8284
Qu Wenruo524272602017-03-08 10:25:52 +08008285static void __endio_write_update_ordered(struct inode *inode,
8286 const u64 offset, const u64 bytes,
8287 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008290 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008291 struct btrfs_workqueue *wq;
8292 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008293 u64 ordered_offset = offset;
8294 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008295 int ret;
8296
Qu Wenruo524272602017-03-08 10:25:52 +08008297 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8298 wq = fs_info->endio_freespace_worker;
8299 func = btrfs_freespace_write_helper;
8300 } else {
8301 wq = fs_info->endio_write_workers;
8302 func = btrfs_endio_write_helper;
8303 }
8304
Chris Mason163cf092010-11-28 19:56:33 -05008305again:
8306 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8307 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008308 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008309 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008310 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008311 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008312
Qu Wenruo524272602017-03-08 10:25:52 +08008313 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8314 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008315out_test:
8316 /*
8317 * our bio might span multiple ordered extents. If we haven't
8318 * completed the accounting for the whole dio, go back and try again
8319 */
Filipe Manana14543772015-11-24 16:23:54 +00008320 if (ordered_offset < offset + bytes) {
8321 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008322 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008323 goto again;
8324 }
Filipe Manana14543772015-11-24 16:23:54 +00008325}
8326
8327static void btrfs_endio_direct_write(struct bio *bio)
8328{
8329 struct btrfs_dio_private *dip = bio->bi_private;
8330 struct bio *dio_bio = dip->dio_bio;
8331
Qu Wenruo524272602017-03-08 10:25:52 +08008332 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8333 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008334
Josef Bacik4b46fce2010-05-23 11:00:55 -04008335 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008336
Filipe Manana1636d1d2016-02-15 16:20:26 +00008337 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008338 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008339 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008340}
8341
Josef Bacikc6100a42017-05-05 11:57:13 -04008342static int __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008343 struct bio *bio, int mirror_num,
8344 unsigned long bio_flags, u64 offset)
8345{
Josef Bacikc6100a42017-05-05 11:57:13 -04008346 struct inode *inode = private_data;
Chris Masoneaf25d92010-05-25 09:48:28 -04008347 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008348 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008349 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008350 return 0;
8351}
8352
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008353static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008354{
8355 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008356 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008357
Miao Xie8b110e32014-09-12 18:44:03 +08008358 if (err)
8359 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008360 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008361 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8362 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008363 (unsigned long long)bio->bi_iter.bi_sector,
8364 bio->bi_iter.bi_size, err);
8365
8366 if (dip->subio_endio)
8367 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008368
8369 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008370 dip->errors = 1;
8371
8372 /*
8373 * before atomic variable goto zero, we must make sure
8374 * dip->errors is perceived to be set.
8375 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008376 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008377 }
8378
8379 /* if there are more bios still pending for this dio, just exit */
8380 if (!atomic_dec_and_test(&dip->pending_bios))
8381 goto out;
8382
Chris Mason9be33952013-05-17 18:30:14 -04008383 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008384 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008385 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008386 dip->dio_bio->bi_error = 0;
8387 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008388 }
8389out:
8390 bio_put(bio);
8391}
8392
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008393static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008394 struct btrfs_dio_private *dip,
8395 struct bio *bio,
8396 u64 file_offset)
8397{
8398 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8399 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8400 int ret;
8401
8402 /*
8403 * We load all the csum data we need when we submit
8404 * the first bio to reduce the csum tree search and
8405 * contention.
8406 */
8407 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008408 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008409 file_offset);
8410 if (ret)
8411 return ret;
8412 }
8413
8414 if (bio == dip->orig_bio)
8415 return 0;
8416
8417 file_offset -= dip->logical_offset;
8418 file_offset >>= inode->i_sb->s_blocksize_bits;
8419 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8420
8421 return 0;
8422}
8423
Miao Xiee65e1532010-11-22 03:04:43 +00008424static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008425 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008426 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008429 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008430 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008431 int ret;
8432
Josef Bacikb812ce22012-11-16 13:56:32 -05008433 if (async_submit)
8434 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8435
Miao Xiee65e1532010-11-22 03:04:43 +00008436 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008437
8438 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008439 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008440 if (ret)
8441 goto err;
8442 }
Miao Xiee65e1532010-11-22 03:04:43 +00008443
Josef Bacik1ae39932011-04-06 14:41:34 -04008444 if (skip_sum)
8445 goto map;
8446
8447 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008448 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8449 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008450 __btrfs_submit_bio_start_direct_io,
8451 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008452 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008453 } else if (write) {
8454 /*
8455 * If we aren't doing async submit, calculate the csum of the
8456 * bio now.
8457 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008458 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008459 if (ret)
8460 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008461 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008462 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008463 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008464 if (ret)
8465 goto err;
8466 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008467map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008468 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008469err:
8470 bio_put(bio);
8471 return ret;
8472}
8473
Mike Christie81a75f672016-06-05 14:31:54 -05008474static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008475 int skip_sum)
8476{
8477 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008478 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008479 struct bio *bio;
8480 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008481 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008482 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008483 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008484 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008485 u64 submit_len;
8486 int clone_offset = 0;
8487 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308488 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008489
Kent Overstreet4f024f32013-10-11 15:44:27 -07008490 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008491 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008492 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8493 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008494 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008495 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008496
Liu Bo725130b2017-05-16 09:51:39 -07008497 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008498 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008499 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008500 goto submit;
8501 }
8502
David Woodhouse53b381b2013-01-29 18:40:14 -05008503 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008504 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008505 async_submit = 0;
8506 else
8507 async_submit = 1;
8508
Liu Bo725130b2017-05-16 09:51:39 -07008509 /* bio split */
8510 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008511 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008512 do {
Liu Bo725130b2017-05-16 09:51:39 -07008513 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008514
Liu Bo725130b2017-05-16 09:51:39 -07008515 /*
8516 * This will never fail as it's passing GPF_NOFS and
8517 * the allocation is backed by btrfs_bioset.
8518 */
Liu Boe4770942017-05-16 10:57:14 -07008519 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008520 clone_len);
8521 bio->bi_private = dip;
8522 bio->bi_end_io = btrfs_end_dio_bio;
8523 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008524
Liu Bo725130b2017-05-16 09:51:39 -07008525 ASSERT(submit_len >= clone_len);
8526 submit_len -= clone_len;
8527 if (submit_len == 0)
8528 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008529
Liu Bo725130b2017-05-16 09:51:39 -07008530 /*
8531 * Increase the count before we submit the bio so we know
8532 * the end IO handler won't happen before we increase the
8533 * count. Otherwise, the dip might get freed before we're
8534 * done setting it up.
8535 */
8536 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008537
Liu Bo725130b2017-05-16 09:51:39 -07008538 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8539 async_submit);
8540 if (ret) {
8541 bio_put(bio);
8542 atomic_dec(&dip->pending_bios);
8543 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008544 }
Liu Bo725130b2017-05-16 09:51:39 -07008545
8546 clone_offset += clone_len;
8547 start_sector += clone_len >> 9;
8548 file_offset += clone_len;
8549
8550 map_length = submit_len;
8551 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8552 start_sector << 9, &map_length, NULL, 0);
8553 if (ret)
8554 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008555 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008556
Josef Bacik02f57c72011-04-06 14:25:44 -04008557submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008558 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008559 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008560 if (!ret)
8561 return 0;
8562
8563 bio_put(bio);
8564out_err:
8565 dip->errors = 1;
8566 /*
8567 * before atomic variable goto zero, we must
8568 * make sure dip->errors is perceived to be set.
8569 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008570 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008571 if (atomic_dec_and_test(&dip->pending_bios))
8572 bio_io_error(dip->orig_bio);
8573
8574 /* bio_end_io() will handle error, so we needn't return it */
8575 return 0;
8576}
8577
Mike Christie8a4c1e42016-06-05 14:31:50 -05008578static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8579 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008580{
Filipe Manana61de7182015-07-01 12:13:10 +01008581 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008582 struct bio *bio = NULL;
8583 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008584 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008585 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008586 int ret = 0;
8587
8588 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8589
David Sterba8b6c1d52017-06-02 17:48:13 +02008590 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591
Miao Xiec1dc0892014-09-12 18:43:56 +08008592 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008593 if (!dip) {
8594 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008595 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008596 }
8597
8598 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599 dip->inode = inode;
8600 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008601 dip->bytes = dio_bio->bi_iter.bi_size;
8602 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008603 bio->bi_private = dip;
8604 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008605 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008606 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008607 io_bio = btrfs_io_bio(bio);
8608 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008609
Miao Xiec1dc0892014-09-12 18:43:56 +08008610 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008611 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008612 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008613 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008614 dip->subio_endio = btrfs_subio_endio_read;
8615 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008616
Filipe Mananaf28a4922015-12-08 19:23:20 +00008617 /*
8618 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8619 * even if we fail to submit a bio, because in such case we do the
8620 * corresponding error handling below and it must not be done a second
8621 * time by btrfs_direct_IO().
8622 */
8623 if (write) {
8624 struct btrfs_dio_data *dio_data = current->journal_info;
8625
8626 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8627 dip->bytes;
8628 dio_data->unsubmitted_oe_range_start =
8629 dio_data->unsubmitted_oe_range_end;
8630 }
8631
Mike Christie81a75f672016-06-05 14:31:54 -05008632 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008633 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008634 return;
Chris Mason9be33952013-05-17 18:30:14 -04008635
Liu Bo3892ac92017-04-17 15:00:28 -07008636 if (io_bio->end_io)
8637 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008638
Josef Bacik4b46fce2010-05-23 11:00:55 -04008639free_ordered:
8640 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008641 * If we arrived here it means either we failed to submit the dip
8642 * or we either failed to clone the dio_bio or failed to allocate the
8643 * dip. If we cloned the dio_bio and allocated the dip, we can just
8644 * call bio_endio against our io_bio so that we get proper resource
8645 * cleanup if we fail to submit the dip, otherwise, we must do the
8646 * same as btrfs_endio_direct_[write|read] because we can't call these
8647 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008648 */
Liu Bo3892ac92017-04-17 15:00:28 -07008649 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008650 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008651 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008652 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008653 * and all the cleanup and final put for dio_bio (through
8654 * dio_end_io()).
8655 */
8656 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008657 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008658 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008659 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008660 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008661 file_offset,
8662 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008663 false);
Filipe Manana14543772015-11-24 16:23:54 +00008664 else
Filipe Manana61de7182015-07-01 12:13:10 +01008665 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8666 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008667
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008668 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008669 /*
8670 * Releases and cleans up our dio_bio, no need to bio_put()
8671 * nor bio_endio()/bio_io_error() against dio_bio.
8672 */
8673 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008674 }
Liu Bo3892ac92017-04-17 15:00:28 -07008675 if (bio)
8676 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008677 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008678}
8679
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008680static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8681 struct kiocb *iocb,
8682 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008683{
8684 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008685 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008686 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008687 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008688
8689 if (offset & blocksize_mask)
8690 goto out;
8691
Al Viro28060d52014-03-22 05:15:17 -04008692 if (iov_iter_alignment(iter) & blocksize_mask)
8693 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008694
Al Viro28060d52014-03-22 05:15:17 -04008695 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008696 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008697 return 0;
8698 /*
8699 * Check to make sure we don't have duplicate iov_base's in this
8700 * iovec, if so return EINVAL, otherwise we'll get csum errors
8701 * when reading back.
8702 */
8703 for (seg = 0; seg < iter->nr_segs; seg++) {
8704 for (i = seg + 1; i < iter->nr_segs; i++) {
8705 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008706 goto out;
8707 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008708 }
8709 retval = 0;
8710out:
8711 return retval;
8712}
Josef Bacikeb838e72012-07-31 16:28:48 -04008713
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008714static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008715{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008716 struct file *file = iocb->ki_filp;
8717 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308719 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008720 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008721 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008722 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008723 bool wakeup = true;
8724 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008725 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008726
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008727 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008728 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008729
Jens Axboefe0f07d2015-04-15 17:05:48 -06008730 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008731 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008732
Josef Bacik0e267c42013-07-02 10:38:02 -04008733 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008734 * The generic stuff only does filemap_write_and_wait_range, which
8735 * isn't enough if we've written compressed pages to this area, so
8736 * we need to flush the dirty pages again to make absolutely sure
8737 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008738 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008739 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008740 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8741 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008742 filemap_fdatawrite_range(inode->i_mapping, offset,
8743 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008744
Omar Sandoval6f673762015-03-16 04:33:52 -07008745 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008746 /*
8747 * If the write DIO is beyond the EOF, we need update
8748 * the isize, but it is protected by i_mutex. So we can
8749 * not unlock the i_mutex at this case.
8750 */
8751 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008752 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008753 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008754 relock = true;
8755 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008756 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008757 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008758 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008759 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008760
8761 /*
8762 * We need to know how many extents we reserved so that we can
8763 * do the accounting properly if we go over the number we
8764 * originally calculated. Abuse current->journal_info for this.
8765 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008766 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008767 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008768 dio_data.unsubmitted_oe_range_start = (u64)offset;
8769 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308770 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308771 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008772 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8773 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008774 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008775 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8776 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008777 }
8778
Omar Sandoval17f8c842015-03-16 04:33:50 -07008779 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008780 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008781 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008782 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008783 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308784 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008785 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008786 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308787 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008788 btrfs_delalloc_release_space(inode, offset,
8789 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008790 /*
8791 * On error we might have left some ordered extents
8792 * without submitting corresponding bios for them, so
8793 * cleanup them up to avoid other tasks getting them
8794 * and waiting for them to complete forever.
8795 */
8796 if (dio_data.unsubmitted_oe_range_start <
8797 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008798 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008799 dio_data.unsubmitted_oe_range_start,
8800 dio_data.unsubmitted_oe_range_end -
8801 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008802 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008803 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008804 btrfs_delalloc_release_space(inode, offset,
8805 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008806 }
Miao Xie38851cc2013-02-08 07:04:11 +00008807out:
Miao Xie2e60a512013-02-08 07:01:08 +00008808 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008809 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008810 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008811 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008812
8813 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008814}
8815
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008816#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8817
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008818static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8819 __u64 start, __u64 len)
8820{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008821 int ret;
8822
8823 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8824 if (ret)
8825 return ret;
8826
Chris Masonec29ed52011-02-23 16:23:20 -05008827 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008828}
8829
Chris Mason9ebefb182007-06-15 13:50:00 -04008830int btrfs_readpage(struct file *file, struct page *page)
8831{
Chris Masond1310b22008-01-24 16:13:08 -05008832 struct extent_io_tree *tree;
8833 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008834 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008835}
Chris Mason1832a6d2007-12-21 16:27:21 -05008836
Chris Mason39279cc2007-06-12 06:35:45 -04008837static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8838{
Chris Masond1310b22008-01-24 16:13:08 -05008839 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008840 struct inode *inode = page->mapping->host;
8841 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008842
8843 if (current->flags & PF_MEMALLOC) {
8844 redirty_page_for_writepage(wbc, page);
8845 unlock_page(page);
8846 return 0;
8847 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008848
8849 /*
8850 * If we are under memory pressure we will call this directly from the
8851 * VM, we need to make sure we have the inode referenced for the ordered
8852 * extent. If not just return like we didn't do anything.
8853 */
8854 if (!igrab(inode)) {
8855 redirty_page_for_writepage(wbc, page);
8856 return AOP_WRITEPAGE_ACTIVATE;
8857 }
Chris Masond1310b22008-01-24 16:13:08 -05008858 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008859 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8860 btrfs_add_delayed_iput(inode);
8861 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008862}
Chris Mason39279cc2007-06-12 06:35:45 -04008863
Eric Sandeen48a3b632013-04-25 20:41:01 +00008864static int btrfs_writepages(struct address_space *mapping,
8865 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008866{
Chris Masond1310b22008-01-24 16:13:08 -05008867 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008868
Chris Masond1310b22008-01-24 16:13:08 -05008869 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008870 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8871}
8872
Chris Mason3ab2fb52007-11-08 10:59:22 -05008873static int
8874btrfs_readpages(struct file *file, struct address_space *mapping,
8875 struct list_head *pages, unsigned nr_pages)
8876{
Chris Masond1310b22008-01-24 16:13:08 -05008877 struct extent_io_tree *tree;
8878 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008879 return extent_readpages(tree, mapping, pages, nr_pages,
8880 btrfs_get_extent);
8881}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008882static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008883{
Chris Masond1310b22008-01-24 16:13:08 -05008884 struct extent_io_tree *tree;
8885 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008886 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008887
Chris Masond1310b22008-01-24 16:13:08 -05008888 tree = &BTRFS_I(page->mapping->host)->io_tree;
8889 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008890 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008891 if (ret == 1) {
8892 ClearPagePrivate(page);
8893 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008894 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008895 }
8896 return ret;
8897}
Chris Mason39279cc2007-06-12 06:35:45 -04008898
Chris Masone6dcd2d2008-07-17 12:53:50 -04008899static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8900{
Chris Mason98509cf2008-09-11 15:51:43 -04008901 if (PageWriteback(page) || PageDirty(page))
8902 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008903 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904}
8905
Lukas Czernerd47992f2013-05-21 23:17:23 -04008906static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8907 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008908{
Josef Bacik5fd02042012-05-02 14:00:54 -04008909 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008910 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008912 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008914 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308915 u64 start;
8916 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008917 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008918
Chris Mason8b62b722009-09-02 16:53:46 -04008919 /*
8920 * we have the page locked, so new writeback can't start,
8921 * and the dirty bit won't be cleared while we are here.
8922 *
8923 * Wait for IO on this page so that we can safely clear
8924 * the PagePrivate2 bit and do ordered accounting
8925 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008927
Josef Bacik5fd02042012-05-02 14:00:54 -04008928 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008929 if (offset) {
8930 btrfs_releasepage(page, GFP_NOFS);
8931 return;
8932 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008933
8934 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008935 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308936again:
8937 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008938 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308939 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308941 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008942 /*
8943 * IO on this page will never be started, so we need
8944 * to account for any ordered extents now
8945 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008946 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308947 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008948 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008949 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008950 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8951 EXTENT_DEFRAG, 1, 0, &cached_state,
8952 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008953 /*
8954 * whoever cleared the private bit is responsible
8955 * for the finish_ordered_io
8956 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008957 if (TestClearPagePrivate2(page)) {
8958 struct btrfs_ordered_inode_tree *tree;
8959 u64 new_len;
8960
8961 tree = &BTRFS_I(inode)->ordered_tree;
8962
8963 spin_lock_irq(&tree->lock);
8964 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308965 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008966 if (new_len < ordered->truncated_len)
8967 ordered->truncated_len = new_len;
8968 spin_unlock_irq(&tree->lock);
8969
8970 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308971 start,
8972 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008973 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008974 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008976 if (!inode_evicting) {
8977 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308978 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008979 &cached_state);
8980 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981
8982 start = end + 1;
8983 if (start < page_end)
8984 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008985 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008986
Qu Wenruob9d0b382015-09-29 10:35:16 +08008987 /*
8988 * Qgroup reserved space handler
8989 * Page here will be either
8990 * 1) Already written to disk
8991 * In this case, its reserved space is released from data rsv map
8992 * and will be freed by delayed_ref handler finally.
8993 * So even we call qgroup_free_data(), it won't decrease reserved
8994 * space.
8995 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008996 * This means the reserved space should be freed here. However,
8997 * if a truncate invalidates the page (by clearing PageDirty)
8998 * and the page is accounted for while allocating extent
8999 * in btrfs_check_data_free_space() we let delayed_ref to
9000 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009001 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009002 if (PageDirty(page))
9003 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009004 if (!inode_evicting) {
9005 clear_extent_bit(tree, page_start, page_end,
9006 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009007 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9008 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009009 &cached_state, GFP_NOFS);
9010
9011 __btrfs_releasepage(page, GFP_NOFS);
9012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013
Chris Mason4a096752008-07-21 10:29:44 -04009014 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009015 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009016 ClearPagePrivate(page);
9017 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009019 }
Chris Mason39279cc2007-06-12 06:35:45 -04009020}
9021
Chris Mason9ebefb182007-06-15 13:50:00 -04009022/*
9023 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9024 * called from a page fault handler when a page is first dirtied. Hence we must
9025 * be careful to check for EOF conditions here. We set the page up correctly
9026 * for a written page which means we get ENOSPC checking when writing into
9027 * holes and correct delalloc and unwritten extent mapping on filesystems that
9028 * support these features.
9029 *
9030 * We are not allowed to take the i_mutex here so we have to play games to
9031 * protect against truncate races as the page could now be beyond EOF. Because
9032 * vmtruncate() writes the inode size before removing pages, once we have the
9033 * page lock we can determine safely if the page is beyond EOF. If it is not
9034 * beyond EOF, then the page is guaranteed safe against truncation until we
9035 * unlock the page.
9036 */
Dave Jiang11bac802017-02-24 14:56:41 -08009037int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009038{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009039 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009040 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009041 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009042 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9043 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009044 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009045 char *kaddr;
9046 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009047 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009048 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009049 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309050 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009051 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309053 u64 end;
9054
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009055 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009056
Jan Karab2b5ef52012-06-12 16:20:45 +02009057 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009058 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009059 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309060 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009061
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309062 /*
9063 * Reserving delalloc space after obtaining the page lock can lead to
9064 * deadlock. For example, if a dirty page is locked by this function
9065 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9066 * dirty page write out, then the btrfs_writepage() function could
9067 * end up waiting indefinitely to get a lock on the page currently
9068 * being processed by btrfs_page_mkwrite() function.
9069 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009070 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309071 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009072 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009073 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009074 reserved = 1;
9075 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009076 if (ret) {
9077 if (ret == -ENOMEM)
9078 ret = VM_FAULT_OOM;
9079 else /* -ENOSPC, -EIO, etc */
9080 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009081 if (reserved)
9082 goto out;
9083 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009084 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009085
Nick Piggin56a76f82009-03-31 15:23:23 -07009086 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009087again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009088 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009089 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009090
Chris Mason9ebefb182007-06-15 13:50:00 -04009091 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009092 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009093 /* page got truncated out from underneath us */
9094 goto out_unlock;
9095 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009096 wait_on_page_writeback(page);
9097
David Sterbaff13db42015-12-03 14:30:40 +01009098 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009099 set_page_extent_mapped(page);
9100
Chris Masoneb84ae02008-07-17 13:53:27 -04009101 /*
9102 * we can't set the delalloc bits if there are pending ordered
9103 * extents. Drop our locks and wait for them to finish
9104 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009105 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9106 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009108 unlock_extent_cached(io_tree, page_start, page_end,
9109 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009110 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009111 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112 btrfs_put_ordered_extent(ordered);
9113 goto again;
9114 }
9115
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009116 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009117 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009118 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009119 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309120 end = page_start + reserved_space - 1;
9121 spin_lock(&BTRFS_I(inode)->lock);
9122 BTRFS_I(inode)->outstanding_extents++;
9123 spin_unlock(&BTRFS_I(inode)->lock);
9124 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009125 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309126 }
9127 }
9128
Josef Bacikfbf19082009-10-01 17:10:23 -04009129 /*
Liu Bo54160342016-12-13 12:15:19 -08009130 * page_mkwrite gets called when the page is firstly dirtied after it's
9131 * faulted in, but write(2) could also dirty a page and set delalloc
9132 * bits, thus in this case for space account reason, we still need to
9133 * clear any delalloc bits within this page range since we have to
9134 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009135 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309136 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009137 EXTENT_DIRTY | EXTENT_DELALLOC |
9138 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009139 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009140
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309141 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009142 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009143 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009144 unlock_extent_cached(io_tree, page_start, page_end,
9145 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009146 ret = VM_FAULT_SIGBUS;
9147 goto out_unlock;
9148 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009149 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009150
9151 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009152 if (page_start + PAGE_SIZE > size)
9153 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009154 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009155 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009156
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009157 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009158 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009159 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009160 flush_dcache_page(page);
9161 kunmap(page);
9162 }
Chris Mason247e7432008-07-17 12:53:51 -04009163 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009164 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009165 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009166
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009167 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009168 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009169 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009170
Josef Bacik2ac55d42010-02-03 19:33:23 +00009171 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009172
9173out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009174 if (!ret) {
9175 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009176 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009177 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009178 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009179out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309180 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009181out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009182 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009183 return ret;
9184}
9185
Josef Bacika41ad392011-01-31 15:30:16 -05009186static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009187{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009189 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009190 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009191 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009192 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009193 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009194 u64 mask = fs_info->sectorsize - 1;
9195 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009196
Josef Bacik0ef8b722013-10-25 16:13:35 -04009197 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9198 (u64)-1);
9199 if (ret)
9200 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009201
Josef Bacikfcb80c22011-05-03 10:40:22 -04009202 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009203 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009204 * 3 things going on here
9205 *
9206 * 1) We need to reserve space for our orphan item and the space to
9207 * delete our orphan item. Lord knows we don't want to have a dangling
9208 * orphan item because we didn't reserve space to remove it.
9209 *
9210 * 2) We need to reserve space to update our inode.
9211 *
9212 * 3) We need to have something to cache all the space that is going to
9213 * be free'd up by the truncate operation, but also have some slack
9214 * space reserved in case it uses space during the truncate (thank you
9215 * very much snapshotting).
9216 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009217 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009219 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220 * doesn't end up using space reserved for updating the inode or
9221 * removing the orphan item. We also need to be able to stop the
9222 * transaction and start a new one, which means we need to be able to
9223 * update the inode several times, and we have no idea of knowing how
9224 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009225 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009226 * Then there is the orphan item, which does indeed need to be held on
9227 * to for the whole operation, and we need nobody to touch this reserved
9228 * space except the orphan code.
9229 *
9230 * So that leaves us with
9231 *
9232 * 1) root->orphan_block_rsv - for the orphan deletion.
9233 * 2) rsv - for the truncate reservation, which we will steal from the
9234 * transaction reservation.
9235 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9236 * updating the inode.
9237 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009238 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 if (!rsv)
9240 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009241 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009242 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009243
Josef Bacik907cbce2011-08-08 13:46:15 -04009244 /*
Josef Bacik07127182011-08-19 10:29:59 -04009245 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009246 * 1 for updating the inode.
9247 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009248 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249 if (IS_ERR(trans)) {
9250 err = PTR_ERR(trans);
9251 goto out;
9252 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009253
Josef Bacik907cbce2011-08-08 13:46:15 -04009254 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009255 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009256 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009257 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009258
Chris Mason5a3f23d2009-03-31 13:27:11 -04009259 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009260 * So if we truncate and then write and fsync we normally would just
9261 * write the extents that changed, which is a problem if we need to
9262 * first truncate that entire inode. So set this flag so we write out
9263 * all of the extents in the inode to the sync log so we're completely
9264 * safe.
9265 */
9266 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009267 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009268
Yan, Zheng80825102009-11-12 09:35:36 +00009269 while (1) {
9270 ret = btrfs_truncate_inode_items(trans, root, inode,
9271 inode->i_size,
9272 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009273 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009274 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009275 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009276 }
Chris Mason39279cc2007-06-12 06:35:45 -04009277
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009278 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009279 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009280 if (ret) {
9281 err = ret;
9282 break;
9283 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009284
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009285 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009286 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009287
9288 trans = btrfs_start_transaction(root, 2);
9289 if (IS_ERR(trans)) {
9290 ret = err = PTR_ERR(trans);
9291 trans = NULL;
9292 break;
9293 }
9294
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009295 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009296 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009297 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009298 BUG_ON(ret); /* shouldn't happen */
9299 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009300 }
9301
9302 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009303 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009304 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009305 if (ret)
9306 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009307 }
9308
Chris Mason917c16b2011-11-08 14:49:59 -05009309 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009310 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009311 ret = btrfs_update_inode(trans, root, inode);
9312 if (ret && !err)
9313 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009314
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009315 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009316 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009317 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009318out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009319 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009320
Josef Bacik3893e332011-01-31 16:03:11 -05009321 if (ret && !err)
9322 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009323
Josef Bacik3893e332011-01-31 16:03:11 -05009324 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009325}
9326
Sven Wegener3b963622008-06-09 21:57:42 -04009327/*
Chris Masond352ac62008-09-29 15:18:18 -04009328 * create a new subvolume directory/inode (helper for the ioctl).
9329 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009330int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009331 struct btrfs_root *new_root,
9332 struct btrfs_root *parent_root,
9333 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009334{
Chris Mason39279cc2007-06-12 06:35:45 -04009335 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009336 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009337 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009338
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009339 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9340 new_dirid, new_dirid,
9341 S_IFDIR | (~current_umask() & S_IRWXUGO),
9342 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009343 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009344 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009345 inode->i_op = &btrfs_dir_inode_operations;
9346 inode->i_fop = &btrfs_dir_file_operations;
9347
Miklos Szeredibfe86842011-10-28 14:13:29 +02009348 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009349 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009350 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009351
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009352 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9353 if (err)
9354 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009355 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009356 new_root->root_key.objectid, err);
9357
Yan, Zheng76dda932009-09-21 16:00:26 -04009358 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009359
Yan, Zheng76dda932009-09-21 16:00:26 -04009360 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009361 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009362}
9363
Chris Mason39279cc2007-06-12 06:35:45 -04009364struct inode *btrfs_alloc_inode(struct super_block *sb)
9365{
9366 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009367 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009368
9369 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9370 if (!ei)
9371 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009372
9373 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009374 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009375 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009376 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009377 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009378 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009379 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009380 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009381 ei->disk_i_size = 0;
9382 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009383 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009384 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009385 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009386 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009387 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009388 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009389
Josef Bacik9e0baf62011-07-15 15:16:44 +00009390 spin_lock_init(&ei->lock);
9391 ei->outstanding_extents = 0;
9392 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009393
Josef Bacik72ac3c02012-05-23 14:13:11 -04009394 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009395 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009396
Miao Xie16cdcec2011-04-22 18:12:22 +08009397 ei->delayed_node = NULL;
9398
chandan r9cc97d62012-07-04 12:48:07 +05309399 ei->i_otime.tv_sec = 0;
9400 ei->i_otime.tv_nsec = 0;
9401
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009402 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009403 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009404 extent_io_tree_init(&ei->io_tree, inode);
9405 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009406 ei->io_tree.track_uptodate = 1;
9407 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009408 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009409 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009410 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009411 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009412 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009413 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009414 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009415 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416
9417 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009418}
9419
Josef Bacikaaedb552013-10-11 14:44:09 -04009420#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9421void btrfs_test_destroy_inode(struct inode *inode)
9422{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009423 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009424 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9425}
9426#endif
9427
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009428static void btrfs_i_callback(struct rcu_head *head)
9429{
9430 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009431 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9432}
9433
Chris Mason39279cc2007-06-12 06:35:45 -04009434void btrfs_destroy_inode(struct inode *inode)
9435{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009436 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009437 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009438 struct btrfs_root *root = BTRFS_I(inode)->root;
9439
Al Virob3d9b7a2012-06-09 13:51:19 -04009440 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009441 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009442 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9443 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009444 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009445 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009446 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009447 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009448
Chris Mason5a3f23d2009-03-31 13:27:11 -04009449 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009450 * This can happen where we create an inode, but somebody else also
9451 * created the same inode and we need to destroy the one we already
9452 * created.
9453 */
9454 if (!root)
9455 goto free;
9456
Josef Bacik8a35d952012-05-23 14:26:42 -04009457 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9458 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009459 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009460 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009461 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009462 }
Josef Bacik7b128762008-07-24 12:17:14 -04009463
Chris Masond3977122009-01-05 21:25:51 -05009464 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009465 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9466 if (!ordered)
9467 break;
9468 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009469 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009470 "found ordered extent %llu %llu on inode cleanup",
9471 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009472 btrfs_remove_ordered_extent(inode, ordered);
9473 btrfs_put_ordered_extent(ordered);
9474 btrfs_put_ordered_extent(ordered);
9475 }
9476 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009477 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009478 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009479 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009480free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009481 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009482}
9483
Al Viro45321ac2010-06-07 13:43:19 -04009484int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009485{
9486 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009487
Naohiro Aota6379ef92013-06-06 09:56:34 +00009488 if (root == NULL)
9489 return 1;
9490
Liu Bofa6ac872013-02-20 14:10:23 +00009491 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009492 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009493 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009494 else
Al Viro45321ac2010-06-07 13:43:19 -04009495 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009496}
9497
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009498static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009499{
9500 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9501
9502 inode_init_once(&ei->vfs_inode);
9503}
9504
9505void btrfs_destroy_cachep(void)
9506{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009507 /*
9508 * Make sure all delayed rcu free inodes are flushed before we
9509 * destroy cache.
9510 */
9511 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009512 kmem_cache_destroy(btrfs_inode_cachep);
9513 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009514 kmem_cache_destroy(btrfs_path_cachep);
9515 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009516}
9517
9518int btrfs_init_cachep(void)
9519{
David Sterba837e1972012-09-07 03:00:48 -06009520 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009521 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009522 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9523 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009524 if (!btrfs_inode_cachep)
9525 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009526
David Sterba837e1972012-09-07 03:00:48 -06009527 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009528 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009529 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009530 if (!btrfs_trans_handle_cachep)
9531 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009532
David Sterba837e1972012-09-07 03:00:48 -06009533 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009534 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009535 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009536 if (!btrfs_path_cachep)
9537 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009538
David Sterba837e1972012-09-07 03:00:48 -06009539 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009540 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009541 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009542 if (!btrfs_free_space_cachep)
9543 goto fail;
9544
Chris Mason39279cc2007-06-12 06:35:45 -04009545 return 0;
9546fail:
9547 btrfs_destroy_cachep();
9548 return -ENOMEM;
9549}
9550
David Howellsa528d352017-01-31 16:46:22 +00009551static int btrfs_getattr(const struct path *path, struct kstat *stat,
9552 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009553{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009554 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009555 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009556 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009557 u32 bi_flags = BTRFS_I(inode)->flags;
9558
9559 stat->result_mask |= STATX_BTIME;
9560 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9561 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9562 if (bi_flags & BTRFS_INODE_APPEND)
9563 stat->attributes |= STATX_ATTR_APPEND;
9564 if (bi_flags & BTRFS_INODE_COMPRESS)
9565 stat->attributes |= STATX_ATTR_COMPRESSED;
9566 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9567 stat->attributes |= STATX_ATTR_IMMUTABLE;
9568 if (bi_flags & BTRFS_INODE_NODUMP)
9569 stat->attributes |= STATX_ATTR_NODUMP;
9570
9571 stat->attributes_mask |= (STATX_ATTR_APPEND |
9572 STATX_ATTR_COMPRESSED |
9573 STATX_ATTR_IMMUTABLE |
9574 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009575
Chris Mason39279cc2007-06-12 06:35:45 -04009576 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009577 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009578
9579 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009580 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009581 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009582 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009583 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009584 return 0;
9585}
9586
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587static int btrfs_rename_exchange(struct inode *old_dir,
9588 struct dentry *old_dentry,
9589 struct inode *new_dir,
9590 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009592 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009593 struct btrfs_trans_handle *trans;
9594 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009595 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 struct inode *new_inode = new_dentry->d_inode;
9597 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009598 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009600 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9601 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 u64 old_idx = 0;
9603 u64 new_idx = 0;
9604 u64 root_objectid;
9605 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009606 bool root_log_pinned = false;
9607 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608
9609 /* we only allow rename subvolume link between subvolumes */
9610 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9611 return -EXDEV;
9612
9613 /* close the race window with snapshot create/destroy ioctl */
9614 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009615 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009617 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618
9619 /*
9620 * We want to reserve the absolute worst case amount of items. So if
9621 * both inodes are subvols and we need to unlink them then that would
9622 * require 4 item modifications, but if they are both normal inodes it
9623 * would require 5 item modifications, so we'll assume their normal
9624 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9625 * should cover the worst case number of items we'll modify.
9626 */
9627 trans = btrfs_start_transaction(root, 12);
9628 if (IS_ERR(trans)) {
9629 ret = PTR_ERR(trans);
9630 goto out_notrans;
9631 }
9632
9633 /*
9634 * We need to find a free sequence number both in the source and
9635 * in the destination directory for the exchange.
9636 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009637 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009638 if (ret)
9639 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009640 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 if (ret)
9642 goto out_fail;
9643
9644 BTRFS_I(old_inode)->dir_index = 0ULL;
9645 BTRFS_I(new_inode)->dir_index = 0ULL;
9646
9647 /* Reference for the source. */
9648 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9649 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009650 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009652 btrfs_pin_log_trans(root);
9653 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 ret = btrfs_insert_inode_ref(trans, dest,
9655 new_dentry->d_name.name,
9656 new_dentry->d_name.len,
9657 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009658 btrfs_ino(BTRFS_I(new_dir)),
9659 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009660 if (ret)
9661 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 }
9663
9664 /* And now for the dest. */
9665 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9666 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009667 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009669 btrfs_pin_log_trans(dest);
9670 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 ret = btrfs_insert_inode_ref(trans, root,
9672 old_dentry->d_name.name,
9673 old_dentry->d_name.len,
9674 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009675 btrfs_ino(BTRFS_I(old_dir)),
9676 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009677 if (ret)
9678 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 }
9680
9681 /* Update inode version and ctime/mtime. */
9682 inode_inc_iversion(old_dir);
9683 inode_inc_iversion(new_dir);
9684 inode_inc_iversion(old_inode);
9685 inode_inc_iversion(new_inode);
9686 old_dir->i_ctime = old_dir->i_mtime = ctime;
9687 new_dir->i_ctime = new_dir->i_mtime = ctime;
9688 old_inode->i_ctime = ctime;
9689 new_inode->i_ctime = ctime;
9690
9691 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009692 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9693 BTRFS_I(old_inode), 1);
9694 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9695 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 }
9697
9698 /* src is a subvolume */
9699 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9700 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9701 ret = btrfs_unlink_subvol(trans, root, old_dir,
9702 root_objectid,
9703 old_dentry->d_name.name,
9704 old_dentry->d_name.len);
9705 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009706 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9707 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 old_dentry->d_name.name,
9709 old_dentry->d_name.len);
9710 if (!ret)
9711 ret = btrfs_update_inode(trans, root, old_inode);
9712 }
9713 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009714 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 goto out_fail;
9716 }
9717
9718 /* dest is a subvolume */
9719 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9720 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9721 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9722 root_objectid,
9723 new_dentry->d_name.name,
9724 new_dentry->d_name.len);
9725 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009726 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9727 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009728 new_dentry->d_name.name,
9729 new_dentry->d_name.len);
9730 if (!ret)
9731 ret = btrfs_update_inode(trans, dest, new_inode);
9732 }
9733 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009734 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 goto out_fail;
9736 }
9737
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009738 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 new_dentry->d_name.name,
9740 new_dentry->d_name.len, 0, old_idx);
9741 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009742 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 goto out_fail;
9744 }
9745
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009746 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747 old_dentry->d_name.name,
9748 old_dentry->d_name.len, 0, new_idx);
9749 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009750 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751 goto out_fail;
9752 }
9753
9754 if (old_inode->i_nlink == 1)
9755 BTRFS_I(old_inode)->dir_index = old_idx;
9756 if (new_inode->i_nlink == 1)
9757 BTRFS_I(new_inode)->dir_index = new_idx;
9758
Filipe Manana86e8aa02016-05-05 02:02:27 +01009759 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009760 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009761 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9762 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009764 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009765 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009766 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009767 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009768 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9769 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009771 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009772 }
9773out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009774 /*
9775 * If we have pinned a log and an error happened, we unpin tasks
9776 * trying to sync the log and force them to fallback to a transaction
9777 * commit if the log currently contains any of the inodes involved in
9778 * this rename operation (to ensure we do not persist a log with an
9779 * inconsistent state for any of these inodes or leading to any
9780 * inconsistencies when replayed). If the transaction was aborted, the
9781 * abortion reason is propagated to userspace when attempting to commit
9782 * the transaction. If the log does not contain any of these inodes, we
9783 * allow the tasks to sync it.
9784 */
9785 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009786 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9787 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9788 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009789 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009790 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009791 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009792
9793 if (root_log_pinned) {
9794 btrfs_end_log_trans(root);
9795 root_log_pinned = false;
9796 }
9797 if (dest_log_pinned) {
9798 btrfs_end_log_trans(dest);
9799 dest_log_pinned = false;
9800 }
9801 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009802 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803out_notrans:
9804 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009805 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009807 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808
9809 return ret;
9810}
9811
9812static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9813 struct btrfs_root *root,
9814 struct inode *dir,
9815 struct dentry *dentry)
9816{
9817 int ret;
9818 struct inode *inode;
9819 u64 objectid;
9820 u64 index;
9821
9822 ret = btrfs_find_free_ino(root, &objectid);
9823 if (ret)
9824 return ret;
9825
9826 inode = btrfs_new_inode(trans, root, dir,
9827 dentry->d_name.name,
9828 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009829 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009830 objectid,
9831 S_IFCHR | WHITEOUT_MODE,
9832 &index);
9833
9834 if (IS_ERR(inode)) {
9835 ret = PTR_ERR(inode);
9836 return ret;
9837 }
9838
9839 inode->i_op = &btrfs_special_inode_operations;
9840 init_special_inode(inode, inode->i_mode,
9841 WHITEOUT_DEV);
9842
9843 ret = btrfs_init_inode_security(trans, inode, dir,
9844 &dentry->d_name);
9845 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009846 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009847
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009848 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9849 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009850 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009851 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009852
9853 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009854out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009855 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009856 if (ret)
9857 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009858 iput(inode);
9859
Filipe Mananac9901612016-05-05 01:41:57 +01009860 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009861}
9862
Chris Mason39279cc2007-06-12 06:35:45 -04009863static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009864 struct inode *new_dir, struct dentry *new_dentry,
9865 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009866{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009867 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009868 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009869 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009870 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9871 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009872 struct inode *new_inode = d_inode(new_dentry);
9873 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009874 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009876 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009877 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009878 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009879
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009880 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009881 return -EPERM;
9882
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009883 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009884 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009885 return -EXDEV;
9886
Li Zefan33345d012011-04-20 10:31:50 +08009887 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009888 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 return -ENOTEMPTY;
9890
Chris Mason39279cc2007-06-12 06:35:45 -04009891 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009893 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009894
9895
9896 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009897 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009898 new_dentry->d_name.name,
9899 new_dentry->d_name.len);
9900
9901 if (ret) {
9902 if (ret == -EEXIST) {
9903 /* we shouldn't get
9904 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309905 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009906 return ret;
9907 }
9908 } else {
9909 /* maybe -EOVERFLOW */
9910 return ret;
9911 }
9912 }
9913 ret = 0;
9914
Chris Mason5a3f23d2009-03-31 13:27:11 -04009915 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009916 * we're using rename to replace one file with another. Start IO on it
9917 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009918 */
Chris Mason8d875f92014-08-12 10:47:42 -07009919 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009920 filemap_flush(old_inode->i_mapping);
9921
Yan, Zheng76dda932009-09-21 16:00:26 -04009922 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009923 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009924 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009925 /*
9926 * We want to reserve the absolute worst case amount of items. So if
9927 * both inodes are subvols and we need to unlink them then that would
9928 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009929 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009930 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9931 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009932 * If our rename has the whiteout flag, we need more 5 units for the
9933 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9934 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009935 */
Filipe Manana5062af32016-05-05 10:26:26 +01009936 trans_num_items = 11;
9937 if (flags & RENAME_WHITEOUT)
9938 trans_num_items += 5;
9939 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009940 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009941 ret = PTR_ERR(trans);
9942 goto out_notrans;
9943 }
Chris Mason5f39d392007-10-15 16:14:19 -04009944
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 if (dest != root)
9946 btrfs_record_root_in_trans(trans, dest);
9947
Nikolay Borisov877574e2017-02-20 13:50:33 +02009948 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009949 if (ret)
9950 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009951
Miao Xie67de1172013-12-26 13:07:06 +08009952 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009953 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009955 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009956 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009957 btrfs_pin_log_trans(root);
9958 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009959 ret = btrfs_insert_inode_ref(trans, dest,
9960 new_dentry->d_name.name,
9961 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009962 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009963 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009964 if (ret)
9965 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009966 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009967
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009968 inode_inc_iversion(old_dir);
9969 inode_inc_iversion(new_dir);
9970 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009971 old_dir->i_ctime = old_dir->i_mtime =
9972 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009973 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009974
Chris Mason12fcfd22009-03-24 10:24:20 -04009975 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009976 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9977 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009978
Li Zefan33345d012011-04-20 10:31:50 +08009979 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9981 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9982 old_dentry->d_name.name,
9983 old_dentry->d_name.len);
9984 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009985 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9986 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009987 old_dentry->d_name.name,
9988 old_dentry->d_name.len);
9989 if (!ret)
9990 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009991 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009992 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009993 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009994 goto out_fail;
9995 }
Chris Mason39279cc2007-06-12 06:35:45 -04009996
9997 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009998 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009999 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010000 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10002 root_objectid = BTRFS_I(new_inode)->location.objectid;
10003 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10004 root_objectid,
10005 new_dentry->d_name.name,
10006 new_dentry->d_name.len);
10007 BUG_ON(new_inode->i_nlink == 0);
10008 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010009 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10010 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010011 new_dentry->d_name.name,
10012 new_dentry->d_name.len);
10013 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010014 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010015 ret = btrfs_orphan_add(trans,
10016 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010017 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010018 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010019 goto out_fail;
10020 }
Chris Mason39279cc2007-06-12 06:35:45 -040010021 }
Josef Bacikaec74772008-07-24 12:12:38 -040010022
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010023 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010024 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010025 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010026 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010027 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010028 goto out_fail;
10029 }
Chris Mason39279cc2007-06-12 06:35:45 -040010030
Miao Xie67de1172013-12-26 13:07:06 +080010031 if (old_inode->i_nlink == 1)
10032 BTRFS_I(old_inode)->dir_index = index;
10033
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010034 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010035 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010036
David Sterbaf85b7372017-01-20 14:54:07 +010010037 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10038 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010039 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010040 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010041 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010042
10043 if (flags & RENAME_WHITEOUT) {
10044 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10045 old_dentry);
10046
10047 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010048 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010049 goto out_fail;
10050 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010051 }
Chris Mason39279cc2007-06-12 06:35:45 -040010052out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010053 /*
10054 * If we have pinned the log and an error happened, we unpin tasks
10055 * trying to sync the log and force them to fallback to a transaction
10056 * commit if the log currently contains any of the inodes involved in
10057 * this rename operation (to ensure we do not persist a log with an
10058 * inconsistent state for any of these inodes or leading to any
10059 * inconsistencies when replayed). If the transaction was aborted, the
10060 * abortion reason is propagated to userspace when attempting to commit
10061 * the transaction. If the log does not contain any of these inodes, we
10062 * allow the tasks to sync it.
10063 */
10064 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010065 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10066 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10067 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010068 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010069 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010070 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010071
10072 btrfs_end_log_trans(root);
10073 log_pinned = false;
10074 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010075 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010076out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010077 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010078 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010079
Chris Mason39279cc2007-06-12 06:35:45 -040010080 return ret;
10081}
10082
Miklos Szeredi80ace852014-07-23 15:15:32 +020010083static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10084 struct inode *new_dir, struct dentry *new_dentry,
10085 unsigned int flags)
10086{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010087 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010088 return -EINVAL;
10089
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010090 if (flags & RENAME_EXCHANGE)
10091 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10092 new_dentry);
10093
10094 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010095}
10096
Miao Xie8ccf6f192012-10-25 09:28:04 +000010097static void btrfs_run_delalloc_work(struct btrfs_work *work)
10098{
10099 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010100 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010101
10102 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10103 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010104 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010105 filemap_flush(inode->i_mapping);
10106 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10107 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010108 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010109
10110 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010111 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010112 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010113 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010114 complete(&delalloc_work->completion);
10115}
10116
10117struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010118 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010119{
10120 struct btrfs_delalloc_work *work;
10121
David Sterba100d5702015-12-08 14:39:32 +010010122 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010123 if (!work)
10124 return NULL;
10125
10126 init_completion(&work->completion);
10127 INIT_LIST_HEAD(&work->list);
10128 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010130 WARN_ON_ONCE(!inode);
10131 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10132 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010133
10134 return work;
10135}
10136
10137void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10138{
10139 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010140 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010141}
10142
Chris Masond352ac62008-09-29 15:18:18 -040010143/*
10144 * some fairly slow code that needs optimization. This walks the list
10145 * of all the inodes with pending delalloc and forces them to disk.
10146 */
Miao Xie6c255e62014-03-06 13:55:01 +080010147static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10148 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010149{
Chris Masonea8c2812008-08-04 23:17:27 -040010150 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010151 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152 struct btrfs_delalloc_work *work, *next;
10153 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010154 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010155 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010156
Miao Xie8ccf6f192012-10-25 09:28:04 +000010157 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010158 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010159
Miao Xie573bfb72014-03-06 13:55:03 +080010160 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 spin_lock(&root->delalloc_lock);
10162 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010163 while (!list_empty(&splice)) {
10164 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010165 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010166
Miao Xieeb73c1b2013-05-15 07:48:22 +000010167 list_move_tail(&binode->delalloc_inodes,
10168 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010169 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010170 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010172 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010173 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010175
David Sterba651d4942015-11-27 19:24:16 +010010176 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010177 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010178 if (delay_iput)
10179 btrfs_add_delayed_iput(inode);
10180 else
10181 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010182 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010183 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010184 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010185 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010186 btrfs_queue_work(root->fs_info->flush_workers,
10187 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010188 ret++;
10189 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010190 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010191 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010192 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010193 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010194 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010195
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010196out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010197 list_for_each_entry_safe(work, next, &works, list) {
10198 list_del_init(&work->list);
10199 btrfs_wait_and_free_delalloc_work(work);
10200 }
10201
Miao Xieeb73c1b2013-05-15 07:48:22 +000010202 if (!list_empty_careful(&splice)) {
10203 spin_lock(&root->delalloc_lock);
10204 list_splice_tail(&splice, &root->delalloc_inodes);
10205 spin_unlock(&root->delalloc_lock);
10206 }
Miao Xie573bfb72014-03-06 13:55:03 +080010207 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 return ret;
10209}
10210
10211int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10212{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010213 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 int ret;
10215
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010216 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 return -EROFS;
10218
Miao Xie6c255e62014-03-06 13:55:01 +080010219 ret = __start_delalloc_inodes(root, delay_iput, -1);
10220 if (ret > 0)
10221 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 /*
10223 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010224 * we have to make sure the IO is actually started and that
10225 * ordered extents get created before we return
10226 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010227 atomic_inc(&fs_info->async_submit_draining);
10228 while (atomic_read(&fs_info->nr_async_submits) ||
10229 atomic_read(&fs_info->async_delalloc_pages)) {
10230 wait_event(fs_info->async_submit_wait,
10231 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10232 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010233 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010234 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010235 return ret;
10236}
10237
Miao Xie6c255e62014-03-06 13:55:01 +080010238int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10239 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010240{
10241 struct btrfs_root *root;
10242 struct list_head splice;
10243 int ret;
10244
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010245 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010246 return -EROFS;
10247
10248 INIT_LIST_HEAD(&splice);
10249
Miao Xie573bfb72014-03-06 13:55:03 +080010250 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010251 spin_lock(&fs_info->delalloc_root_lock);
10252 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010253 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010254 root = list_first_entry(&splice, struct btrfs_root,
10255 delalloc_root);
10256 root = btrfs_grab_fs_root(root);
10257 BUG_ON(!root);
10258 list_move_tail(&root->delalloc_root,
10259 &fs_info->delalloc_roots);
10260 spin_unlock(&fs_info->delalloc_root_lock);
10261
Miao Xie6c255e62014-03-06 13:55:01 +080010262 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010263 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010264 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010265 goto out;
10266
Miao Xie6c255e62014-03-06 13:55:01 +080010267 if (nr != -1) {
10268 nr -= ret;
10269 WARN_ON(nr < 0);
10270 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010271 spin_lock(&fs_info->delalloc_root_lock);
10272 }
10273 spin_unlock(&fs_info->delalloc_root_lock);
10274
Miao Xie6c255e62014-03-06 13:55:01 +080010275 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 atomic_inc(&fs_info->async_submit_draining);
10277 while (atomic_read(&fs_info->nr_async_submits) ||
10278 atomic_read(&fs_info->async_delalloc_pages)) {
10279 wait_event(fs_info->async_submit_wait,
10280 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10281 atomic_read(&fs_info->async_delalloc_pages) == 0));
10282 }
10283 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010284out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010285 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010286 spin_lock(&fs_info->delalloc_root_lock);
10287 list_splice_tail(&splice, &fs_info->delalloc_roots);
10288 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010289 }
Miao Xie573bfb72014-03-06 13:55:03 +080010290 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010291 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010292}
10293
Chris Mason39279cc2007-06-12 06:35:45 -040010294static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10295 const char *symname)
10296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010297 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010298 struct btrfs_trans_handle *trans;
10299 struct btrfs_root *root = BTRFS_I(dir)->root;
10300 struct btrfs_path *path;
10301 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010302 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010303 int err;
10304 int drop_inode = 0;
10305 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010306 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010307 int name_len;
10308 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010309 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010310 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010311 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010312
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010313 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010315 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010316
Josef Bacik9ed74f22009-09-11 16:12:44 -040010317 /*
10318 * 2 items for inode item and ref
10319 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010320 * 1 item for updating parent inode item
10321 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010322 * 1 item for xattr if selinux is on
10323 */
Filipe Manana9269d122015-12-31 18:16:29 +000010324 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010325 if (IS_ERR(trans))
10326 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010327
Li Zefan581bb052011-04-20 10:06:11 +080010328 err = btrfs_find_free_ino(root, &objectid);
10329 if (err)
10330 goto out_unlock;
10331
Josef Bacikaec74772008-07-24 12:12:38 -040010332 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010333 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10334 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010335 if (IS_ERR(inode)) {
10336 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010337 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010338 }
Chris Mason39279cc2007-06-12 06:35:45 -040010339
Casey Schauflerad19db72011-12-15 10:09:07 -050010340 /*
10341 * If the active LSM wants to access the inode during
10342 * d_instantiate it needs these. Smack checks to see
10343 * if the filesystem supports xattrs by looking at the
10344 * ops vector.
10345 */
10346 inode->i_fop = &btrfs_file_operations;
10347 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010348 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010349 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10350
10351 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10352 if (err)
10353 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010354
Chris Mason39279cc2007-06-12 06:35:45 -040010355 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010356 if (!path) {
10357 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010358 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010359 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010360 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010361 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010362 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010363 datasize = btrfs_file_extent_calc_inline_size(name_len);
10364 err = btrfs_insert_empty_item(trans, root, path, &key,
10365 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010366 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010367 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010368 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010369 }
Chris Mason5f39d392007-10-15 16:14:19 -040010370 leaf = path->nodes[0];
10371 ei = btrfs_item_ptr(leaf, path->slots[0],
10372 struct btrfs_file_extent_item);
10373 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10374 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010375 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010376 btrfs_set_file_extent_encryption(leaf, ei, 0);
10377 btrfs_set_file_extent_compression(leaf, ei, 0);
10378 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10379 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10380
Chris Mason39279cc2007-06-12 06:35:45 -040010381 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010382 write_extent_buffer(leaf, symname, ptr, name_len);
10383 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010384 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010385
Chris Mason39279cc2007-06-12 06:35:45 -040010386 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010387 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010388 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010389 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010390 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010391 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010392 /*
10393 * Last step, add directory indexes for our symlink inode. This is the
10394 * last step to avoid extra cleanup of these indexes if an error happens
10395 * elsewhere above.
10396 */
10397 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010398 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10399 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010400 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010401 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010402 goto out_unlock_inode;
10403 }
10404
10405 unlock_new_inode(inode);
10406 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010407
10408out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010409 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010410 if (drop_inode) {
10411 inode_dec_link_count(inode);
10412 iput(inode);
10413 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010414 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010415 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010416
10417out_unlock_inode:
10418 drop_inode = 1;
10419 unlock_new_inode(inode);
10420 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010421}
Chris Mason16432982008-04-10 10:23:21 -040010422
Josef Bacik0af3d002010-06-21 14:48:16 -040010423static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10424 u64 start, u64 num_bytes, u64 min_size,
10425 loff_t actual_len, u64 *alloc_hint,
10426 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010429 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10430 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010431 struct btrfs_root *root = BTRFS_I(inode)->root;
10432 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010433 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010434 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010435 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010436 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010437 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010438 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010439 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010440
Josef Bacik0af3d002010-06-21 14:48:16 -040010441 if (trans)
10442 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010443 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010444 if (own_trans) {
10445 trans = btrfs_start_transaction(root, 3);
10446 if (IS_ERR(trans)) {
10447 ret = PTR_ERR(trans);
10448 break;
10449 }
Yan Zhengd899e052008-10-30 14:25:28 -040010450 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010451
Byongho Leeee221842015-12-15 01:42:10 +090010452 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010453 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010454 /*
10455 * If we are severely fragmented we could end up with really
10456 * small allocations, so if the allocator is returning small
10457 * chunks lets make its job easier by only searching for those
10458 * sized chunks.
10459 */
10460 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010461 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10462 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010463 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010464 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010465 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010466 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010467 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010468 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010469
Josef Bacik0b670dc2015-09-23 17:11:16 -040010470 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010471 ret = insert_reserved_file_extent(trans, inode,
10472 cur_offset, ins.objectid,
10473 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010474 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010475 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010476 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010477 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010478 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010479 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010480 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010481 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010482 break;
10483 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010484
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010485 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010486 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010487
Josef Bacik5dc562c2012-08-17 13:14:17 -040010488 em = alloc_extent_map();
10489 if (!em) {
10490 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10491 &BTRFS_I(inode)->runtime_flags);
10492 goto next;
10493 }
10494
10495 em->start = cur_offset;
10496 em->orig_start = cur_offset;
10497 em->len = ins.offset;
10498 em->block_start = ins.objectid;
10499 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010500 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010501 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010502 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010503 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10504 em->generation = trans->transid;
10505
10506 while (1) {
10507 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010508 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010509 write_unlock(&em_tree->lock);
10510 if (ret != -EEXIST)
10511 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010512 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010513 cur_offset + ins.offset - 1,
10514 0);
10515 }
10516 free_extent_map(em);
10517next:
Yan Zhengd899e052008-10-30 14:25:28 -040010518 num_bytes -= ins.offset;
10519 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010520 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010521
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010522 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010523 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010524 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010525 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010526 (actual_len > inode->i_size) &&
10527 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010528 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010529 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010530 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010531 i_size = cur_offset;
10532 i_size_write(inode, i_size);
10533 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010534 }
10535
Yan Zhengd899e052008-10-30 14:25:28 -040010536 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010537
10538 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010539 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010540 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010541 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010542 break;
10543 }
Yan Zhengd899e052008-10-30 14:25:28 -040010544
Josef Bacik0af3d002010-06-21 14:48:16 -040010545 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010546 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010547 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010548 if (cur_offset < end)
10549 btrfs_free_reserved_data_space(inode, cur_offset,
10550 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010551 return ret;
10552}
10553
Josef Bacik0af3d002010-06-21 14:48:16 -040010554int btrfs_prealloc_file_range(struct inode *inode, int mode,
10555 u64 start, u64 num_bytes, u64 min_size,
10556 loff_t actual_len, u64 *alloc_hint)
10557{
10558 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10559 min_size, actual_len, alloc_hint,
10560 NULL);
10561}
10562
10563int btrfs_prealloc_file_range_trans(struct inode *inode,
10564 struct btrfs_trans_handle *trans, int mode,
10565 u64 start, u64 num_bytes, u64 min_size,
10566 loff_t actual_len, u64 *alloc_hint)
10567{
10568 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10569 min_size, actual_len, alloc_hint, trans);
10570}
10571
Chris Masone6dcd2d2008-07-17 12:53:50 -040010572static int btrfs_set_page_dirty(struct page *page)
10573{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010574 return __set_page_dirty_nobuffers(page);
10575}
10576
Al Viro10556cb2011-06-20 19:28:19 -040010577static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010578{
Li Zefanb83cc962010-12-20 16:04:08 +080010579 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010580 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010581
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010582 if (mask & MAY_WRITE &&
10583 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10584 if (btrfs_root_readonly(root))
10585 return -EROFS;
10586 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10587 return -EACCES;
10588 }
Al Viro2830ba72011-06-20 19:16:29 -040010589 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010590}
Chris Mason39279cc2007-06-12 06:35:45 -040010591
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010592static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10593{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010594 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010595 struct btrfs_trans_handle *trans;
10596 struct btrfs_root *root = BTRFS_I(dir)->root;
10597 struct inode *inode = NULL;
10598 u64 objectid;
10599 u64 index;
10600 int ret = 0;
10601
10602 /*
10603 * 5 units required for adding orphan entry
10604 */
10605 trans = btrfs_start_transaction(root, 5);
10606 if (IS_ERR(trans))
10607 return PTR_ERR(trans);
10608
10609 ret = btrfs_find_free_ino(root, &objectid);
10610 if (ret)
10611 goto out;
10612
10613 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010614 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010615 if (IS_ERR(inode)) {
10616 ret = PTR_ERR(inode);
10617 inode = NULL;
10618 goto out;
10619 }
10620
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010621 inode->i_fop = &btrfs_file_operations;
10622 inode->i_op = &btrfs_file_inode_operations;
10623
10624 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010625 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10626
Chris Masonb0d5d102014-09-08 13:08:51 -070010627 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10628 if (ret)
10629 goto out_inode;
10630
10631 ret = btrfs_update_inode(trans, root, inode);
10632 if (ret)
10633 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010634 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010635 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010636 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010637
Filipe Manana5762b5c2014-08-01 00:10:32 +010010638 /*
10639 * We set number of links to 0 in btrfs_new_inode(), and here we set
10640 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10641 * through:
10642 *
10643 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10644 */
10645 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010646 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010647 d_tmpfile(dentry, inode);
10648 mark_inode_dirty(inode);
10649
10650out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010651 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010652 if (ret)
10653 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010654 btrfs_balance_delayed_items(fs_info);
10655 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010657
10658out_inode:
10659 unlock_new_inode(inode);
10660 goto out;
10661
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010662}
10663
David Sterba20a7db82017-02-17 16:24:29 +010010664__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010665static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010666{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010667 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010668}
10669
Josef Bacikc6100a42017-05-05 11:57:13 -040010670static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10671{
10672 struct inode *inode = private_data;
10673 return btrfs_sb(inode->i_sb);
10674}
10675
10676static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10677 u64 start, u64 end)
10678{
10679 struct inode *inode = private_data;
10680 u64 isize;
10681
10682 isize = i_size_read(inode);
10683 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10684 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10685 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10686 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10687 }
10688}
10689
10690void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10691{
10692 struct inode *inode = private_data;
10693 unsigned long index = start >> PAGE_SHIFT;
10694 unsigned long end_index = end >> PAGE_SHIFT;
10695 struct page *page;
10696
10697 while (index <= end_index) {
10698 page = find_get_page(inode->i_mapping, index);
10699 ASSERT(page); /* Pages should be in the extent_io_tree */
10700 set_page_writeback(page);
10701 put_page(page);
10702 index++;
10703 }
10704}
10705
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010706static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010707 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010708 .lookup = btrfs_lookup,
10709 .create = btrfs_create,
10710 .unlink = btrfs_unlink,
10711 .link = btrfs_link,
10712 .mkdir = btrfs_mkdir,
10713 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010714 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010715 .symlink = btrfs_symlink,
10716 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010717 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010718 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010719 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010720 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010721 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010722 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010723 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010724};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010725static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010726 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010727 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010728 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010729};
Yan, Zheng76dda932009-09-21 16:00:26 -040010730
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010731static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010732 .llseek = generic_file_llseek,
10733 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010734 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010735 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010736#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010737 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010738#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010739 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010740 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010741};
10742
David Sterba20e55062015-11-19 11:42:28 +010010743static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010744 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010745 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010746 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010747 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010748 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010749 .tree_fs_info = iotree_fs_info,
10750 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010751
10752 /* optional callbacks */
10753 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010754 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010755 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010756 .set_bit_hook = btrfs_set_bit_hook,
10757 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010758 .merge_extent_hook = btrfs_merge_extent_hook,
10759 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010760 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010761};
10762
Chris Mason35054392009-01-21 13:11:13 -050010763/*
10764 * btrfs doesn't support the bmap operation because swapfiles
10765 * use bmap to make a mapping of extents in the file. They assume
10766 * these extents won't change over the life of the file and they
10767 * use the bmap result to do IO directly to the drive.
10768 *
10769 * the btrfs bmap call would return logical addresses that aren't
10770 * suitable for IO and they also will change frequently as COW
10771 * operations happen. So, swapfile + btrfs == corruption.
10772 *
10773 * For now we're avoiding this by dropping bmap.
10774 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010775static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010776 .readpage = btrfs_readpage,
10777 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010778 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010779 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010780 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010781 .invalidatepage = btrfs_invalidatepage,
10782 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010783 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010784 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010785};
10786
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010787static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010788 .readpage = btrfs_readpage,
10789 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010790 .invalidatepage = btrfs_invalidatepage,
10791 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010792};
10793
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010794static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010795 .getattr = btrfs_getattr,
10796 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010797 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010798 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010799 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010800 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010801 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010802 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010803};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010804static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010805 .getattr = btrfs_getattr,
10806 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010807 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010808 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010809 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010810 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010811 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010812};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010813static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010814 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010815 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010816 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010817 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010818 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010819 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010820};
Yan, Zheng76dda932009-09-21 16:00:26 -040010821
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010822const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010823 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010824 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010825};