blob: b74a6d23a0fe629818e5e5bf0af8c7c11280389a [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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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
610 * win, compare the page count read with the blocks on disk
611 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300612 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400613 if (total_compressed >= total_in) {
614 will_compress = 0;
615 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700617 *num_added += 1;
618
619 /*
620 * The async work queues will take care of doing actual
621 * allocation on disk for these compressed pages, and
622 * will submit them to the elevator.
623 */
624 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100625 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700626 compress_type);
627
628 if (start + num_bytes < end) {
629 start += num_bytes;
630 pages = NULL;
631 cond_resched();
632 goto again;
633 }
634 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
636 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400638 /*
639 * the compression code ran but failed to make things smaller,
640 * free any pages it allocated and our page pointer array
641 */
David Sterba4d3a8002017-02-14 19:04:07 +0100642 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400643 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
646 kfree(pages);
647 pages = NULL;
648 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100649 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650
651 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400652 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500653 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500654 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500655 }
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500657cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700658 /*
659 * No compression, but we still need to write the pages in the file
660 * we've been given so far. redirty the locked page if it corresponds
661 * to our extent and set things up for the async work queue to run
662 * cow_file_range to do the normal delalloc dance.
663 */
664 if (page_offset(locked_page) >= start &&
665 page_offset(locked_page) <= end)
666 __set_page_dirty_nobuffers(locked_page);
667 /* unlocked later on in the async handlers */
668
669 if (redirty)
670 extent_range_redirty_for_io(inode, start, end);
671 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
672 BTRFS_COMPRESS_NONE);
673 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500674
Filipe Mananac44f6492014-10-09 21:15:44 +0100675 return;
Chris Mason771ed682008-11-06 22:02:51 -0500676
677free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100678 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500679 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500681 }
Chris Masond3977122009-01-05 21:25:51 -0500682 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500683}
Chris Mason771ed682008-11-06 22:02:51 -0500684
Filipe Manana40ae8372014-10-06 22:14:24 +0100685static void free_async_extent_pages(struct async_extent *async_extent)
686{
687 int i;
688
689 if (!async_extent->pages)
690 return;
691
692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300694 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500699}
700
701/*
702 * phase two of compressed writeback. This is the ordered portion
703 * of the code, which only gets called in the order the work was
704 * queued. We walk all the async extents created by compress_file_range
705 * and send them down to the disk.
706 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100707static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct async_cow *async_cow)
709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct async_extent *async_extent;
712 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500713 struct btrfs_key ins;
714 struct extent_map *em;
715 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719again:
Chris Masond3977122009-01-05 21:25:51 -0500720 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500721 async_extent = list_entry(async_cow->extents.next,
722 struct async_extent, list);
723 list_del(&async_extent->list);
724
725 io_tree = &BTRFS_I(inode)->io_tree;
726
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727retry:
Chris Mason771ed682008-11-06 22:02:51 -0500728 /* did the compression code fall back to uncompressed IO? */
729 if (!async_extent->pages) {
730 int page_started = 0;
731 unsigned long nr_written = 0;
732
733 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000734 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100735 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500736
737 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500738 ret = cow_file_range(inode, async_cow->locked_page,
739 async_extent->start,
740 async_extent->start +
741 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800742 async_extent->start +
743 async_extent->ram_size - 1,
744 &page_started, &nr_written, 0,
745 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 /* JDM XXX */
748
Chris Mason771ed682008-11-06 22:02:51 -0500749 /*
750 * if page_started, cow_file_range inserted an
751 * inline extent and took care of all the unlocking
752 * and IO for us. Otherwise, we need to submit
753 * all those pages down to the drive.
754 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500756 extent_write_locked_range(io_tree,
757 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500758 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500759 async_extent->ram_size - 1,
760 btrfs_get_extent,
761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
763 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
769 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100770 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500771
Wang Xiaoguang18513092016-07-25 15:51:40 +0800772 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500773 async_extent->compressed_size,
774 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800775 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100777 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400779 if (ret == -ENOSPC) {
780 unlock_extent(io_tree, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800783
784 /*
785 * we need to redirty the pages if we decide to
786 * fallback to uncompressed IO, otherwise we
787 * will not submit these pages down to lower
788 * layers.
789 */
790 extent_range_redirty_for_io(inode,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500798 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000799 /*
800 * here we're doing allocation and writeback of the
801 * compressed pages
802 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800803 em = create_io_em(inode, async_extent->start,
804 async_extent->ram_size, /* len */
805 async_extent->start, /* orig_start */
806 ins.objectid, /* block_start */
807 ins.offset, /* block_len */
808 ins.offset, /* orig_block_len */
809 async_extent->ram_size, /* ram_bytes */
810 async_extent->compress_type,
811 BTRFS_ORDERED_COMPRESSED);
812 if (IS_ERR(em))
813 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200825 btrfs_drop_extent_cache(BTRFS_I(inode),
826 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 async_extent->start +
828 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400831 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason771ed682008-11-06 22:02:51 -0500833 /*
834 * clear dirty, set writeback and unlock the pages.
835 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400837 async_extent->start +
838 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800839 async_extent->start +
840 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400841 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
842 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400843 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500844 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500845 async_extent->start,
846 async_extent->ram_size,
847 ins.objectid,
848 ins.offset, async_extent->pages,
849 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 if (ret) {
851 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
852 struct page *p = async_extent->pages[0];
853 const u64 start = async_extent->start;
854 const u64 end = start + async_extent->ram_size - 1;
855
856 p->mapping = inode->i_mapping;
857 tree->ops->writepage_end_io_hook(p, start, end,
858 NULL, 0);
859 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800860 extent_clear_unlock_delalloc(inode, start, end, end,
861 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100862 PAGE_END_WRITEBACK |
863 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100864 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100865 }
Chris Mason771ed682008-11-06 22:02:51 -0500866 alloc_hint = ins.objectid + ins.offset;
867 kfree(async_extent);
868 cond_resched();
869 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100870 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500871out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400872 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400873 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100874out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400875 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876 async_extent->start +
877 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800878 async_extent->start +
879 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400880 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100881 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400882 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
883 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100884 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
885 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100886 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500889}
890
Josef Bacik4b46fce2010-05-23 11:00:55 -0400891static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
892 u64 num_bytes)
893{
894 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
895 struct extent_map *em;
896 u64 alloc_hint = 0;
897
898 read_lock(&em_tree->lock);
899 em = search_extent_mapping(em_tree, start, num_bytes);
900 if (em) {
901 /*
902 * if block start isn't an actual block number then find the
903 * first block in this inode and use that as a hint. If that
904 * block is also bogus then just don't worry about it.
905 */
906 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
907 free_extent_map(em);
908 em = search_extent_mapping(em_tree, 0, 0);
909 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
910 alloc_hint = em->block_start;
911 if (em)
912 free_extent_map(em);
913 } else {
914 alloc_hint = em->block_start;
915 free_extent_map(em);
916 }
917 }
918 read_unlock(&em_tree->lock);
919
920 return alloc_hint;
921}
922
Chris Mason771ed682008-11-06 22:02:51 -0500923/*
924 * when extent_io.c finds a delayed allocation range in the file,
925 * the call backs end up in this code. The basic idea is to
926 * allocate extents on disk for the range, and create ordered data structs
927 * in ram to track those extents.
928 *
929 * locked_page is the page that writepage had locked already. We use
930 * it to make sure we don't do extra locks or unlocks.
931 *
932 * *page_started is set to one if we unlock locked_page and do everything
933 * required to start IO on it. It may be clean and already done with
934 * IO when we return.
935 */
Josef Bacik00361582013-08-14 14:02:47 -0400936static noinline int cow_file_range(struct inode *inode,
937 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800938 u64 start, u64 end, u64 delalloc_end,
939 int *page_started, unsigned long *nr_written,
940 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500941{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400943 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500944 u64 alloc_hint = 0;
945 u64 num_bytes;
946 unsigned long ram_size;
947 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000948 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400949 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500950 struct btrfs_key ins;
951 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000952 unsigned clear_bits;
953 unsigned long page_ops;
954 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500955 int ret = 0;
956
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200957 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500959 ret = -EINVAL;
960 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400961 }
Chris Mason771ed682008-11-06 22:02:51 -0500962
Qu Wenruofda28322013-02-26 08:10:22 +0000963 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500964 num_bytes = max(blocksize, num_bytes);
965 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500966
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200967 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400968
Chris Mason771ed682008-11-06 22:02:51 -0500969 if (start == 0) {
970 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800971 ret = cow_file_range_inline(root, inode, start, end, 0,
972 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800974 extent_clear_unlock_delalloc(inode, start, end,
975 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400976 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100977 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400978 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400979 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
980 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800981 btrfs_free_reserved_data_space_noquota(inode, start,
982 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500983 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300984 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500985 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500986 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100988 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500989 }
990 }
Chris Masonc8b97812008-10-29 14:49:59 -0400991
992 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400993 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400994
Josef Bacik4b46fce2010-05-23 11:00:55 -0400995 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200996 btrfs_drop_extent_cache(BTRFS_I(inode), start,
997 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400998
Chris Masond3977122009-01-05 21:25:51 -0500999 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001000 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001001 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001002 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001003 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001004 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001006 cur_alloc_size = ins.offset;
1007 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001008
Chris Mason771ed682008-11-06 22:02:51 -05001009 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001010 em = create_io_em(inode, start, ins.offset, /* len */
1011 start, /* orig_start */
1012 ins.objectid, /* block_start */
1013 ins.offset, /* block_len */
1014 ins.offset, /* orig_block_len */
1015 ram_size, /* ram_bytes */
1016 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001017 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001018 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001019 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001020 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001023 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001024 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001025 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001026
Yan Zheng17d217f2008-12-12 10:03:38 -05001027 if (root->root_key.objectid ==
1028 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1029 ret = btrfs_reloc_clone_csums(inode, start,
1030 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001031 /*
1032 * Only drop cache here, and process as normal.
1033 *
1034 * We must not allow extent_clear_unlock_delalloc()
1035 * at out_unlock label to free meta of this ordered
1036 * extent, as its meta should be freed by
1037 * btrfs_finish_ordered_io().
1038 *
1039 * So we must continue until @start is increased to
1040 * skip current ordered extent.
1041 */
Josef Bacik00361582013-08-14 14:02:47 -04001042 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001043 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1044 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001045 }
1046
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001047 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001048
Chris Masonc8b97812008-10-29 14:49:59 -04001049 /* we're not doing compressed IO, don't unlock the first
1050 * page (which the caller expects to stay locked), don't
1051 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001052 *
1053 * Do set the Private2 bit so we know this page was properly
1054 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001055 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001056 page_ops = unlock ? PAGE_UNLOCK : 0;
1057 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001058
Josef Bacikc2790a22013-07-29 11:20:47 -04001059 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001060 start + ram_size - 1,
1061 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001062 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001063 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001064 if (disk_num_bytes < cur_alloc_size)
1065 disk_num_bytes = 0;
1066 else
1067 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001068 num_bytes -= cur_alloc_size;
1069 alloc_hint = ins.objectid + ins.offset;
1070 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001072
1073 /*
1074 * btrfs_reloc_clone_csums() error, since start is increased
1075 * extent_clear_unlock_delalloc() at out_unlock label won't
1076 * free metadata of current ordered extent, we're OK to exit.
1077 */
1078 if (ret)
1079 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001080 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001083
Filipe Mananad9f85962014-08-25 10:43:00 +01001084out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001085 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001086out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001088 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001090 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1091 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001092 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1093 PAGE_END_WRITEBACK;
1094 /*
1095 * If we reserved an extent for our delalloc range (or a subrange) and
1096 * failed to create the respective ordered extent, then it means that
1097 * when we reserved the extent we decremented the extent's size from
1098 * the data space_info's bytes_may_use counter and incremented the
1099 * space_info's bytes_reserved counter by the same amount. We must make
1100 * sure extent_clear_unlock_delalloc() does not try to decrement again
1101 * the data space_info's bytes_may_use counter, therefore we do not pass
1102 * it the flag EXTENT_CLEAR_DATA_RESV.
1103 */
1104 if (extent_reserved) {
1105 extent_clear_unlock_delalloc(inode, start,
1106 start + cur_alloc_size,
1107 start + cur_alloc_size,
1108 locked_page,
1109 clear_bits,
1110 page_ops);
1111 start += cur_alloc_size;
1112 if (start >= end)
1113 goto out;
1114 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001115 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1116 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001117 clear_bits | EXTENT_CLEAR_DATA_RESV,
1118 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001120}
Chris Masonc8b97812008-10-29 14:49:59 -04001121
Chris Mason771ed682008-11-06 22:02:51 -05001122/*
1123 * work queue call back to started compression on a file and pages
1124 */
1125static noinline void async_cow_start(struct btrfs_work *work)
1126{
1127 struct async_cow *async_cow;
1128 int num_added = 0;
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 compress_file_range(async_cow->inode, async_cow->locked_page,
1132 async_cow->start, async_cow->end, async_cow,
1133 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001135 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001137 }
Chris Mason771ed682008-11-06 22:02:51 -05001138}
1139
1140/*
1141 * work queue call back to submit previously compressed pages
1142 */
1143static noinline void async_cow_submit(struct btrfs_work *work)
1144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001146 struct async_cow *async_cow;
1147 struct btrfs_root *root;
1148 unsigned long nr_pages;
1149
1150 async_cow = container_of(work, struct async_cow, work);
1151
1152 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001154 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1155 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001156
David Sterbaee863952015-02-16 19:41:40 +01001157 /*
1158 * atomic_sub_return implies a barrier for waitqueue_active
1159 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001160 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001161 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 waitqueue_active(&fs_info->async_submit_wait))
1163 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001164
Chris Masond3977122009-01-05 21:25:51 -05001165 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001166 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001167}
Chris Masonc8b97812008-10-29 14:49:59 -04001168
Chris Mason771ed682008-11-06 22:02:51 -05001169static noinline void async_cow_free(struct btrfs_work *work)
1170{
1171 struct async_cow *async_cow;
1172 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001173 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001174 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001175 kfree(async_cow);
1176}
1177
1178static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1179 u64 start, u64 end, int *page_started,
1180 unsigned long *nr_written)
1181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001183 struct async_cow *async_cow;
1184 struct btrfs_root *root = BTRFS_I(inode)->root;
1185 unsigned long nr_pages;
1186 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001187
Chris Masona3429ab2009-10-08 12:30:20 -04001188 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1189 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001190 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001191 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001192 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001194 async_cow->root = root;
1195 async_cow->locked_page = locked_page;
1196 async_cow->start = start;
1197
Wang Shilongf79707b2014-07-17 11:44:09 +08001198 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001200 cur_end = end;
1201 else
Byongho Leeee221842015-12-15 01:42:10 +09001202 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001203
1204 async_cow->end = cur_end;
1205 INIT_LIST_HEAD(&async_cow->extents);
1206
Liu Bo9e0af232014-08-15 23:36:53 +08001207 btrfs_init_work(&async_cow->work,
1208 btrfs_delalloc_helper,
1209 async_cow_start, async_cow_submit,
1210 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001211
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001212 nr_pages = (cur_end - start + PAGE_SIZE) >>
1213 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001214 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001215
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001216 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001217
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001218 while (atomic_read(&fs_info->async_submit_draining) &&
1219 atomic_read(&fs_info->async_delalloc_pages)) {
1220 wait_event(fs_info->async_submit_wait,
1221 (atomic_read(&fs_info->async_delalloc_pages) ==
1222 0));
Chris Mason771ed682008-11-06 22:02:51 -05001223 }
1224
1225 *nr_written += nr_pages;
1226 start = cur_end + 1;
1227 }
1228 *page_started = 1;
1229 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001230}
1231
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001232static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001233 u64 bytenr, u64 num_bytes)
1234{
1235 int ret;
1236 struct btrfs_ordered_sum *sums;
1237 LIST_HEAD(list);
1238
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001239 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001240 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001241 if (ret == 0 && list_empty(&list))
1242 return 0;
1243
1244 while (!list_empty(&list)) {
1245 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1246 list_del(&sums->list);
1247 kfree(sums);
1248 }
1249 return 1;
1250}
1251
Chris Masond352ac62008-09-29 15:18:18 -04001252/*
1253 * when nowcow writeback call back. This checks for snapshots or COW copies
1254 * of the extents that exist in the file, and COWs the file as required.
1255 *
1256 * If no cow copies or snapshots exist, we write directly to the existing
1257 * blocks on disk
1258 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001259static noinline int run_delalloc_nocow(struct inode *inode,
1260 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001261 u64 start, u64 end, int *page_started, int force,
1262 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001265 struct btrfs_root *root = BTRFS_I(inode)->root;
1266 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001269 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001270 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 u64 cow_start;
1272 u64 cur_offset;
1273 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001274 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 u64 disk_bytenr;
1276 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001277 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001278 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001280 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001281 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 int nocow;
1283 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001284 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001285 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
1287 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001288 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001289 extent_clear_unlock_delalloc(inode, start, end, end,
1290 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001291 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001292 EXTENT_DO_ACCOUNTING |
1293 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001294 PAGE_CLEAR_DIRTY |
1295 PAGE_SET_WRITEBACK |
1296 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001297 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001298 }
Li Zefan82d59022011-04-20 10:33:24 +08001299
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001300 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001301
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 cow_start = (u64)-1;
1303 cur_offset = start;
1304 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001305 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001307 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001308 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1310 leaf = path->nodes[0];
1311 btrfs_item_key_to_cpu(leaf, &found_key,
1312 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001313 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 found_key.type == BTRFS_EXTENT_DATA_KEY)
1315 path->slots[0]--;
1316 }
1317 check_prev = 0;
1318next_slot:
1319 leaf = path->nodes[0];
1320 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1321 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0)
1325 break;
1326 leaf = path->nodes[0];
1327 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001328
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 nocow = 0;
1330 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001333
Filipe Manana1d512cb2015-11-09 00:33:58 +00001334 if (found_key.objectid > ino)
1335 break;
1336 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1337 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1338 path->slots[0]++;
1339 goto next_slot;
1340 }
1341 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 found_key.offset > end)
1343 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 if (found_key.offset > cur_offset) {
1346 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001347 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 goto out_check;
1349 }
Chris Masonc31f8832008-01-08 15:46:31 -05001350
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 fi = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_file_extent_item);
1353 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001354
Josef Bacikcc95bef2013-04-04 14:31:27 -04001355 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001356 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1357 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 extent_end = found_key.offset +
1361 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001362 disk_num_bytes =
1363 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (extent_end <= start) {
1365 path->slots[0]++;
1366 goto next_slot;
1367 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001368 if (disk_bytenr == 0)
1369 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 if (btrfs_file_extent_compression(leaf, fi) ||
1371 btrfs_file_extent_encryption(leaf, fi) ||
1372 btrfs_file_extent_other_encoding(leaf, fi))
1373 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001374 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1375 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001376 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001378 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001379 found_key.offset -
1380 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001381 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 disk_bytenr += cur_offset - found_key.offset;
1384 num_bytes = min(end + 1, extent_end) - cur_offset;
1385 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 * if there are pending snapshots for this root,
1387 * we fall into common COW way.
1388 */
1389 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001390 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001391 if (!err)
1392 goto out_check;
1393 }
1394 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 * force cow if csum exists in the range.
1396 * this ensure that csum for a given extent are
1397 * either valid or do not exist.
1398 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001399 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001400 num_bytes)) {
1401 if (!nolock)
1402 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001403 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001404 }
1405 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1406 if (!nolock)
1407 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001408 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001409 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001410 nocow = 1;
1411 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1412 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001413 btrfs_file_extent_inline_len(leaf,
1414 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001415 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001416 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 } else {
1418 BUG_ON(1);
1419 }
1420out_check:
1421 if (extent_end <= start) {
1422 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001423 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001424 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001425 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 goto next_slot;
1428 }
1429 if (!nocow) {
1430 if (cow_start == (u64)-1)
1431 cow_start = cur_offset;
1432 cur_offset = extent_end;
1433 if (cur_offset > end)
1434 break;
1435 path->slots[0]++;
1436 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001437 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001438
David Sterbab3b4aa72011-04-21 01:20:15 +02001439 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001441 ret = cow_file_range(inode, locked_page,
1442 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001443 end, page_started, nr_written, 1,
1444 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 if (ret) {
1446 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001447 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001448 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001449 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001450 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001454 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001455
Yan Zhengd899e052008-10-30 14:25:28 -04001456 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001457 u64 orig_start = found_key.offset - extent_offset;
1458
1459 em = create_io_em(inode, cur_offset, num_bytes,
1460 orig_start,
1461 disk_bytenr, /* block_start */
1462 num_bytes, /* block_len */
1463 disk_num_bytes, /* orig_block_len */
1464 ram_bytes, BTRFS_COMPRESS_NONE,
1465 BTRFS_ORDERED_PREALLOC);
1466 if (IS_ERR(em)) {
1467 if (!nolock && nocow)
1468 btrfs_end_write_no_snapshoting(root);
1469 if (nocow)
1470 btrfs_dec_nocow_writers(fs_info,
1471 disk_bytenr);
1472 ret = PTR_ERR(em);
1473 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001474 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001475 free_extent_map(em);
1476 }
1477
1478 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001479 type = BTRFS_ORDERED_PREALLOC;
1480 } else {
1481 type = BTRFS_ORDERED_NOCOW;
1482 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001483
1484 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001485 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001486 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001487 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001488 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001489
Yan, Zhengefa56462010-05-16 10:49:59 -04001490 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001491 BTRFS_DATA_RELOC_TREE_OBJECTID)
1492 /*
1493 * Error handled later, as we must prevent
1494 * extent_clear_unlock_delalloc() in error handler
1495 * from freeing metadata of created ordered extent.
1496 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001497 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1498 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001499
Josef Bacikc2790a22013-07-29 11:20:47 -04001500 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001501 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001502 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001503 EXTENT_DELALLOC |
1504 EXTENT_CLEAR_DATA_RESV,
1505 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1506
Wang Shilonge9894fd2014-03-27 11:12:25 +08001507 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001508 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001509 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001510
1511 /*
1512 * btrfs_reloc_clone_csums() error, now we're OK to call error
1513 * handler, as metadata for created ordered extent will only
1514 * be freed by btrfs_finish_ordered_io().
1515 */
1516 if (ret)
1517 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001518 if (cur_offset > end)
1519 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001520 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001521 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001522
Josef Bacik17ca04a2012-05-31 15:58:55 -04001523 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001525 cur_offset = end;
1526 }
1527
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001529 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1530 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001531 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 }
1534
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001535error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001537 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001538 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001539 EXTENT_DELALLOC | EXTENT_DEFRAG |
1540 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1541 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001542 PAGE_SET_WRITEBACK |
1543 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001544 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001545 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001546}
1547
Wang Shilong47059d92014-07-03 18:22:07 +08001548static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1549{
1550
1551 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1552 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1553 return 0;
1554
1555 /*
1556 * @defrag_bytes is a hint value, no spinlock held here,
1557 * if is not zero, it means the file is defragging.
1558 * Force cow if given extent needs to be defragged.
1559 */
1560 if (BTRFS_I(inode)->defrag_bytes &&
1561 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1562 EXTENT_DEFRAG, 0, NULL))
1563 return 1;
1564
1565 return 0;
1566}
1567
Chris Masond352ac62008-09-29 15:18:18 -04001568/*
1569 * extent_io.c call back to do delayed allocation processing
1570 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001571static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001572 u64 start, u64 end, int *page_started,
1573 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001574{
Josef Bacikc6100a42017-05-05 11:57:13 -04001575 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001576 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001577 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001578
Wang Shilong47059d92014-07-03 18:22:07 +08001579 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001580 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001581 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001582 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001583 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001584 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001585 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001586 ret = cow_file_range(inode, locked_page, start, end, end,
1587 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001588 } else {
1589 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1590 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001591 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001592 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001593 }
Qu Wenruo524272602017-03-08 10:25:52 +08001594 if (ret)
1595 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001596 return ret;
1597}
1598
Josef Bacikc6100a42017-05-05 11:57:13 -04001599static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001600 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001601{
Josef Bacikc6100a42017-05-05 11:57:13 -04001602 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001603 u64 size;
1604
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001606 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001607 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001608
Josef Bacikdcab6a32015-02-11 15:08:59 -05001609 size = orig->end - orig->start + 1;
1610 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001611 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 u64 new_size;
1613
1614 /*
Josef Bacikba117212015-03-13 15:01:24 -04001615 * See the explanation in btrfs_merge_extent_hook, the same
1616 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001617 */
1618 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001619 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001620 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001621 num_extents += count_max_extents(new_size);
1622 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 return;
1624 }
1625
Josef Bacik9e0baf62011-07-15 15:16:44 +00001626 spin_lock(&BTRFS_I(inode)->lock);
1627 BTRFS_I(inode)->outstanding_extents++;
1628 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629}
1630
1631/*
1632 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1633 * extents so we can keep track of new extents that are just merged onto old
1634 * extents, such as when we are doing sequential writes, so we can properly
1635 * account for the metadata space we'll need.
1636 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001637static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001638 struct extent_state *new,
1639 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640{
Josef Bacikc6100a42017-05-05 11:57:13 -04001641 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001642 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001643 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644
Josef Bacik9ed74f22009-09-11 16:12:44 -04001645 /* not delalloc, ignore it */
1646 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001647 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648
Josef Bacik8461a3d2015-03-13 15:12:08 -04001649 if (new->start > other->start)
1650 new_size = new->end - other->start + 1;
1651 else
1652 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653
1654 /* we're not bigger than the max, unreserve the space and go */
1655 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1656 spin_lock(&BTRFS_I(inode)->lock);
1657 BTRFS_I(inode)->outstanding_extents--;
1658 spin_unlock(&BTRFS_I(inode)->lock);
1659 return;
1660 }
1661
1662 /*
Josef Bacikba117212015-03-13 15:01:24 -04001663 * We have to add up either side to figure out how many extents were
1664 * accounted for before we merged into one big extent. If the number of
1665 * extents we accounted for is <= the amount we need for the new range
1666 * then we can return, otherwise drop. Think of it like this
1667 *
1668 * [ 4k][MAX_SIZE]
1669 *
1670 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1671 * need 2 outstanding extents, on one side we have 1 and the other side
1672 * we have 1 so they are == and we can return. But in this case
1673 *
1674 * [MAX_SIZE+4k][MAX_SIZE+4k]
1675 *
1676 * Each range on their own accounts for 2 extents, but merged together
1677 * they are only 3 extents worth of accounting, so we need to drop in
1678 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001679 */
Josef Bacikba117212015-03-13 15:01:24 -04001680 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001681 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001682 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001683 num_extents += count_max_extents(old_size);
1684 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 return;
1686
Josef Bacik9e0baf62011-07-15 15:16:44 +00001687 spin_lock(&BTRFS_I(inode)->lock);
1688 BTRFS_I(inode)->outstanding_extents--;
1689 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001690}
1691
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1693 struct inode *inode)
1694{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001695 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1696
Miao Xieeb73c1b2013-05-15 07:48:22 +00001697 spin_lock(&root->delalloc_lock);
1698 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1699 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1700 &root->delalloc_inodes);
1701 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1702 &BTRFS_I(inode)->runtime_flags);
1703 root->nr_delalloc_inodes++;
1704 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001705 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706 BUG_ON(!list_empty(&root->delalloc_root));
1707 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001708 &fs_info->delalloc_roots);
1709 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710 }
1711 }
1712 spin_unlock(&root->delalloc_lock);
1713}
1714
1715static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001716 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719
Miao Xieeb73c1b2013-05-15 07:48:22 +00001720 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001721 if (!list_empty(&inode->delalloc_inodes)) {
1722 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001724 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 root->nr_delalloc_inodes--;
1726 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001727 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 BUG_ON(list_empty(&root->delalloc_root));
1729 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001730 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 }
1732 }
1733 spin_unlock(&root->delalloc_lock);
1734}
1735
Chris Masond352ac62008-09-29 15:18:18 -04001736/*
1737 * extent_io.c set_bit_hook, used to track delayed allocation
1738 * bytes in this file, and to maintain the list of inodes that
1739 * have pending delalloc work to be done.
1740 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001741static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001742 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001743{
Josef Bacikc6100a42017-05-05 11:57:13 -04001744 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001745
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1747
Wang Shilong47059d92014-07-03 18:22:07 +08001748 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1749 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001750 /*
1751 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001752 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001753 * bit, which is only set or cleared with irqs on
1754 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001755 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001756 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001757 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001758 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001759
Josef Bacik9e0baf62011-07-15 15:16:44 +00001760 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001762 } else {
1763 spin_lock(&BTRFS_I(inode)->lock);
1764 BTRFS_I(inode)->outstanding_extents++;
1765 spin_unlock(&BTRFS_I(inode)->lock);
1766 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001767
Josef Bacik6a3891c2015-03-16 17:38:52 -04001768 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001770 return;
1771
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001772 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1773 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001776 if (*bits & EXTENT_DEFRAG)
1777 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001778 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001779 &BTRFS_I(inode)->runtime_flags))
1780 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001781 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001782 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001783
1784 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1785 (*bits & EXTENT_DELALLOC_NEW)) {
1786 spin_lock(&BTRFS_I(inode)->lock);
1787 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1788 state->start;
1789 spin_unlock(&BTRFS_I(inode)->lock);
1790 }
Chris Mason291d6732008-01-29 15:55:23 -05001791}
1792
Chris Masond352ac62008-09-29 15:18:18 -04001793/*
1794 * extent_io.c clear_bit_hook, see set_bit_hook for why
1795 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001796static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001797 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001798 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001799{
Josef Bacikc6100a42017-05-05 11:57:13 -04001800 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001802 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001803 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001804
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001805 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001806 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001807 inode->defrag_bytes -= len;
1808 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001809
Chris Mason75eff682008-12-15 15:54:40 -05001810 /*
1811 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001812 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001813 * bit, which is only set or cleared with irqs on
1814 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001815 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001817 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001818
Josef Bacik9e0baf62011-07-15 15:16:44 +00001819 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001821 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 spin_lock(&inode->lock);
1823 inode->outstanding_extents -= num_extents;
1824 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001825 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001826
Josef Bacikb6d08f02013-09-27 14:57:43 -04001827 /*
1828 * We don't reserve metadata space for space cache inodes so we
1829 * don't need to call dellalloc_release_metadata if there is an
1830 * error.
1831 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001832 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001833 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834 btrfs_delalloc_release_metadata(inode, len);
1835
Josef Bacik6a3891c2015-03-16 17:38:52 -04001836 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001837 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001838 return;
1839
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1841 do_list && !(state->state & EXTENT_NORESERVE) &&
1842 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001843 btrfs_free_reserved_data_space_noquota(
1844 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001845 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001846
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1848 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001849 spin_lock(&inode->lock);
1850 inode->delalloc_bytes -= len;
1851 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001852 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001853 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001854 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001856 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001857
1858 if ((state->state & EXTENT_DELALLOC_NEW) &&
1859 (*bits & EXTENT_DELALLOC_NEW)) {
1860 spin_lock(&inode->lock);
1861 ASSERT(inode->new_delalloc_bytes >= len);
1862 inode->new_delalloc_bytes -= len;
1863 spin_unlock(&inode->lock);
1864 }
Chris Mason291d6732008-01-29 15:55:23 -05001865}
1866
Chris Masond352ac62008-09-29 15:18:18 -04001867/*
1868 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1869 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001870 *
1871 * return 1 if page cannot be merged to bio
1872 * return 0 if page can be merged to bio
1873 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001874 */
Mike Christie81a75f672016-06-05 14:31:54 -05001875int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001876 size_t size, struct bio *bio,
1877 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001878{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 struct inode *inode = page->mapping->host;
1880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001881 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001882 u64 length = 0;
1883 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001884 int ret;
1885
Chris Mason771ed682008-11-06 22:02:51 -05001886 if (bio_flags & EXTENT_BIO_COMPRESSED)
1887 return 0;
1888
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1892 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001893 if (ret < 0)
1894 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001895 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001896 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001897 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001898}
1899
Chris Masond352ac62008-09-29 15:18:18 -04001900/*
1901 * in order to insert checksums into the metadata in large chunks,
1902 * we wait until bio submission time. All the pages in the bio are
1903 * checksummed and sums are attached onto the ordered extent record.
1904 *
1905 * At IO completion time the cums attached on the ordered extent record
1906 * are inserted into the btree
1907 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001908static int __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001909 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001910 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001911{
Josef Bacikc6100a42017-05-05 11:57:13 -04001912 struct inode *inode = private_data;
Chris Mason065631f2008-02-20 12:07:25 -05001913 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001914
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001915 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001917 return 0;
1918}
Chris Masone0156402008-04-16 11:15:20 -04001919
Chris Mason4a69a412008-11-06 22:03:00 -05001920/*
1921 * in order to insert checksums into the metadata in large chunks,
1922 * we wait until bio submission time. All the pages in the bio are
1923 * checksummed and sums are attached onto the ordered extent record.
1924 *
1925 * At IO completion time the cums attached on the ordered extent record
1926 * are inserted into the btree
1927 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001928static int __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001929 int mirror_num, unsigned long bio_flags,
1930 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001931{
Josef Bacikc6100a42017-05-05 11:57:13 -04001932 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001934 int ret;
1935
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001936 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001937 if (ret) {
1938 bio->bi_error = ret;
1939 bio_endio(bio);
1940 }
Stefan Behrens61891922012-11-05 18:51:52 +01001941 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001942}
1943
Chris Masond352ac62008-09-29 15:18:18 -04001944/*
Chris Masoncad321a2008-12-17 14:51:42 -05001945 * extent_io.c submission hook. This does the right thing for csum calculation
1946 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001947 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001948static int btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1949 int mirror_num, unsigned long bio_flags,
1950 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001951{
Josef Bacikc6100a42017-05-05 11:57:13 -04001952 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301955 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001956 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001957 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001958 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001959
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001960 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001961
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001962 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301963 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001964
Mike Christie37226b22016-06-05 14:31:52 -05001965 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001966 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001967 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001968 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001969
Chris Masond20f7042008-12-08 16:58:54 -05001970 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001971 ret = btrfs_submit_compressed_read(inode, bio,
1972 mirror_num,
1973 bio_flags);
1974 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001975 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001976 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001977 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001978 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001979 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001980 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001981 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001982 /* csum items have already been cloned */
1983 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1984 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001985 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001986 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1987 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 __btrfs_submit_bio_start,
1989 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001990 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001991 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001992 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001993 if (ret)
1994 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001995 }
1996
Chris Mason0b86a832008-03-24 15:01:56 -04001997mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001998 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001999
2000out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002001 if (ret < 0) {
2002 bio->bi_error = ret;
2003 bio_endio(bio);
2004 }
Stefan Behrens61891922012-11-05 18:51:52 +01002005 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002006}
Chris Mason6885f302008-02-20 16:11:05 -05002007
Chris Masond352ac62008-09-29 15:18:18 -04002008/*
2009 * given a list of ordered sums record them in the inode. This happens
2010 * at IO completion time based on sums calculated at bio submission time.
2011 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002012static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002013 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002014{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015 struct btrfs_ordered_sum *sum;
2016
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002017 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002018 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002019 btrfs_csum_file_blocks(trans,
2020 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002021 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022 }
2023 return 0;
2024}
2025
Josef Bacik2ac55d42010-02-03 19:33:23 +00002026int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002027 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002028{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002029 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002030 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002031 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002032}
2033
Chris Masond352ac62008-09-29 15:18:18 -04002034/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002035struct btrfs_writepage_fixup {
2036 struct page *page;
2037 struct btrfs_work work;
2038};
2039
Christoph Hellwigb2950862008-12-02 09:54:17 -05002040static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002041{
2042 struct btrfs_writepage_fixup *fixup;
2043 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002044 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002045 struct page *page;
2046 struct inode *inode;
2047 u64 page_start;
2048 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002049 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002050
2051 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2052 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002053again:
Chris Mason247e7432008-07-17 12:53:51 -04002054 lock_page(page);
2055 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2056 ClearPageChecked(page);
2057 goto out_page;
2058 }
2059
2060 inode = page->mapping->host;
2061 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002063
David Sterbaff13db42015-12-03 14:30:40 +01002064 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002065 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002066
2067 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002068 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002069 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002070
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002071 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002073 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002074 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2075 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002076 unlock_page(page);
2077 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002078 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002079 goto again;
2080 }
Chris Mason247e7432008-07-17 12:53:51 -04002081
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002082 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002083 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002084 if (ret) {
2085 mapping_set_error(page->mapping, ret);
2086 end_extent_writepage(page, ret, page_start, page_end);
2087 ClearPageChecked(page);
2088 goto out;
2089 }
2090
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002091 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2092 0);
Chris Mason247e7432008-07-17 12:53:51 -04002093 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002094 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002095out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002096 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2097 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002098out_page:
2099 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002101 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002102}
2103
2104/*
2105 * There are a few paths in the higher layers of the kernel that directly
2106 * set the page dirty bit without asking the filesystem if it is a
2107 * good idea. This causes problems because we want to make sure COW
2108 * properly happens and the data=ordered rules are followed.
2109 *
Chris Masonc8b97812008-10-29 14:49:59 -04002110 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002111 * hasn't been properly setup for IO. We kick off an async process
2112 * to fix it up. The async helper will wait for ordered extents, set
2113 * the delalloc bit and make it safe to write the page.
2114 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002115static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002116{
2117 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002119 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002120
Chris Mason8b62b722009-09-02 16:53:46 -04002121 /* this page is properly in the ordered list */
2122 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002123 return 0;
2124
2125 if (PageChecked(page))
2126 return -EAGAIN;
2127
2128 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2129 if (!fixup)
2130 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002131
Chris Mason247e7432008-07-17 12:53:51 -04002132 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002133 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002134 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2135 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002136 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002138 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002139}
2140
Yan Zhengd899e052008-10-30 14:25:28 -04002141static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2142 struct inode *inode, u64 file_pos,
2143 u64 disk_bytenr, u64 disk_num_bytes,
2144 u64 num_bytes, u64 ram_bytes,
2145 u8 compression, u8 encryption,
2146 u16 other_encoding, int extent_type)
2147{
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_file_extent_item *fi;
2150 struct btrfs_path *path;
2151 struct extent_buffer *leaf;
2152 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002153 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002154 int ret;
2155
2156 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002157 if (!path)
2158 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002159
Chris Masona1ed8352009-09-11 12:27:37 -04002160 /*
2161 * we may be replacing one extent in the tree with another.
2162 * The new extent is pinned in the extent map, and we don't want
2163 * to drop it from the cache until it is completely in the btree.
2164 *
2165 * So, tell btrfs_drop_extents to leave this extent in the cache.
2166 * the caller is expected to unpin it and allow it to be merged
2167 * with the others.
2168 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002169 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2170 file_pos + num_bytes, NULL, 0,
2171 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002172 if (ret)
2173 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002174
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002175 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002176 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002177 ins.offset = file_pos;
2178 ins.type = BTRFS_EXTENT_DATA_KEY;
2179
2180 path->leave_spinning = 1;
2181 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2182 sizeof(*fi));
2183 if (ret)
2184 goto out;
2185 }
Yan Zhengd899e052008-10-30 14:25:28 -04002186 leaf = path->nodes[0];
2187 fi = btrfs_item_ptr(leaf, path->slots[0],
2188 struct btrfs_file_extent_item);
2189 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2190 btrfs_set_file_extent_type(leaf, fi, extent_type);
2191 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2192 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2193 btrfs_set_file_extent_offset(leaf, fi, 0);
2194 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2195 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2196 btrfs_set_file_extent_compression(leaf, fi, compression);
2197 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2198 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002199
Yan Zhengd899e052008-10-30 14:25:28 -04002200 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002201 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002202
2203 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002204
2205 ins.objectid = disk_bytenr;
2206 ins.offset = disk_num_bytes;
2207 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002208 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002209 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002210 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002211 * Release the reserved range from inode dirty range map, as it is
2212 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002213 */
2214 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002215out:
Yan Zhengd899e052008-10-30 14:25:28 -04002216 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002217
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002218 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002219}
2220
Liu Bo38c227d2013-01-29 03:18:40 +00002221/* snapshot-aware defrag */
2222struct sa_defrag_extent_backref {
2223 struct rb_node node;
2224 struct old_sa_defrag_extent *old;
2225 u64 root_id;
2226 u64 inum;
2227 u64 file_pos;
2228 u64 extent_offset;
2229 u64 num_bytes;
2230 u64 generation;
2231};
2232
2233struct old_sa_defrag_extent {
2234 struct list_head list;
2235 struct new_sa_defrag_extent *new;
2236
2237 u64 extent_offset;
2238 u64 bytenr;
2239 u64 offset;
2240 u64 len;
2241 int count;
2242};
2243
2244struct new_sa_defrag_extent {
2245 struct rb_root root;
2246 struct list_head head;
2247 struct btrfs_path *path;
2248 struct inode *inode;
2249 u64 file_pos;
2250 u64 len;
2251 u64 bytenr;
2252 u64 disk_len;
2253 u8 compress_type;
2254};
2255
2256static int backref_comp(struct sa_defrag_extent_backref *b1,
2257 struct sa_defrag_extent_backref *b2)
2258{
2259 if (b1->root_id < b2->root_id)
2260 return -1;
2261 else if (b1->root_id > b2->root_id)
2262 return 1;
2263
2264 if (b1->inum < b2->inum)
2265 return -1;
2266 else if (b1->inum > b2->inum)
2267 return 1;
2268
2269 if (b1->file_pos < b2->file_pos)
2270 return -1;
2271 else if (b1->file_pos > b2->file_pos)
2272 return 1;
2273
2274 /*
2275 * [------------------------------] ===> (a range of space)
2276 * |<--->| |<---->| =============> (fs/file tree A)
2277 * |<---------------------------->| ===> (fs/file tree B)
2278 *
2279 * A range of space can refer to two file extents in one tree while
2280 * refer to only one file extent in another tree.
2281 *
2282 * So we may process a disk offset more than one time(two extents in A)
2283 * and locate at the same extent(one extent in B), then insert two same
2284 * backrefs(both refer to the extent in B).
2285 */
2286 return 0;
2287}
2288
2289static void backref_insert(struct rb_root *root,
2290 struct sa_defrag_extent_backref *backref)
2291{
2292 struct rb_node **p = &root->rb_node;
2293 struct rb_node *parent = NULL;
2294 struct sa_defrag_extent_backref *entry;
2295 int ret;
2296
2297 while (*p) {
2298 parent = *p;
2299 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2300
2301 ret = backref_comp(backref, entry);
2302 if (ret < 0)
2303 p = &(*p)->rb_left;
2304 else
2305 p = &(*p)->rb_right;
2306 }
2307
2308 rb_link_node(&backref->node, parent, p);
2309 rb_insert_color(&backref->node, root);
2310}
2311
2312/*
2313 * Note the backref might has changed, and in this case we just return 0.
2314 */
2315static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2316 void *ctx)
2317{
2318 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002319 struct old_sa_defrag_extent *old = ctx;
2320 struct new_sa_defrag_extent *new = old->new;
2321 struct btrfs_path *path = new->path;
2322 struct btrfs_key key;
2323 struct btrfs_root *root;
2324 struct sa_defrag_extent_backref *backref;
2325 struct extent_buffer *leaf;
2326 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002328 int slot;
2329 int ret;
2330 u64 extent_offset;
2331 u64 num_bytes;
2332
2333 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002334 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002335 return 0;
2336
2337 key.objectid = root_id;
2338 key.type = BTRFS_ROOT_ITEM_KEY;
2339 key.offset = (u64)-1;
2340
Liu Bo38c227d2013-01-29 03:18:40 +00002341 root = btrfs_read_fs_root_no_name(fs_info, &key);
2342 if (IS_ERR(root)) {
2343 if (PTR_ERR(root) == -ENOENT)
2344 return 0;
2345 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002346 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002347 inum, offset, root_id);
2348 return PTR_ERR(root);
2349 }
2350
2351 key.objectid = inum;
2352 key.type = BTRFS_EXTENT_DATA_KEY;
2353 if (offset > (u64)-1 << 32)
2354 key.offset = 0;
2355 else
2356 key.offset = offset;
2357
2358 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302359 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002360 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002361 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002362
2363 while (1) {
2364 cond_resched();
2365
2366 leaf = path->nodes[0];
2367 slot = path->slots[0];
2368
2369 if (slot >= btrfs_header_nritems(leaf)) {
2370 ret = btrfs_next_leaf(root, path);
2371 if (ret < 0) {
2372 goto out;
2373 } else if (ret > 0) {
2374 ret = 0;
2375 goto out;
2376 }
2377 continue;
2378 }
2379
2380 path->slots[0]++;
2381
2382 btrfs_item_key_to_cpu(leaf, &key, slot);
2383
2384 if (key.objectid > inum)
2385 goto out;
2386
2387 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2388 continue;
2389
2390 extent = btrfs_item_ptr(leaf, slot,
2391 struct btrfs_file_extent_item);
2392
2393 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2394 continue;
2395
Liu Boe68afa42013-07-01 22:13:26 +08002396 /*
2397 * 'offset' refers to the exact key.offset,
2398 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2399 * (key.offset - extent_offset).
2400 */
2401 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002402 continue;
2403
Liu Boe68afa42013-07-01 22:13:26 +08002404 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002405 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002406
Liu Bo38c227d2013-01-29 03:18:40 +00002407 if (extent_offset >= old->extent_offset + old->offset +
2408 old->len || extent_offset + num_bytes <=
2409 old->extent_offset + old->offset)
2410 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002411 break;
2412 }
2413
2414 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2415 if (!backref) {
2416 ret = -ENOENT;
2417 goto out;
2418 }
2419
2420 backref->root_id = root_id;
2421 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002422 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002423 backref->num_bytes = num_bytes;
2424 backref->extent_offset = extent_offset;
2425 backref->generation = btrfs_file_extent_generation(leaf, extent);
2426 backref->old = old;
2427 backref_insert(&new->root, backref);
2428 old->count++;
2429out:
2430 btrfs_release_path(path);
2431 WARN_ON(ret);
2432 return ret;
2433}
2434
2435static noinline bool record_extent_backrefs(struct btrfs_path *path,
2436 struct new_sa_defrag_extent *new)
2437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002438 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002439 struct old_sa_defrag_extent *old, *tmp;
2440 int ret;
2441
2442 new->path = path;
2443
2444 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002445 ret = iterate_inodes_from_logical(old->bytenr +
2446 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002447 path, record_one_backref,
2448 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002449 if (ret < 0 && ret != -ENOENT)
2450 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002451
2452 /* no backref to be processed for this extent */
2453 if (!old->count) {
2454 list_del(&old->list);
2455 kfree(old);
2456 }
2457 }
2458
2459 if (list_empty(&new->head))
2460 return false;
2461
2462 return true;
2463}
2464
2465static int relink_is_mergable(struct extent_buffer *leaf,
2466 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002467 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002468{
Liu Bo116e0022013-08-02 16:30:40 +08002469 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002470 return 0;
2471
2472 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2473 return 0;
2474
Liu Bo116e0022013-08-02 16:30:40 +08002475 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2476 return 0;
2477
2478 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002479 btrfs_file_extent_other_encoding(leaf, fi))
2480 return 0;
2481
2482 return 1;
2483}
2484
2485/*
2486 * Note the backref might has changed, and in this case we just return 0.
2487 */
2488static noinline int relink_extent_backref(struct btrfs_path *path,
2489 struct sa_defrag_extent_backref *prev,
2490 struct sa_defrag_extent_backref *backref)
2491{
2492 struct btrfs_file_extent_item *extent;
2493 struct btrfs_file_extent_item *item;
2494 struct btrfs_ordered_extent *ordered;
2495 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002496 struct btrfs_root *root;
2497 struct btrfs_key key;
2498 struct extent_buffer *leaf;
2499 struct old_sa_defrag_extent *old = backref->old;
2500 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002501 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002502 struct inode *inode;
2503 struct extent_state *cached = NULL;
2504 int ret = 0;
2505 u64 start;
2506 u64 len;
2507 u64 lock_start;
2508 u64 lock_end;
2509 bool merge = false;
2510 int index;
2511
2512 if (prev && prev->root_id == backref->root_id &&
2513 prev->inum == backref->inum &&
2514 prev->file_pos + prev->num_bytes == backref->file_pos)
2515 merge = true;
2516
2517 /* step 1: get root */
2518 key.objectid = backref->root_id;
2519 key.type = BTRFS_ROOT_ITEM_KEY;
2520 key.offset = (u64)-1;
2521
Liu Bo38c227d2013-01-29 03:18:40 +00002522 index = srcu_read_lock(&fs_info->subvol_srcu);
2523
2524 root = btrfs_read_fs_root_no_name(fs_info, &key);
2525 if (IS_ERR(root)) {
2526 srcu_read_unlock(&fs_info->subvol_srcu, index);
2527 if (PTR_ERR(root) == -ENOENT)
2528 return 0;
2529 return PTR_ERR(root);
2530 }
Liu Bo38c227d2013-01-29 03:18:40 +00002531
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002532 if (btrfs_root_readonly(root)) {
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534 return 0;
2535 }
2536
Liu Bo38c227d2013-01-29 03:18:40 +00002537 /* step 2: get inode */
2538 key.objectid = backref->inum;
2539 key.type = BTRFS_INODE_ITEM_KEY;
2540 key.offset = 0;
2541
2542 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2543 if (IS_ERR(inode)) {
2544 srcu_read_unlock(&fs_info->subvol_srcu, index);
2545 return 0;
2546 }
2547
2548 srcu_read_unlock(&fs_info->subvol_srcu, index);
2549
2550 /* step 3: relink backref */
2551 lock_start = backref->file_pos;
2552 lock_end = backref->file_pos + backref->num_bytes - 1;
2553 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002554 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002555
2556 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2557 if (ordered) {
2558 btrfs_put_ordered_extent(ordered);
2559 goto out_unlock;
2560 }
2561
2562 trans = btrfs_join_transaction(root);
2563 if (IS_ERR(trans)) {
2564 ret = PTR_ERR(trans);
2565 goto out_unlock;
2566 }
2567
2568 key.objectid = backref->inum;
2569 key.type = BTRFS_EXTENT_DATA_KEY;
2570 key.offset = backref->file_pos;
2571
2572 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2573 if (ret < 0) {
2574 goto out_free_path;
2575 } else if (ret > 0) {
2576 ret = 0;
2577 goto out_free_path;
2578 }
2579
2580 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2581 struct btrfs_file_extent_item);
2582
2583 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2584 backref->generation)
2585 goto out_free_path;
2586
2587 btrfs_release_path(path);
2588
2589 start = backref->file_pos;
2590 if (backref->extent_offset < old->extent_offset + old->offset)
2591 start += old->extent_offset + old->offset -
2592 backref->extent_offset;
2593
2594 len = min(backref->extent_offset + backref->num_bytes,
2595 old->extent_offset + old->offset + old->len);
2596 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2597
2598 ret = btrfs_drop_extents(trans, root, inode, start,
2599 start + len, 1);
2600 if (ret)
2601 goto out_free_path;
2602again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002603 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002604 key.type = BTRFS_EXTENT_DATA_KEY;
2605 key.offset = start;
2606
Liu Boa09a0a72013-03-11 09:20:58 +00002607 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002608 if (merge) {
2609 struct btrfs_file_extent_item *fi;
2610 u64 extent_len;
2611 struct btrfs_key found_key;
2612
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002613 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002614 if (ret < 0)
2615 goto out_free_path;
2616
2617 path->slots[0]--;
2618 leaf = path->nodes[0];
2619 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2620
2621 fi = btrfs_item_ptr(leaf, path->slots[0],
2622 struct btrfs_file_extent_item);
2623 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2624
Liu Bo116e0022013-08-02 16:30:40 +08002625 if (extent_len + found_key.offset == start &&
2626 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002627 btrfs_set_file_extent_num_bytes(leaf, fi,
2628 extent_len + len);
2629 btrfs_mark_buffer_dirty(leaf);
2630 inode_add_bytes(inode, len);
2631
2632 ret = 1;
2633 goto out_free_path;
2634 } else {
2635 merge = false;
2636 btrfs_release_path(path);
2637 goto again;
2638 }
2639 }
2640
2641 ret = btrfs_insert_empty_item(trans, root, path, &key,
2642 sizeof(*extent));
2643 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002644 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002645 goto out_free_path;
2646 }
2647
2648 leaf = path->nodes[0];
2649 item = btrfs_item_ptr(leaf, path->slots[0],
2650 struct btrfs_file_extent_item);
2651 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2652 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2653 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2654 btrfs_set_file_extent_num_bytes(leaf, item, len);
2655 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2656 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2657 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2658 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2659 btrfs_set_file_extent_encryption(leaf, item, 0);
2660 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2661
2662 btrfs_mark_buffer_dirty(leaf);
2663 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002664 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002665
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002666 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002667 new->disk_len, 0,
2668 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002669 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002670 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002671 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002672 goto out_free_path;
2673 }
2674
2675 ret = 1;
2676out_free_path:
2677 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002678 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002679 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002680out_unlock:
2681 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2682 &cached, GFP_NOFS);
2683 iput(inode);
2684 return ret;
2685}
2686
Liu Bo6f519562013-10-29 10:45:05 +08002687static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2688{
2689 struct old_sa_defrag_extent *old, *tmp;
2690
2691 if (!new)
2692 return;
2693
2694 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002695 kfree(old);
2696 }
2697 kfree(new);
2698}
2699
Liu Bo38c227d2013-01-29 03:18:40 +00002700static void relink_file_extents(struct new_sa_defrag_extent *new)
2701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002702 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002703 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002704 struct sa_defrag_extent_backref *backref;
2705 struct sa_defrag_extent_backref *prev = NULL;
2706 struct inode *inode;
2707 struct btrfs_root *root;
2708 struct rb_node *node;
2709 int ret;
2710
2711 inode = new->inode;
2712 root = BTRFS_I(inode)->root;
2713
2714 path = btrfs_alloc_path();
2715 if (!path)
2716 return;
2717
2718 if (!record_extent_backrefs(path, new)) {
2719 btrfs_free_path(path);
2720 goto out;
2721 }
2722 btrfs_release_path(path);
2723
2724 while (1) {
2725 node = rb_first(&new->root);
2726 if (!node)
2727 break;
2728 rb_erase(node, &new->root);
2729
2730 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2731
2732 ret = relink_extent_backref(path, prev, backref);
2733 WARN_ON(ret < 0);
2734
2735 kfree(prev);
2736
2737 if (ret == 1)
2738 prev = backref;
2739 else
2740 prev = NULL;
2741 cond_resched();
2742 }
2743 kfree(prev);
2744
2745 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002746out:
Liu Bo6f519562013-10-29 10:45:05 +08002747 free_sa_defrag_extent(new);
2748
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002749 atomic_dec(&fs_info->defrag_running);
2750 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002751}
2752
2753static struct new_sa_defrag_extent *
2754record_old_file_extents(struct inode *inode,
2755 struct btrfs_ordered_extent *ordered)
2756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002758 struct btrfs_root *root = BTRFS_I(inode)->root;
2759 struct btrfs_path *path;
2760 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002761 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002762 struct new_sa_defrag_extent *new;
2763 int ret;
2764
2765 new = kmalloc(sizeof(*new), GFP_NOFS);
2766 if (!new)
2767 return NULL;
2768
2769 new->inode = inode;
2770 new->file_pos = ordered->file_offset;
2771 new->len = ordered->len;
2772 new->bytenr = ordered->start;
2773 new->disk_len = ordered->disk_len;
2774 new->compress_type = ordered->compress_type;
2775 new->root = RB_ROOT;
2776 INIT_LIST_HEAD(&new->head);
2777
2778 path = btrfs_alloc_path();
2779 if (!path)
2780 goto out_kfree;
2781
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002782 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002783 key.type = BTRFS_EXTENT_DATA_KEY;
2784 key.offset = new->file_pos;
2785
2786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2787 if (ret < 0)
2788 goto out_free_path;
2789 if (ret > 0 && path->slots[0] > 0)
2790 path->slots[0]--;
2791
2792 /* find out all the old extents for the file range */
2793 while (1) {
2794 struct btrfs_file_extent_item *extent;
2795 struct extent_buffer *l;
2796 int slot;
2797 u64 num_bytes;
2798 u64 offset;
2799 u64 end;
2800 u64 disk_bytenr;
2801 u64 extent_offset;
2802
2803 l = path->nodes[0];
2804 slot = path->slots[0];
2805
2806 if (slot >= btrfs_header_nritems(l)) {
2807 ret = btrfs_next_leaf(root, path);
2808 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002809 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002810 else if (ret > 0)
2811 break;
2812 continue;
2813 }
2814
2815 btrfs_item_key_to_cpu(l, &key, slot);
2816
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002817 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002818 break;
2819 if (key.type != BTRFS_EXTENT_DATA_KEY)
2820 break;
2821 if (key.offset >= new->file_pos + new->len)
2822 break;
2823
2824 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2825
2826 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2827 if (key.offset + num_bytes < new->file_pos)
2828 goto next;
2829
2830 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2831 if (!disk_bytenr)
2832 goto next;
2833
2834 extent_offset = btrfs_file_extent_offset(l, extent);
2835
2836 old = kmalloc(sizeof(*old), GFP_NOFS);
2837 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002838 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002839
2840 offset = max(new->file_pos, key.offset);
2841 end = min(new->file_pos + new->len, key.offset + num_bytes);
2842
2843 old->bytenr = disk_bytenr;
2844 old->extent_offset = extent_offset;
2845 old->offset = offset - key.offset;
2846 old->len = end - offset;
2847 old->new = new;
2848 old->count = 0;
2849 list_add_tail(&old->list, &new->head);
2850next:
2851 path->slots[0]++;
2852 cond_resched();
2853 }
2854
2855 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002856 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002857
2858 return new;
2859
Liu Bo38c227d2013-01-29 03:18:40 +00002860out_free_path:
2861 btrfs_free_path(path);
2862out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002863 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002864 return NULL;
2865}
2866
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002867static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002868 u64 start, u64 len)
2869{
2870 struct btrfs_block_group_cache *cache;
2871
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002873 ASSERT(cache);
2874
2875 spin_lock(&cache->lock);
2876 cache->delalloc_bytes -= len;
2877 spin_unlock(&cache->lock);
2878
2879 btrfs_put_block_group(cache);
2880}
2881
Chris Masond352ac62008-09-29 15:18:18 -04002882/* as ordered data IO finishes, this gets called so we can finish
2883 * an ordered extent if the range of bytes in the file it covers are
2884 * fully written.
2885 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002886static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002887{
Josef Bacik5fd02042012-05-02 14:00:54 -04002888 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002890 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002891 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002892 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002893 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002894 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002895 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002896 int ret = 0;
2897 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002898 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002899 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002900 bool range_locked = false;
2901 bool clear_new_delalloc_bytes = false;
2902
2903 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2904 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2905 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2906 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002907
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002908 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002909
Josef Bacik5fd02042012-05-02 14:00:54 -04002910 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2911 ret = -EIO;
2912 goto out;
2913 }
2914
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002915 btrfs_free_io_failure_record(BTRFS_I(inode),
2916 ordered_extent->file_offset,
2917 ordered_extent->file_offset +
2918 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002919
Josef Bacik77cef2e2013-08-29 13:57:21 -04002920 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2921 truncated = true;
2922 logical_len = ordered_extent->truncated_len;
2923 /* Truncated the entire extent, don't bother adding */
2924 if (!logical_len)
2925 goto out;
2926 }
2927
Yan, Zhengc2167752009-11-12 09:34:21 +00002928 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002929 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002930
2931 /*
2932 * For mwrite(mmap + memset to write) case, we still reserve
2933 * space for NOCOW range.
2934 * As NOCOW won't cause a new delayed ref, just free the space
2935 */
2936 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2937 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002938 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2939 if (nolock)
2940 trans = btrfs_join_transaction_nolock(root);
2941 else
2942 trans = btrfs_join_transaction(root);
2943 if (IS_ERR(trans)) {
2944 ret = PTR_ERR(trans);
2945 trans = NULL;
2946 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002947 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002948 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002949 ret = btrfs_update_inode_fallback(trans, root, inode);
2950 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002951 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002952 goto out;
2953 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002955 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002956 lock_extent_bits(io_tree, ordered_extent->file_offset,
2957 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002958 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002959
Liu Bo38c227d2013-01-29 03:18:40 +00002960 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2961 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002962 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002963 if (ret) {
2964 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002965 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002966 /* the inode is shared */
2967 new = record_old_file_extents(inode, ordered_extent);
2968
2969 clear_extent_bit(io_tree, ordered_extent->file_offset,
2970 ordered_extent->file_offset + ordered_extent->len - 1,
2971 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2972 }
2973
Josef Bacik0cb59c92010-07-02 12:14:14 -04002974 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002975 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002976 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002977 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 if (IS_ERR(trans)) {
2979 ret = PTR_ERR(trans);
2980 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002981 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002982 }
Chris Masona79b7d42014-05-22 16:18:52 -07002983
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002984 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002985
Chris Masonc8b97812008-10-29 14:49:59 -04002986 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002987 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002988 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002989 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002990 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002991 ordered_extent->file_offset,
2992 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002993 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002994 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002996 ret = insert_reserved_file_extent(trans, inode,
2997 ordered_extent->file_offset,
2998 ordered_extent->start,
2999 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003000 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003001 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003002 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003003 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003004 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003005 ordered_extent->start,
3006 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003007 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003008 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3009 ordered_extent->file_offset, ordered_extent->len,
3010 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003011 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003012 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003013 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003014 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003015
David Sterbadf9f6282017-02-10 19:35:37 +01003016 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003017
Josef Bacik6c760c02012-11-09 10:53:21 -05003018 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3019 ret = btrfs_update_inode_fallback(trans, root, inode);
3020 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003021 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003022 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003023 }
3024 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003025out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003026 if (range_locked || clear_new_delalloc_bytes) {
3027 unsigned int clear_bits = 0;
3028
3029 if (range_locked)
3030 clear_bits |= EXTENT_LOCKED;
3031 if (clear_new_delalloc_bytes)
3032 clear_bits |= EXTENT_DELALLOC_NEW;
3033 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3034 ordered_extent->file_offset,
3035 ordered_extent->file_offset +
3036 ordered_extent->len - 1,
3037 clear_bits,
3038 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3039 0, &cached_state, GFP_NOFS);
3040 }
3041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003043 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3044 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003045 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003046 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003047
Josef Bacik77cef2e2013-08-29 13:57:21 -04003048 if (ret || truncated) {
3049 u64 start, end;
3050
3051 if (truncated)
3052 start = ordered_extent->file_offset + logical_len;
3053 else
3054 start = ordered_extent->file_offset;
3055 end = ordered_extent->file_offset + ordered_extent->len - 1;
3056 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3057
3058 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003059 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003060
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003061 /*
3062 * If the ordered extent had an IOERR or something else went
3063 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003064 * back to the allocator. We only free the extent in the
3065 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003066 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003067 if ((ret || !logical_len) &&
3068 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003069 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003070 btrfs_free_reserved_extent(fs_info,
3071 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003072 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003073 }
3074
3075
Josef Bacik5fd02042012-05-02 14:00:54 -04003076 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003077 * This needs to be done to make sure anybody waiting knows we are done
3078 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003079 */
3080 btrfs_remove_ordered_extent(inode, ordered_extent);
3081
Liu Bo38c227d2013-01-29 03:18:40 +00003082 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003083 if (new) {
3084 if (ret) {
3085 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003086 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003087 } else {
3088 relink_file_extents(new);
3089 }
3090 }
Liu Bo38c227d2013-01-29 03:18:40 +00003091
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092 /* once for us */
3093 btrfs_put_ordered_extent(ordered_extent);
3094 /* once for the tree */
3095 btrfs_put_ordered_extent(ordered_extent);
3096
Josef Bacik5fd02042012-05-02 14:00:54 -04003097 return ret;
3098}
3099
3100static void finish_ordered_fn(struct btrfs_work *work)
3101{
3102 struct btrfs_ordered_extent *ordered_extent;
3103 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3104 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003105}
3106
David Sterbac3988d62017-02-17 15:18:32 +01003107static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003108 struct extent_state *state, int uptodate)
3109{
Josef Bacik5fd02042012-05-02 14:00:54 -04003110 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003111 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003112 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003113 struct btrfs_workqueue *wq;
3114 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003115
liubo1abe9b82011-03-24 11:18:59 +00003116 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3117
Chris Mason8b62b722009-09-02 16:53:46 -04003118 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003119 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3120 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003121 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003122
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003123 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003124 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003125 func = btrfs_freespace_write_helper;
3126 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003127 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003128 func = btrfs_endio_write_helper;
3129 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003130
Liu Bo9e0af232014-08-15 23:36:53 +08003131 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3132 NULL);
3133 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003134}
3135
Miao Xiedc380ae2014-09-12 18:43:55 +08003136static int __readpage_endio_check(struct inode *inode,
3137 struct btrfs_io_bio *io_bio,
3138 int icsum, struct page *page,
3139 int pgoff, u64 start, size_t len)
3140{
3141 char *kaddr;
3142 u32 csum_expected;
3143 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003144
3145 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3146
3147 kaddr = kmap_atomic(page);
3148 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003149 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003150 if (csum != csum_expected)
3151 goto zeroit;
3152
3153 kunmap_atomic(kaddr);
3154 return 0;
3155zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003156 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003157 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003158 memset(kaddr + pgoff, 1, len);
3159 flush_dcache_page(page);
3160 kunmap_atomic(kaddr);
3161 if (csum_expected == 0)
3162 return 0;
3163 return -EIO;
3164}
3165
Chris Masond352ac62008-09-29 15:18:18 -04003166/*
Chris Masond352ac62008-09-29 15:18:18 -04003167 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003168 * if there's a match, we allow the bio to finish. If not, the code in
3169 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003170 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003171static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3172 u64 phy_offset, struct page *page,
3173 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003174{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003175 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003176 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003177 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003178 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003179
Chris Masond20f7042008-12-08 16:58:54 -05003180 if (PageChecked(page)) {
3181 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003182 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003183 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003184
3185 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003186 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003187
Yan Zheng17d217f2008-12-12 10:03:38 -05003188 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003189 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003190 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003191 return 0;
3192 }
3193
Miao Xiefacc8a222013-07-25 19:22:34 +08003194 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003195 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3196 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003197}
Chris Masonb888db22007-08-27 16:49:44 -04003198
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003199void btrfs_add_delayed_iput(struct inode *inode)
3200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003202 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003203
3204 if (atomic_add_unless(&inode->i_count, -1, 1))
3205 return;
3206
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003207 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003208 if (binode->delayed_iput_count == 0) {
3209 ASSERT(list_empty(&binode->delayed_iput));
3210 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3211 } else {
3212 binode->delayed_iput_count++;
3213 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003214 spin_unlock(&fs_info->delayed_iput_lock);
3215}
3216
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003217void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003220 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003221 while (!list_empty(&fs_info->delayed_iputs)) {
3222 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223
David Sterba8089fe62015-11-19 14:15:51 +01003224 inode = list_first_entry(&fs_info->delayed_iputs,
3225 struct btrfs_inode, delayed_iput);
3226 if (inode->delayed_iput_count) {
3227 inode->delayed_iput_count--;
3228 list_move_tail(&inode->delayed_iput,
3229 &fs_info->delayed_iputs);
3230 } else {
3231 list_del_init(&inode->delayed_iput);
3232 }
3233 spin_unlock(&fs_info->delayed_iput_lock);
3234 iput(&inode->vfs_inode);
3235 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236 }
David Sterba8089fe62015-11-19 14:15:51 +01003237 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003238}
3239
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003241 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 * files in the subvolume, it removes orphan item and frees block_rsv
3243 * structure.
3244 */
3245void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3246 struct btrfs_root *root)
3247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003248 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003249 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 int ret;
3251
Josef Bacik8a35d952012-05-23 14:26:42 -04003252 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3254 return;
3255
Josef Bacik90290e12011-12-02 15:44:12 -05003256 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003257 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003258 spin_unlock(&root->orphan_lock);
3259 return;
3260 }
3261
3262 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3263 spin_unlock(&root->orphan_lock);
3264 return;
3265 }
3266
3267 block_rsv = root->orphan_block_rsv;
3268 root->orphan_block_rsv = NULL;
3269 spin_unlock(&root->orphan_lock);
3270
Miao Xie27cdeb72014-04-02 19:51:05 +08003271 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003273 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003275 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003276 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003277 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003278 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3279 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 }
3281
Josef Bacik90290e12011-12-02 15:44:12 -05003282 if (block_rsv) {
3283 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003284 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 }
3286}
3287
3288/*
Josef Bacik7b128762008-07-24 12:17:14 -04003289 * This creates an orphan entry for the given inode in case something goes
3290 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291 *
3292 * NOTE: caller of this function should reserve 5 units of metadata for
3293 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003294 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003295int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3296 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003297{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003298 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3299 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 struct btrfs_block_rsv *block_rsv = NULL;
3301 int reserve = 0;
3302 int insert = 0;
3303 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003304
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003306 block_rsv = btrfs_alloc_block_rsv(fs_info,
3307 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003308 if (!block_rsv)
3309 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003310 }
3311
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 spin_lock(&root->orphan_lock);
3313 if (!root->orphan_block_rsv) {
3314 root->orphan_block_rsv = block_rsv;
3315 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003316 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 block_rsv = NULL;
3318 }
Josef Bacik7b128762008-07-24 12:17:14 -04003319
Josef Bacik8a35d952012-05-23 14:26:42 -04003320 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003321 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322#if 0
3323 /*
3324 * For proper ENOSPC handling, we should do orphan
3325 * cleanup when mounting. But this introduces backward
3326 * compatibility issue.
3327 */
3328 if (!xchg(&root->orphan_item_inserted, 1))
3329 insert = 2;
3330 else
3331 insert = 1;
3332#endif
3333 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003334 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003335 }
Josef Bacik7b128762008-07-24 12:17:14 -04003336
Josef Bacik72ac3c02012-05-23 14:13:11 -04003337 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003338 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003341
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 /* grab metadata reservation from transaction handle */
3343 if (reserve) {
3344 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003345 ASSERT(!ret);
3346 if (ret) {
3347 atomic_dec(&root->orphan_inodes);
3348 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003349 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003350 if (insert)
3351 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003352 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003353 return ret;
3354 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 }
3356
3357 /* insert an orphan item to track this unlinked/truncated file */
3358 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003359 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003360 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003361 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003362 if (reserve) {
3363 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 btrfs_orphan_release_metadata(inode);
3366 }
3367 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003368 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003369 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003370 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003371 return ret;
3372 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003373 }
3374 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 }
3376
3377 /* insert an orphan item to track subvolume contains orphan files */
3378 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003379 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003381 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003382 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003383 return ret;
3384 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 }
3386 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003387}
3388
3389/*
3390 * We have done the truncate/delete so we can go ahead and remove the orphan
3391 * item for this particular inode.
3392 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003393static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003394 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003395{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003396 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003397 int delete_item = 0;
3398 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003399 int ret = 0;
3400
Yan, Zhengd68fc572010-05-16 10:49:58 -04003401 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003402 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003403 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003404 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003405
Josef Bacik72ac3c02012-05-23 14:13:11 -04003406 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003407 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 spin_unlock(&root->orphan_lock);
3410
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003411 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003412 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003413 if (trans)
3414 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003415 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003416 }
Josef Bacik7b128762008-07-24 12:17:14 -04003417
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003418 if (release_rsv)
3419 btrfs_orphan_release_metadata(inode);
3420
Josef Bacik4ef31a42013-08-13 14:10:08 -04003421 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003422}
3423
3424/*
3425 * this cleans up any orphans that may be left on the list from the last use
3426 * of this root.
3427 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003428int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003430 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003431 struct btrfs_path *path;
3432 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003433 struct btrfs_key key, found_key;
3434 struct btrfs_trans_handle *trans;
3435 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003436 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003437 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3438
Yan, Zhengd68fc572010-05-16 10:49:58 -04003439 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003440 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003441
3442 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003443 if (!path) {
3444 ret = -ENOMEM;
3445 goto out;
3446 }
David Sterbae4058b52015-11-27 16:31:35 +01003447 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003448
3449 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003450 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003451 key.offset = (u64)-1;
3452
Josef Bacik7b128762008-07-24 12:17:14 -04003453 while (1) {
3454 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003455 if (ret < 0)
3456 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003457
3458 /*
3459 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003460 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003461 * find the key and see if we have stuff that matches
3462 */
3463 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003465 if (path->slots[0] == 0)
3466 break;
3467 path->slots[0]--;
3468 }
3469
3470 /* pull out the item */
3471 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3473
3474 /* make sure the item matches what we want */
3475 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3476 break;
David Sterba962a2982014-06-04 18:41:45 +02003477 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003478 break;
3479
3480 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003481 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003482
3483 /*
3484 * this is where we are basically btrfs_lookup, without the
3485 * crossing root thing. we store the inode number in the
3486 * offset of the orphan item.
3487 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003488
3489 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003490 btrfs_err(fs_info,
3491 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003492 ret = -EINVAL;
3493 goto out;
3494 }
3495
3496 last_objectid = found_key.offset;
3497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003498 found_key.objectid = found_key.offset;
3499 found_key.type = BTRFS_INODE_ITEM_KEY;
3500 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003501 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303502 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003503 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003504 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003505
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003507 struct btrfs_root *dead_root;
3508 struct btrfs_fs_info *fs_info = root->fs_info;
3509 int is_dead_root = 0;
3510
3511 /*
3512 * this is an orphan in the tree root. Currently these
3513 * could come from 2 sources:
3514 * a) a snapshot deletion in progress
3515 * b) a free space cache inode
3516 * We need to distinguish those two, as the snapshot
3517 * orphan must not get deleted.
3518 * find_dead_roots already ran before us, so if this
3519 * is a snapshot deletion, we should find the root
3520 * in the dead_roots list
3521 */
3522 spin_lock(&fs_info->trans_lock);
3523 list_for_each_entry(dead_root, &fs_info->dead_roots,
3524 root_list) {
3525 if (dead_root->root_key.objectid ==
3526 found_key.objectid) {
3527 is_dead_root = 1;
3528 break;
3529 }
3530 }
3531 spin_unlock(&fs_info->trans_lock);
3532 if (is_dead_root) {
3533 /* prevent this orphan from being found again */
3534 key.offset = found_key.objectid - 1;
3535 continue;
3536 }
3537 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003538 /*
3539 * Inode is already gone but the orphan item is still there,
3540 * kill the orphan item.
3541 */
Filipe Manana67710892016-06-06 11:51:25 +01003542 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 trans = btrfs_start_transaction(root, 1);
3544 if (IS_ERR(trans)) {
3545 ret = PTR_ERR(trans);
3546 goto out;
3547 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003548 btrfs_debug(fs_info, "auto deleting %Lu",
3549 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003550 ret = btrfs_del_orphan_item(trans, root,
3551 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003552 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003553 if (ret)
3554 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003555 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003556 }
Josef Bacik7b128762008-07-24 12:17:14 -04003557
Josef Bacik7b128762008-07-24 12:17:14 -04003558 /*
3559 * add this inode to the orphan list so btrfs_orphan_del does
3560 * the proper thing when we hit it
3561 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003562 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3563 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003564 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003565
Josef Bacik7b128762008-07-24 12:17:14 -04003566 /* if we have links, this was a truncate, lets do that */
3567 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303568 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003569 iput(inode);
3570 continue;
3571 }
Josef Bacik7b128762008-07-24 12:17:14 -04003572 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003573
3574 /* 1 for the orphan item deletion. */
3575 trans = btrfs_start_transaction(root, 1);
3576 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003577 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003578 ret = PTR_ERR(trans);
3579 goto out;
3580 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003581 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003582 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003583 if (ret) {
3584 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003585 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003586 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003587
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003588 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003589 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003590 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003591 } else {
3592 nr_unlink++;
3593 }
3594
3595 /* this will do delete_inode and everything for us */
3596 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003597 if (ret)
3598 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003599 }
Miao Xie3254c872011-11-10 20:45:05 -05003600 /* release the path since we're done with it */
3601 btrfs_release_path(path);
3602
Yan, Zhengd68fc572010-05-16 10:49:58 -04003603 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3604
3605 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003606 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003607 (u64)-1);
3608
Miao Xie27cdeb72014-04-02 19:51:05 +08003609 if (root->orphan_block_rsv ||
3610 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003611 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003612 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003613 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003614 }
Josef Bacik7b128762008-07-24 12:17:14 -04003615
3616 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003617 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003618 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003619 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003620
3621out:
3622 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003624 btrfs_free_path(path);
3625 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003626}
3627
Chris Masond352ac62008-09-29 15:18:18 -04003628/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003629 * very simple check to peek ahead in the leaf looking for xattrs. If we
3630 * don't find any xattrs, we know there can't be any acls.
3631 *
3632 * slot is the slot the inode is in, objectid is the objectid of the inode
3633 */
3634static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003635 int slot, u64 objectid,
3636 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003637{
3638 u32 nritems = btrfs_header_nritems(leaf);
3639 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003640 static u64 xattr_access = 0;
3641 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003642 int scanned = 0;
3643
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003645 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3646 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3647 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3648 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003649 }
3650
Chris Mason46a53cc2009-04-27 11:47:50 -04003651 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003652 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003653 while (slot < nritems) {
3654 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3655
3656 /* we found a different objectid, there must not be acls */
3657 if (found_key.objectid != objectid)
3658 return 0;
3659
3660 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003661 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003662 if (*first_xattr_slot == -1)
3663 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003664 if (found_key.offset == xattr_access ||
3665 found_key.offset == xattr_default)
3666 return 1;
3667 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003668
3669 /*
3670 * we found a key greater than an xattr key, there can't
3671 * be any acls later on
3672 */
3673 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3674 return 0;
3675
3676 slot++;
3677 scanned++;
3678
3679 /*
3680 * it goes inode, inode backrefs, xattrs, extents,
3681 * so if there are a ton of hard links to an inode there can
3682 * be a lot of backrefs. Don't waste time searching too hard,
3683 * this is just an optimization
3684 */
3685 if (scanned >= 8)
3686 break;
3687 }
3688 /* we hit the end of the leaf before we found an xattr or
3689 * something larger than an xattr. We have to assume the inode
3690 * has acls
3691 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 if (*first_xattr_slot == -1)
3693 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003694 return 1;
3695}
3696
3697/*
Chris Masond352ac62008-09-29 15:18:18 -04003698 * read an inode from the btree into the in-memory inode
3699 */
Filipe Manana67710892016-06-06 11:51:25 +01003700static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003703 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003704 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003705 struct btrfs_inode_item *inode_item;
3706 struct btrfs_root *root = BTRFS_I(inode)->root;
3707 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003708 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003709 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003710 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003711 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003712 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003713 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003714
3715 ret = btrfs_fill_inode(inode, &rdev);
3716 if (!ret)
3717 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003718
3719 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003720 if (!path) {
3721 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003722 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003723 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003724
Chris Mason39279cc2007-06-12 06:35:45 -04003725 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003726
Chris Mason39279cc2007-06-12 06:35:45 -04003727 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003728 if (ret) {
3729 if (ret > 0)
3730 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003732 }
Chris Mason39279cc2007-06-12 06:35:45 -04003733
Chris Mason5f39d392007-10-15 16:14:19 -04003734 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003735
3736 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003737 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3740 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003741 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003742 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003743 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3744 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003745 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003746
David Sterbaa937b972014-12-12 17:39:12 +01003747 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3748 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003749
David Sterbaa937b972014-12-12 17:39:12 +01003750 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3751 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003752
David Sterbaa937b972014-12-12 17:39:12 +01003753 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3754 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003755
chandan r9cc97d62012-07-04 12:48:07 +05303756 BTRFS_I(inode)->i_otime.tv_sec =
3757 btrfs_timespec_sec(leaf, &inode_item->otime);
3758 BTRFS_I(inode)->i_otime.tv_nsec =
3759 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003761 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003762 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003763 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3764
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003765 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003766 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003767 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003768 rdev = btrfs_inode_rdev(leaf, inode_item);
3769
Josef Bacikaec74772008-07-24 12:12:38 -04003770 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003771 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003772
3773cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003774 /*
3775 * If we were modified in the current generation and evicted from memory
3776 * and then re-read we need to do a full sync since we don't have any
3777 * idea about which extents were modified before we were evicted from
3778 * cache.
3779 *
3780 * This is required for both inode re-read from disk and delayed inode
3781 * in delayed_nodes_tree.
3782 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003783 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003784 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3785 &BTRFS_I(inode)->runtime_flags);
3786
Filipe Mananabde6c242015-07-24 00:00:19 +01003787 /*
3788 * We don't persist the id of the transaction where an unlink operation
3789 * against the inode was last made. So here we assume the inode might
3790 * have been evicted, and therefore the exact value of last_unlink_trans
3791 * lost, and set it to last_trans to avoid metadata inconsistencies
3792 * between the inode and its parent if the inode is fsync'ed and the log
3793 * replayed. For example, in the scenario:
3794 *
3795 * touch mydir/foo
3796 * ln mydir/foo mydir/bar
3797 * sync
3798 * unlink mydir/bar
3799 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3800 * xfs_io -c fsync mydir/foo
3801 * <power failure>
3802 * mount fs, triggers fsync log replay
3803 *
3804 * We must make sure that when we fsync our inode foo we also log its
3805 * parent inode, otherwise after log replay the parent still has the
3806 * dentry with the "bar" name but our inode foo has a link count of 1
3807 * and doesn't have an inode ref with the name "bar" anymore.
3808 *
3809 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003810 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003811 * transaction commits on fsync if our inode is a directory, or if our
3812 * inode is not a directory, logging its parent unnecessarily.
3813 */
3814 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3815
Miao Xie67de1172013-12-26 13:07:06 +08003816 path->slots[0]++;
3817 if (inode->i_nlink != 1 ||
3818 path->slots[0] >= btrfs_header_nritems(leaf))
3819 goto cache_acl;
3820
3821 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003822 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003823 goto cache_acl;
3824
3825 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3826 if (location.type == BTRFS_INODE_REF_KEY) {
3827 struct btrfs_inode_ref *ref;
3828
3829 ref = (struct btrfs_inode_ref *)ptr;
3830 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3831 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3832 struct btrfs_inode_extref *extref;
3833
3834 extref = (struct btrfs_inode_extref *)ptr;
3835 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3836 extref);
3837 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003838cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003839 /*
3840 * try to precache a NULL acl entry for files that don't have
3841 * any xattrs or acls
3842 */
Li Zefan33345d012011-04-20 10:31:50 +08003843 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003844 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003845 if (first_xattr_slot != -1) {
3846 path->slots[0] = first_xattr_slot;
3847 ret = btrfs_load_inode_props(inode, path);
3848 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003849 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003850 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003851 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003852 root->root_key.objectid, ret);
3853 }
3854 btrfs_free_path(path);
3855
Al Viro72c04902009-06-24 16:58:48 -04003856 if (!maybe_acls)
3857 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003858
Chris Mason39279cc2007-06-12 06:35:45 -04003859 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003860 case S_IFREG:
3861 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003862 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 inode->i_fop = &btrfs_file_operations;
3864 inode->i_op = &btrfs_file_inode_operations;
3865 break;
3866 case S_IFDIR:
3867 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003868 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 break;
3870 case S_IFLNK:
3871 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003872 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003873 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3874 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003875 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003876 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003877 init_special_inode(inode, inode->i_mode, rdev);
3878 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003879 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003880
3881 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003882 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003883
3884make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003885 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003886 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003887 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003888}
3889
Chris Masond352ac62008-09-29 15:18:18 -04003890/*
3891 * given a leaf and an inode, copy the inode fields into the leaf
3892 */
Chris Masone02119d2008-09-05 16:13:11 -04003893static void fill_inode_item(struct btrfs_trans_handle *trans,
3894 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003895 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003896 struct inode *inode)
3897{
Liu Bo51fab692012-12-27 09:01:21 +00003898 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003899
Liu Bo51fab692012-12-27 09:01:21 +00003900 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003901
Liu Bo51fab692012-12-27 09:01:21 +00003902 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3903 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3904 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3905 &token);
3906 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3907 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003908
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003911 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003912 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003913
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003916 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003917 inode->i_mtime.tv_nsec, &token);
3918
David Sterbaa937b972014-12-12 17:39:12 +01003919 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003920 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003921 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003922 inode->i_ctime.tv_nsec, &token);
3923
chandan r9cc97d62012-07-04 12:48:07 +05303924 btrfs_set_token_timespec_sec(leaf, &item->otime,
3925 BTRFS_I(inode)->i_otime.tv_sec, &token);
3926 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3927 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3928
Liu Bo51fab692012-12-27 09:01:21 +00003929 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3930 &token);
3931 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3932 &token);
3933 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3934 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3935 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3936 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3937 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003938}
3939
Chris Masond352ac62008-09-29 15:18:18 -04003940/*
3941 * copy everything in the in-memory inode into the btree.
3942 */
Chris Mason21151332011-11-10 20:39:08 -05003943static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003944 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003945{
3946 struct btrfs_inode_item *inode_item;
3947 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003948 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003949 int ret;
3950
3951 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003952 if (!path)
3953 return -ENOMEM;
3954
Chris Masonb9473432009-03-13 11:00:37 -04003955 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003956 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3957 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003958 if (ret) {
3959 if (ret > 0)
3960 ret = -ENOENT;
3961 goto failed;
3962 }
3963
Chris Mason5f39d392007-10-15 16:14:19 -04003964 leaf = path->nodes[0];
3965 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003966 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003967
Chris Masone02119d2008-09-05 16:13:11 -04003968 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003969 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003970 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003971 ret = 0;
3972failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003973 btrfs_free_path(path);
3974 return ret;
3975}
3976
Chris Masond352ac62008-09-29 15:18:18 -04003977/*
Chris Mason21151332011-11-10 20:39:08 -05003978 * copy everything in the in-memory inode into the btree.
3979 */
3980noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3981 struct btrfs_root *root, struct inode *inode)
3982{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003984 int ret;
3985
3986 /*
3987 * If the inode is a free space inode, we can deadlock during commit
3988 * if we put it into the delayed code.
3989 *
3990 * The data relocation inode should also be directly updated
3991 * without delay
3992 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003993 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003994 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003995 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003996 btrfs_update_root_times(trans, root);
3997
Chris Mason21151332011-11-10 20:39:08 -05003998 ret = btrfs_delayed_update_inode(trans, root, inode);
3999 if (!ret)
4000 btrfs_set_inode_last_trans(trans, inode);
4001 return ret;
4002 }
4003
4004 return btrfs_update_inode_item(trans, root, inode);
4005}
4006
Josef Bacikbe6aef62012-10-22 15:43:12 -04004007noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4008 struct btrfs_root *root,
4009 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004010{
4011 int ret;
4012
4013 ret = btrfs_update_inode(trans, root, inode);
4014 if (ret == -ENOSPC)
4015 return btrfs_update_inode_item(trans, root, inode);
4016 return ret;
4017}
4018
4019/*
Chris Masond352ac62008-09-29 15:18:18 -04004020 * unlink helper that gets used here in inode.c and in the tree logging
4021 * recovery code. It remove a link in a directory with a given name, and
4022 * also drops the back refs in the inode to the directory
4023 */
Al Viro92986792011-03-04 17:14:37 +00004024static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4025 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004026 struct btrfs_inode *dir,
4027 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004028 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004030 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004031 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004032 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004033 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004034 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004035 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004036 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004037 u64 ino = btrfs_ino(inode);
4038 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004039
4040 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004041 if (!path) {
4042 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004043 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004044 }
4045
Chris Masonb9473432009-03-13 11:00:37 -04004046 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004047 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004048 name, name_len, -1);
4049 if (IS_ERR(di)) {
4050 ret = PTR_ERR(di);
4051 goto err;
4052 }
4053 if (!di) {
4054 ret = -ENOENT;
4055 goto err;
4056 }
Chris Mason5f39d392007-10-15 16:14:19 -04004057 leaf = path->nodes[0];
4058 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004059 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004060 if (ret)
4061 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004062 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004063
Miao Xie67de1172013-12-26 13:07:06 +08004064 /*
4065 * If we don't have dir index, we have to get it by looking up
4066 * the inode ref, since we get the inode ref, remove it directly,
4067 * it is unnecessary to do delayed deletion.
4068 *
4069 * But if we have dir index, needn't search inode ref to get it.
4070 * Since the inode ref is close to the inode item, it is better
4071 * that we delay to delete it, and just do this deletion when
4072 * we update the inode item.
4073 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004074 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004075 ret = btrfs_delayed_delete_inode_ref(inode);
4076 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004077 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004078 goto skip_backref;
4079 }
4080 }
4081
Li Zefan33345d012011-04-20 10:31:50 +08004082 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4083 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004084 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004085 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004086 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004087 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004088 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004089 goto err;
4090 }
Miao Xie67de1172013-12-26 13:07:06 +08004091skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004092 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004093 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004094 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 }
Chris Mason39279cc2007-06-12 06:35:45 -04004097
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004098 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4099 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004101 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004102 goto err;
4103 }
Chris Masone02119d2008-09-05 16:13:11 -04004104
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004105 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4106 index);
Chris Mason6418c962010-10-30 07:34:24 -04004107 if (ret == -ENOENT)
4108 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004109 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004110 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004111err:
4112 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004113 if (ret)
4114 goto out;
4115
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004116 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004117 inode_inc_iversion(&inode->vfs_inode);
4118 inode_inc_iversion(&dir->vfs_inode);
4119 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4120 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4121 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004122out:
Chris Mason39279cc2007-06-12 06:35:45 -04004123 return ret;
4124}
4125
Al Viro92986792011-03-04 17:14:37 +00004126int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4127 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004128 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004129 const char *name, int name_len)
4130{
4131 int ret;
4132 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4133 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004134 drop_nlink(&inode->vfs_inode);
4135 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004136 }
4137 return ret;
4138}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139
4140/*
4141 * helper to start transaction for unlink and rmdir.
4142 *
Josef Bacikd52be812013-05-29 14:54:47 -04004143 * unlink and rmdir are special in btrfs, they do not always free space, so
4144 * if we cannot make our reservations the normal way try and see if there is
4145 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4146 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004147 */
Josef Bacikd52be812013-05-29 14:54:47 -04004148static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151
Josef Bacike70bea52011-10-11 14:18:24 -04004152 /*
4153 * 1 for the possible orphan item
4154 * 1 for the dir item
4155 * 1 for the dir index
4156 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004157 * 1 for the inode
4158 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004159 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004160}
4161
Chris Mason39279cc2007-06-12 06:35:45 -04004162static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4163{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004165 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004166 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004167 int ret;
4168
Josef Bacikd52be812013-05-29 14:54:47 -04004169 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004171 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004172
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004173 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4174 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004175
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004176 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4177 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4178 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004179 if (ret)
4180 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004181
Yan, Zhenga22285a2010-05-16 10:48:46 -04004182 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004183 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004184 if (ret)
4185 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004186 }
Josef Bacik7b128762008-07-24 12:17:14 -04004187
Tsutomu Itohb5324022011-07-19 07:27:20 +00004188out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004189 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004190 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 return ret;
4192}
4193
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4195 struct btrfs_root *root,
4196 struct inode *dir, u64 objectid,
4197 const char *name, int name_len)
4198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004199 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200 struct btrfs_path *path;
4201 struct extent_buffer *leaf;
4202 struct btrfs_dir_item *di;
4203 struct btrfs_key key;
4204 u64 index;
4205 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004206 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207
4208 path = btrfs_alloc_path();
4209 if (!path)
4210 return -ENOMEM;
4211
Li Zefan33345d012011-04-20 10:31:50 +08004212 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004214 if (IS_ERR_OR_NULL(di)) {
4215 if (!di)
4216 ret = -ENOENT;
4217 else
4218 ret = PTR_ERR(di);
4219 goto out;
4220 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221
4222 leaf = path->nodes[0];
4223 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4224 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4225 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004226 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004227 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004228 goto out;
4229 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004230 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004232 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4233 root->root_key.objectid, dir_ino,
4234 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004236 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004237 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 goto out;
4239 }
Li Zefan33345d012011-04-20 10:31:50 +08004240 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 if (IS_ERR_OR_NULL(di)) {
4243 if (!di)
4244 ret = -ENOENT;
4245 else
4246 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004247 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 goto out;
4249 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250
4251 leaf = path->nodes[0];
4252 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004253 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004254 index = key.offset;
4255 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004256 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004258 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004260 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 goto out;
4262 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004263
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004264 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004265 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004266 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004267 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004269 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004271 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273}
4274
Chris Mason39279cc2007-06-12 06:35:45 -04004275static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4276{
David Howells2b0143b2015-03-17 22:25:59 +00004277 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004278 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004280 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004281 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004282
David Sterbab3ae2442012-09-13 16:04:34 -06004283 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004284 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004285 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004286 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004287
Josef Bacikd52be812013-05-29 14:54:47 -04004288 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004289 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004290 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004291
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004292 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004293 err = btrfs_unlink_subvol(trans, root, dir,
4294 BTRFS_I(inode)->location.objectid,
4295 dentry->d_name.name,
4296 dentry->d_name.len);
4297 goto out;
4298 }
4299
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004300 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004301 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004302 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004303
Filipe Manana44f714d2016-06-06 16:11:13 +01004304 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4305
Chris Mason39279cc2007-06-12 06:35:45 -04004306 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004307 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4308 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4309 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004310 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004311 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004312 /*
4313 * Propagate the last_unlink_trans value of the deleted dir to
4314 * its parent directory. This is to prevent an unrecoverable
4315 * log tree in the case we do something like this:
4316 * 1) create dir foo
4317 * 2) create snapshot under dir foo
4318 * 3) delete the snapshot
4319 * 4) rmdir foo
4320 * 5) mkdir foo
4321 * 6) fsync foo or some file inside foo
4322 */
4323 if (last_unlink_trans >= trans->transid)
4324 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4325 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004326out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004327 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004328 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004329
Chris Mason39279cc2007-06-12 06:35:45 -04004330 return err;
4331}
4332
Chris Mason28f75a02015-02-04 06:59:29 -08004333static int truncate_space_check(struct btrfs_trans_handle *trans,
4334 struct btrfs_root *root,
4335 u64 bytes_deleted)
4336{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004338 int ret;
4339
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004340 /*
4341 * This is only used to apply pressure to the enospc system, we don't
4342 * intend to use this reservation at all.
4343 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004344 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004345 bytes_deleted *= fs_info->nodesize;
4346 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004347 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004348 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004349 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004350 trans->transid,
4351 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004352 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004353 }
Chris Mason28f75a02015-02-04 06:59:29 -08004354 return ret;
4355
4356}
4357
Filipe Manana0305cd52015-10-16 12:34:25 +01004358static int truncate_inline_extent(struct inode *inode,
4359 struct btrfs_path *path,
4360 struct btrfs_key *found_key,
4361 const u64 item_end,
4362 const u64 new_size)
4363{
4364 struct extent_buffer *leaf = path->nodes[0];
4365 int slot = path->slots[0];
4366 struct btrfs_file_extent_item *fi;
4367 u32 size = (u32)(new_size - found_key->offset);
4368 struct btrfs_root *root = BTRFS_I(inode)->root;
4369
4370 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4371
4372 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4373 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004374 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004375
4376 /*
4377 * Zero out the remaining of the last page of our inline extent,
4378 * instead of directly truncating our inline extent here - that
4379 * would be much more complex (decompressing all the data, then
4380 * compressing the truncated data, which might be bigger than
4381 * the size of the inline extent, resize the extent, etc).
4382 * We release the path because to get the page we might need to
4383 * read the extent item from disk (data not in the page cache).
4384 */
4385 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304386 return btrfs_truncate_block(inode, offset, page_end - offset,
4387 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004388 }
4389
4390 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4391 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004392 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004393
4394 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4395 inode_sub_bytes(inode, item_end + 1 - new_size);
4396
4397 return 0;
4398}
4399
Chris Mason323ac952008-10-01 19:05:46 -04004400/*
Chris Mason39279cc2007-06-12 06:35:45 -04004401 * this can truncate away extent items, csum items and directory items.
4402 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004403 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004404 *
4405 * csum items that cross the new i_size are truncated to the new size
4406 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004407 *
4408 * min_type is the minimum key type to truncate down to. If set to 0, this
4409 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004410 */
Yan, Zheng80825102009-11-12 09:35:36 +00004411int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4412 struct btrfs_root *root,
4413 struct inode *inode,
4414 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004415{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004416 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004418 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004420 struct btrfs_key key;
4421 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004423 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004424 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004425 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004426 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004427 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004428 int found_extent;
4429 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004430 int pending_del_nr = 0;
4431 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004432 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004433 int ret;
4434 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004435 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004436 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004437 bool be_nice = 0;
4438 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004439 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004440
4441 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004442
Chris Mason28ed1342014-12-17 09:41:04 -08004443 /*
4444 * for non-free space inodes and ref cows, we want to back off from
4445 * time to time
4446 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004447 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004448 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4449 be_nice = 1;
4450
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004451 path = btrfs_alloc_path();
4452 if (!path)
4453 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004454 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004455
Josef Bacik5dc562c2012-08-17 13:14:17 -04004456 /*
4457 * We want to drop from the next block forward in case this new size is
4458 * not block aligned since we will be keeping the last block of the
4459 * extent just the way it is.
4460 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004461 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004462 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004463 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004464 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004465 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004466
Miao Xie16cdcec2011-04-22 18:12:22 +08004467 /*
4468 * This function is also used to drop the items in the log tree before
4469 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4470 * it is used to drop the loged items. So we shouldn't kill the delayed
4471 * items.
4472 */
4473 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004474 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004475
Li Zefan33345d012011-04-20 10:31:50 +08004476 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 key.type = (u8)-1;
4479
Chris Mason85e21ba2008-01-29 15:11:36 -05004480search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004481 /*
4482 * with a 16K leaf size and 128MB extents, you can actually queue
4483 * up a huge file in a single leaf. Most of the time that
4484 * bytes_deleted is > 0, it will be huge by the time we get here
4485 */
Byongho Leeee221842015-12-15 01:42:10 +09004486 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004487 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004488 err = -EAGAIN;
4489 goto error;
4490 }
4491 }
4492
4493
Chris Masonb9473432009-03-13 11:00:37 -04004494 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004495 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004496 if (ret < 0) {
4497 err = ret;
4498 goto out;
4499 }
Chris Masond3977122009-01-05 21:25:51 -05004500
Chris Mason85e21ba2008-01-29 15:11:36 -05004501 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004502 /* there are no items in the tree for us to truncate, we're
4503 * done
4504 */
Yan, Zheng80825102009-11-12 09:35:36 +00004505 if (path->slots[0] == 0)
4506 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004507 path->slots[0]--;
4508 }
4509
Chris Masond3977122009-01-05 21:25:51 -05004510 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004511 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004512 leaf = path->nodes[0];
4513 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004514 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004515
Li Zefan33345d012011-04-20 10:31:50 +08004516 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004517 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004518
Chris Mason85e21ba2008-01-29 15:11:36 -05004519 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004520 break;
4521
Chris Mason5f39d392007-10-15 16:14:19 -04004522 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004523 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004524 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004525 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004526 extent_type = btrfs_file_extent_type(leaf, fi);
4527 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004528 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004529 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004530
4531 trace_btrfs_truncate_show_fi_regular(
4532 BTRFS_I(inode), leaf, fi,
4533 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004534 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004535 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004536 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004537
4538 trace_btrfs_truncate_show_fi_inline(
4539 BTRFS_I(inode), leaf, fi, path->slots[0],
4540 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004541 }
Yan008630c2007-11-07 13:31:09 -05004542 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004543 }
Yan, Zheng80825102009-11-12 09:35:36 +00004544 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004545 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004546 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004547 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004548 break;
4549 if (found_key.offset >= new_size)
4550 del_item = 1;
4551 else
4552 del_item = 0;
4553 }
Chris Mason39279cc2007-06-12 06:35:45 -04004554 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004555 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004556 if (found_type != BTRFS_EXTENT_DATA_KEY)
4557 goto delete;
4558
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004559 if (del_item)
4560 last_size = found_key.offset;
4561 else
4562 last_size = new_size;
4563
Chris Mason179e29e2007-11-01 11:28:41 -04004564 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004565 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004566 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004567 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004568 u64 orig_num_bytes =
4569 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004570 extent_num_bytes = ALIGN(new_size -
4571 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004572 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004573 btrfs_set_file_extent_num_bytes(leaf, fi,
4574 extent_num_bytes);
4575 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004576 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004577 if (test_bit(BTRFS_ROOT_REF_COWS,
4578 &root->state) &&
4579 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004580 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004581 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004582 } else {
Chris Masondb945352007-10-15 16:15:53 -04004583 extent_num_bytes =
4584 btrfs_file_extent_disk_num_bytes(leaf,
4585 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004586 extent_offset = found_key.offset -
4587 btrfs_file_extent_offset(leaf, fi);
4588
Chris Mason39279cc2007-06-12 06:35:45 -04004589 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004590 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004591 if (extent_start != 0) {
4592 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004593 if (test_bit(BTRFS_ROOT_REF_COWS,
4594 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004595 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004596 }
4597 }
Chris Mason90692182008-02-08 13:49:28 -05004598 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004599 /*
4600 * we can't truncate inline items that have had
4601 * special encodings
4602 */
4603 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004604 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4605 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004606
4607 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004608 * Need to release path in order to truncate a
4609 * compressed extent. So delete any accumulated
4610 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004611 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004612 if (btrfs_file_extent_compression(leaf, fi) !=
4613 BTRFS_COMPRESS_NONE && pending_del_nr) {
4614 err = btrfs_del_items(trans, root, path,
4615 pending_del_slot,
4616 pending_del_nr);
4617 if (err) {
4618 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004619 err);
4620 goto error;
4621 }
4622 pending_del_nr = 0;
4623 }
4624
4625 err = truncate_inline_extent(inode, path,
4626 &found_key,
4627 item_end,
4628 new_size);
4629 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004630 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004631 goto error;
4632 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004633 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4634 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004635 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004636 }
Chris Mason39279cc2007-06-12 06:35:45 -04004637 }
Chris Mason179e29e2007-11-01 11:28:41 -04004638delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004639 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 if (!pending_del_nr) {
4641 /* no pending yet, add ourselves */
4642 pending_del_slot = path->slots[0];
4643 pending_del_nr = 1;
4644 } else if (pending_del_nr &&
4645 path->slots[0] + 1 == pending_del_slot) {
4646 /* hop on the pending chunk */
4647 pending_del_nr++;
4648 pending_del_slot = path->slots[0];
4649 } else {
Chris Masond3977122009-01-05 21:25:51 -05004650 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004651 }
Chris Mason39279cc2007-06-12 06:35:45 -04004652 } else {
4653 break;
4654 }
Chris Mason28f75a02015-02-04 06:59:29 -08004655 should_throttle = 0;
4656
Miao Xie27cdeb72014-04-02 19:51:05 +08004657 if (found_extent &&
4658 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004660 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004661 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004662 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004663 extent_num_bytes, 0,
4664 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004665 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004666 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004667 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4668 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004669 trans->delayed_ref_updates * 2,
4670 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004671 if (be_nice) {
4672 if (truncate_space_check(trans, root,
4673 extent_num_bytes)) {
4674 should_end = 1;
4675 }
4676 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004677 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004678 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004679 }
Chris Mason39279cc2007-06-12 06:35:45 -04004680 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004681
Yan, Zheng80825102009-11-12 09:35:36 +00004682 if (found_type == BTRFS_INODE_ITEM_KEY)
4683 break;
4684
4685 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004686 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004687 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004688 if (pending_del_nr) {
4689 ret = btrfs_del_items(trans, root, path,
4690 pending_del_slot,
4691 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004693 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004694 goto error;
4695 }
Yan, Zheng80825102009-11-12 09:35:36 +00004696 pending_del_nr = 0;
4697 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004698 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004699 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004700 unsigned long updates = trans->delayed_ref_updates;
4701 if (updates) {
4702 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004703 ret = btrfs_run_delayed_refs(trans,
4704 fs_info,
4705 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004706 if (ret && !err)
4707 err = ret;
4708 }
4709 }
Chris Mason28f75a02015-02-04 06:59:29 -08004710 /*
4711 * if we failed to refill our space rsv, bail out
4712 * and let the transaction restart
4713 */
4714 if (should_end) {
4715 err = -EAGAIN;
4716 goto error;
4717 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004719 } else {
4720 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004721 }
Chris Mason39279cc2007-06-12 06:35:45 -04004722 }
Yan, Zheng80825102009-11-12 09:35:36 +00004723out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004724 if (pending_del_nr) {
4725 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4726 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004728 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004730error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004731 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4732 ASSERT(last_size >= new_size);
4733 if (!err && last_size > new_size)
4734 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004735 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004736 }
Chris Mason28ed1342014-12-17 09:41:04 -08004737
Chris Mason39279cc2007-06-12 06:35:45 -04004738 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004739
Byongho Leeee221842015-12-15 01:42:10 +09004740 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004741 unsigned long updates = trans->delayed_ref_updates;
4742 if (updates) {
4743 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004744 ret = btrfs_run_delayed_refs(trans, fs_info,
4745 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004746 if (ret && !err)
4747 err = ret;
4748 }
4749 }
Yan, Zheng80825102009-11-12 09:35:36 +00004750 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004751}
4752
Chris Masona52d9a82007-08-27 16:49:44 -04004753/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304754 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004755 * @inode - inode that we're zeroing
4756 * @from - the offset to start zeroing
4757 * @len - the length to zero, 0 to zero the entire range respective to the
4758 * offset
4759 * @front - zero up to the offset instead of from the offset on
4760 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304761 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004762 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004763 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304764int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004765 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004766{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004767 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004768 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004769 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4770 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004771 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004772 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004773 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004774 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004776 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004777 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004778 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 u64 block_start;
4780 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004781
Josef Bacik2aaa6652012-08-29 14:27:18 -04004782 if ((offset & (blocksize - 1)) == 0 &&
4783 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004784 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004786 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004788 if (ret)
4789 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004790
Chris Mason211c17f2008-05-15 09:13:45 -04004791again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004792 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004793 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004794 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 round_down(from, blocksize),
4796 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004797 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004798 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004799 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004800
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 block_start = round_down(from, blocksize);
4802 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803
Chris Masona52d9a82007-08-27 16:49:44 -04004804 if (!PageUptodate(page)) {
4805 ret = btrfs_readpage(NULL, page);
4806 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004807 if (page->mapping != mapping) {
4808 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004809 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004810 goto again;
4811 }
Chris Masona52d9a82007-08-27 16:49:44 -04004812 if (!PageUptodate(page)) {
4813 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004814 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004815 }
4816 }
Chris Mason211c17f2008-05-15 09:13:45 -04004817 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004818
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 set_page_extent_mapped(page);
4821
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004823 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004825 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004827 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004828 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 btrfs_put_ordered_extent(ordered);
4830 goto again;
4831 }
4832
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004834 EXTENT_DIRTY | EXTENT_DELALLOC |
4835 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004836 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004837
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304838 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004839 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004840 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004843 goto out_unlock;
4844 }
4845
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004847 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304848 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004849 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004850 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 memset(kaddr + (block_start - page_offset(page)),
4852 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004853 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 memset(kaddr + (block_start - page_offset(page)) + offset,
4855 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 flush_dcache_page(page);
4857 kunmap(page);
4858 }
Chris Mason247e7432008-07-17 12:53:51 -04004859 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004860 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304861 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004862 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004863
Chris Mason89642222008-07-24 09:41:53 -04004864out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004865 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304866 btrfs_delalloc_release_space(inode, block_start,
4867 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004868 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004869 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004870out:
4871 return ret;
4872}
4873
Josef Bacik16e75492013-10-22 12:18:51 -04004874static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4875 u64 offset, u64 len)
4876{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004878 struct btrfs_trans_handle *trans;
4879 int ret;
4880
4881 /*
4882 * Still need to make sure the inode looks like it's been updated so
4883 * that any holes get logged if we fsync.
4884 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4886 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004887 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4888 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4889 return 0;
4890 }
4891
4892 /*
4893 * 1 - for the one we're dropping
4894 * 1 - for the one we're adding
4895 * 1 - for updating the inode.
4896 */
4897 trans = btrfs_start_transaction(root, 3);
4898 if (IS_ERR(trans))
4899 return PTR_ERR(trans);
4900
4901 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4902 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004903 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004904 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004905 return ret;
4906 }
4907
David Sterbaf85b7372017-01-20 14:54:07 +01004908 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4909 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004910 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004911 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004912 else
4913 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004914 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004915 return ret;
4916}
4917
Josef Bacik695a0d02011-03-04 15:46:53 -05004918/*
4919 * This function puts in dummy file extents for the area we're creating a hole
4920 * for. So if we are truncating this file to a larger size we need to insert
4921 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4922 * the range between oldsize and size
4923 */
Josef Bacika41ad392011-01-31 15:30:16 -05004924int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004927 struct btrfs_root *root = BTRFS_I(inode)->root;
4928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004929 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004930 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004931 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004932 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4933 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004934 u64 last_byte;
4935 u64 cur_offset;
4936 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004937 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004938
Josef Bacika71754f2013-06-17 17:14:39 -04004939 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304940 * If our size started in the middle of a block we need to zero out the
4941 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004942 * expose stale data.
4943 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304944 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004945 if (err)
4946 return err;
4947
Yan Zheng9036c102008-10-30 14:19:41 -04004948 if (size <= hole_start)
4949 return 0;
4950
Yan Zheng9036c102008-10-30 14:19:41 -04004951 while (1) {
4952 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004953
David Sterbaff13db42015-12-03 14:30:40 +01004954 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004955 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004956 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004957 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004958 if (!ordered)
4959 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004960 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4961 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004962 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004963 btrfs_put_ordered_extent(ordered);
4964 }
4965
Yan Zheng9036c102008-10-30 14:19:41 -04004966 cur_offset = hole_start;
4967 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004968 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004969 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004970 if (IS_ERR(em)) {
4971 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004972 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004973 break;
4974 }
Yan Zheng9036c102008-10-30 14:19:41 -04004975 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004976 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004977 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004978 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004979 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004980
Josef Bacik16e75492013-10-22 12:18:51 -04004981 err = maybe_insert_hole(root, inode, cur_offset,
4982 hole_size);
4983 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004984 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004985 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 cur_offset + hole_size - 1, 0);
4987 hole_em = alloc_extent_map();
4988 if (!hole_em) {
4989 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4990 &BTRFS_I(inode)->runtime_flags);
4991 goto next;
4992 }
4993 hole_em->start = cur_offset;
4994 hole_em->len = hole_size;
4995 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004996
Josef Bacik5dc562c2012-08-17 13:14:17 -04004997 hole_em->block_start = EXTENT_MAP_HOLE;
4998 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004999 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005000 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005001 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005002 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005003 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005004
5005 while (1) {
5006 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005007 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005008 write_unlock(&em_tree->lock);
5009 if (err != -EEXIST)
5010 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005011 btrfs_drop_extent_cache(BTRFS_I(inode),
5012 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005013 cur_offset +
5014 hole_size - 1, 0);
5015 }
5016 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005017 }
Josef Bacik16e75492013-10-22 12:18:51 -04005018next:
Yan Zheng9036c102008-10-30 14:19:41 -04005019 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005020 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005021 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005022 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005023 break;
5024 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005025 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005026 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5027 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005028 return err;
5029}
5030
Eric Sandeen3972f262013-01-12 02:57:22 +00005031static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005032{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005033 struct btrfs_root *root = BTRFS_I(inode)->root;
5034 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005035 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005036 loff_t newsize = attr->ia_size;
5037 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005038 int ret;
5039
Eric Sandeen3972f262013-01-12 02:57:22 +00005040 /*
5041 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5042 * special case where we need to update the times despite not having
5043 * these flags set. For all other operations the VFS set these flags
5044 * explicitly if it wants a timestamp update.
5045 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005046 if (newsize != oldsize) {
5047 inode_inc_iversion(inode);
5048 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5049 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005050 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005051 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005052
Josef Bacika41ad392011-01-31 15:30:16 -05005053 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005054 /*
5055 * Don't do an expanding truncate while snapshoting is ongoing.
5056 * This is to ensure the snapshot captures a fully consistent
5057 * state of this file - if the snapshot captures this expanding
5058 * truncation, it must capture all writes that happened before
5059 * this truncation.
5060 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005061 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005062 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 if (ret) {
5064 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005065 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005066 }
Yan, Zheng80825102009-11-12 09:35:36 +00005067
Miao Xief4a2f4c2011-12-14 20:12:01 -05005068 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005069 if (IS_ERR(trans)) {
5070 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005071 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005072 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005073
5074 i_size_write(inode, newsize);
5075 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305076 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005077 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005078 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005079 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005080 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005081
Josef Bacika41ad392011-01-31 15:30:16 -05005082 /*
5083 * We're truncating a file that used to have good data down to
5084 * zero. Make sure it gets into the ordered flush list so that
5085 * any new writes get down to disk quickly.
5086 */
5087 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005088 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5089 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005090
Josef Bacikf3fe8202013-01-07 17:03:21 -05005091 /*
5092 * 1 for the orphan item we're going to add
5093 * 1 for the orphan item deletion.
5094 */
5095 trans = btrfs_start_transaction(root, 2);
5096 if (IS_ERR(trans))
5097 return PTR_ERR(trans);
5098
5099 /*
5100 * We need to do this in case we fail at _any_ point during the
5101 * actual truncate. Once we do the truncate_setsize we could
5102 * invalidate pages which forces any outstanding ordered io to
5103 * be instantly completed which will give us extents that need
5104 * to be truncated. If we fail to get an orphan inode down we
5105 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005106 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005107 * will be consistent.
5108 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005109 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005110 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005111 if (ret)
5112 return ret;
5113
Josef Bacika41ad392011-01-31 15:30:16 -05005114 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5115 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005116
5117 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005118 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005119 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005120 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005121
Josef Bacika41ad392011-01-31 15:30:16 -05005122 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005123 if (ret && inode->i_nlink) {
5124 int err;
5125
Liu Bo19fd2df2016-12-01 13:01:02 -08005126 /* To get a stable disk_i_size */
5127 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5128 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005129 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005130 return err;
5131 }
5132
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005133 /*
5134 * failed to truncate, disk_i_size is only adjusted down
5135 * as we remove extents, so it should represent the true
5136 * size of the inode, so reset the in memory size and
5137 * delete our orphan entry.
5138 */
5139 trans = btrfs_join_transaction(root);
5140 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005141 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005142 return ret;
5143 }
5144 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005145 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005146 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005147 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005148 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005149 }
Yan, Zheng80825102009-11-12 09:35:36 +00005150 }
5151
Josef Bacika41ad392011-01-31 15:30:16 -05005152 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005153}
5154
Chris Mason39279cc2007-06-12 06:35:45 -04005155static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5156{
David Howells2b0143b2015-03-17 22:25:59 +00005157 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005158 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005159 int err;
5160
Li Zefanb83cc962010-12-20 16:04:08 +08005161 if (btrfs_root_readonly(root))
5162 return -EROFS;
5163
Jan Kara31051c82016-05-26 16:55:18 +02005164 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005165 if (err)
5166 return err;
5167
Chris Mason5a3f23d2009-03-31 13:27:11 -04005168 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005169 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005170 if (err)
5171 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005172 }
Yan Zheng9036c102008-10-30 14:19:41 -04005173
Christoph Hellwig10257742010-06-04 11:30:02 +02005174 if (attr->ia_valid) {
5175 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005176 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005177 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005178
Josef Bacik22c44fe2011-11-30 10:45:38 -05005179 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005180 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005181 }
5182
Chris Mason39279cc2007-06-12 06:35:45 -04005183 return err;
5184}
Chris Mason61295eb2008-01-14 16:24:38 -05005185
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005186/*
5187 * While truncating the inode pages during eviction, we get the VFS calling
5188 * btrfs_invalidatepage() against each page of the inode. This is slow because
5189 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5190 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5191 * extent_state structures over and over, wasting lots of time.
5192 *
5193 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5194 * those expensive operations on a per page basis and do only the ordered io
5195 * finishing, while we release here the extent_map and extent_state structures,
5196 * without the excessive merging and splitting.
5197 */
5198static void evict_inode_truncate_pages(struct inode *inode)
5199{
5200 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5201 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5202 struct rb_node *node;
5203
5204 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005205 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005206
5207 write_lock(&map_tree->lock);
5208 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5209 struct extent_map *em;
5210
5211 node = rb_first(&map_tree->map);
5212 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005213 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5214 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005215 remove_extent_mapping(map_tree, em);
5216 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005217 if (need_resched()) {
5218 write_unlock(&map_tree->lock);
5219 cond_resched();
5220 write_lock(&map_tree->lock);
5221 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 }
5223 write_unlock(&map_tree->lock);
5224
Filipe Manana6ca07092015-05-26 00:55:42 +01005225 /*
5226 * Keep looping until we have no more ranges in the io tree.
5227 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005228 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5229 * still in progress (unlocked the pages in the bio but did not yet
5230 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 * ranges can still be locked and eviction started because before
5232 * submitting those bios, which are executed by a separate task (work
5233 * queue kthread), inode references (inode->i_count) were not taken
5234 * (which would be dropped in the end io callback of each bio).
5235 * Therefore here we effectively end up waiting for those bios and
5236 * anyone else holding locked ranges without having bumped the inode's
5237 * reference count - if we don't do it, when they access the inode's
5238 * io_tree to unlock a range it may be too late, leading to an
5239 * use-after-free issue.
5240 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005241 spin_lock(&io_tree->lock);
5242 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5243 struct extent_state *state;
5244 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005245 u64 start;
5246 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247
5248 node = rb_first(&io_tree->state);
5249 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005250 start = state->start;
5251 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005252 spin_unlock(&io_tree->lock);
5253
David Sterbaff13db42015-12-03 14:30:40 +01005254 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005255
5256 /*
5257 * If still has DELALLOC flag, the extent didn't reach disk,
5258 * and its reserved space won't be freed by delayed_ref.
5259 * So we need to free its reserved space here.
5260 * (Refer to comment in btrfs_invalidatepage, case 2)
5261 *
5262 * Note, end is the bytenr of last byte, so we need + 1 here.
5263 */
5264 if (state->state & EXTENT_DELALLOC)
5265 btrfs_qgroup_free_data(inode, start, end - start + 1);
5266
Filipe Manana6ca07092015-05-26 00:55:42 +01005267 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005268 EXTENT_LOCKED | EXTENT_DIRTY |
5269 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5270 EXTENT_DEFRAG, 1, 1,
5271 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005272
Filipe Manana7064dd52014-08-08 02:47:05 +01005273 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005274 spin_lock(&io_tree->lock);
5275 }
5276 spin_unlock(&io_tree->lock);
5277}
5278
Al Virobd555972010-06-07 11:35:40 -04005279void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005282 struct btrfs_trans_handle *trans;
5283 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005284 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005285 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005286 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005287 int ret;
5288
liubo1abe9b82011-03-24 11:18:59 +00005289 trace_btrfs_inode_evict(inode);
5290
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005291 if (!root) {
5292 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5293 return;
5294 }
5295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005296 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005297
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005298 evict_inode_truncate_pages(inode);
5299
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005300 if (inode->i_nlink &&
5301 ((btrfs_root_refs(&root->root_item) != 0 &&
5302 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005303 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005304 goto no_delete;
5305
Chris Mason39279cc2007-06-12 06:35:45 -04005306 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005307 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005308 goto no_delete;
5309 }
Al Virobd555972010-06-07 11:35:40 -04005310 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005311 if (!special_file(inode->i_mode))
5312 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005313
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005314 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005315
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005316 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005317 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005318 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005319 goto no_delete;
5320 }
5321
Yan, Zheng76dda932009-09-21 16:00:26 -04005322 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005323 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5324 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005325 goto no_delete;
5326 }
5327
Nikolay Borisovaa790212017-01-10 20:35:40 +02005328 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005329 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005330 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005331 goto no_delete;
5332 }
5333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005334 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005335 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005336 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005337 goto no_delete;
5338 }
Josef Bacik4a338542011-08-29 11:01:31 -04005339 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005340 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005341 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005342
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005343 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005344
Josef Bacik4289a662011-08-05 13:22:24 -04005345 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005346 * This is a bit simpler than btrfs_truncate since we've already
5347 * reserved our space for our orphan item in the unlink, so we just
5348 * need to reserve some slack space in case we add bytes and update
5349 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005350 */
Yan, Zheng80825102009-11-12 09:35:36 +00005351 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005352 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5353 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005354
Josef Bacik726c35f2011-09-26 15:46:06 -04005355 /*
5356 * Try and steal from the global reserve since we will
5357 * likely not use this space anyway, we want to try as
5358 * hard as possible to get this to work.
5359 */
5360 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005361 steal_from_global++;
5362 else
5363 steal_from_global = 0;
5364 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005365
Josef Bacik3bce8762015-02-24 12:35:51 -08005366 /*
5367 * steal_from_global == 0: we reserved stuff, hooray!
5368 * steal_from_global == 1: we didn't reserve stuff, boo!
5369 * steal_from_global == 2: we've committed, still not a lot of
5370 * room but maybe we'll have room in the global reserve this
5371 * time.
5372 * steal_from_global == 3: abandon all hope!
5373 */
5374 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005375 btrfs_warn(fs_info,
5376 "Could not get space for a delete, will truncate on mount %d",
5377 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005378 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005379 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005380 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005381 }
5382
Miao Xie0e8c36a2012-12-19 06:59:51 +00005383 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005384 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005385 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005387 goto no_delete;
5388 }
5389
Josef Bacik3bce8762015-02-24 12:35:51 -08005390 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005391 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005392 * sure there is room to do it, if not we need to commit and try
5393 * again.
5394 */
5395 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005396 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005397 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005398 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005399 else
5400 ret = -ENOSPC;
5401 }
5402
5403 /*
5404 * Couldn't steal from the global reserve, we have too much
5405 * pending stuff built up, commit the transaction and try it
5406 * again.
5407 */
5408 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005409 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005410 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005411 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005412 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005413 goto no_delete;
5414 }
5415 continue;
5416 } else {
5417 steal_from_global = 0;
5418 }
5419
Josef Bacik4289a662011-08-05 13:22:24 -04005420 trans->block_rsv = rsv;
5421
Yan, Zhengd68fc572010-05-16 10:49:58 -04005422 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005423 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005424 break;
5425
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005426 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005427 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005428 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005429 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005430 }
5431
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005432 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005433
Josef Bacik4ef31a42013-08-13 14:10:08 -04005434 /*
5435 * Errors here aren't a big deal, it just means we leave orphan items
5436 * in the tree. They will be cleaned up on the next mount.
5437 */
Yan, Zheng80825102009-11-12 09:35:36 +00005438 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005439 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005440 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005441 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005442 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005443 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005445 trans->block_rsv = &fs_info->trans_block_rsv;
5446 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005447 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005448 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005449
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005450 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005451 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005452no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005453 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005454 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005455}
5456
5457/*
5458 * this returns the key found in the dir entry in the location pointer.
5459 * If no dir entries were found, location->objectid is 0.
5460 */
5461static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5462 struct btrfs_key *location)
5463{
5464 const char *name = dentry->d_name.name;
5465 int namelen = dentry->d_name.len;
5466 struct btrfs_dir_item *di;
5467 struct btrfs_path *path;
5468 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005469 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005470
5471 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005472 if (!path)
5473 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005474
David Sterbaf85b7372017-01-20 14:54:07 +01005475 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5476 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005477 if (IS_ERR(di))
5478 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005479
David Sterbac7040052011-04-19 18:00:01 +02005480 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005481 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005482
Chris Mason5f39d392007-10-15 16:14:19 -04005483 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005484out:
Chris Mason39279cc2007-06-12 06:35:45 -04005485 btrfs_free_path(path);
5486 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005487out_err:
5488 location->objectid = 0;
5489 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005490}
5491
5492/*
5493 * when we hit a tree root in a directory, the btrfs part of the inode
5494 * needs to be changed to reflect the root directory of the tree root. This
5495 * is kind of like crossing a mount point.
5496 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005497static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 struct inode *dir,
5499 struct dentry *dentry,
5500 struct btrfs_key *location,
5501 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005502{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 struct btrfs_path *path;
5504 struct btrfs_root *new_root;
5505 struct btrfs_root_ref *ref;
5506 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005507 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 int ret;
5509 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 path = btrfs_alloc_path();
5512 if (!path) {
5513 err = -ENOMEM;
5514 goto out;
5515 }
Chris Mason39279cc2007-06-12 06:35:45 -04005516
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005518 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5519 key.type = BTRFS_ROOT_REF_KEY;
5520 key.offset = location->objectid;
5521
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005522 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 if (ret) {
5524 if (ret < 0)
5525 err = ret;
5526 goto out;
5527 }
Chris Mason39279cc2007-06-12 06:35:45 -04005528
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 leaf = path->nodes[0];
5530 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005531 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5533 goto out;
5534
5535 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5536 (unsigned long)(ref + 1),
5537 dentry->d_name.len);
5538 if (ret)
5539 goto out;
5540
David Sterbab3b4aa72011-04-21 01:20:15 +02005541 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005543 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 if (IS_ERR(new_root)) {
5545 err = PTR_ERR(new_root);
5546 goto out;
5547 }
5548
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005549 *sub_root = new_root;
5550 location->objectid = btrfs_root_dirid(&new_root->root_item);
5551 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005552 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005553 err = 0;
5554out:
5555 btrfs_free_path(path);
5556 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005557}
5558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559static void inode_tree_add(struct inode *inode)
5560{
5561 struct btrfs_root *root = BTRFS_I(inode)->root;
5562 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005563 struct rb_node **p;
5564 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005565 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005566 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567
Al Viro1d3382cb2010-10-23 15:19:20 -04005568 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005569 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005570 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005572 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 while (*p) {
5574 parent = *p;
5575 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5576
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005577 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005578 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005579 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005580 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 else {
5582 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005583 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005584 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005585 RB_CLEAR_NODE(parent);
5586 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005587 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 }
5589 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005590 rb_link_node(new, parent, p);
5591 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592 spin_unlock(&root->inode_lock);
5593}
5594
5595static void inode_tree_del(struct inode *inode)
5596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005597 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005601 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005605 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005607 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005608
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005609 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005610 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005611 spin_lock(&root->inode_lock);
5612 empty = RB_EMPTY_ROOT(&root->inode_tree);
5613 spin_unlock(&root->inode_lock);
5614 if (empty)
5615 btrfs_add_dead_root(root);
5616 }
5617}
5618
Jeff Mahoney143bede2012-03-01 14:56:26 +01005619void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005620{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005621 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005622 struct rb_node *node;
5623 struct rb_node *prev;
5624 struct btrfs_inode *entry;
5625 struct inode *inode;
5626 u64 objectid = 0;
5627
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005628 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005629 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005630
5631 spin_lock(&root->inode_lock);
5632again:
5633 node = root->inode_tree.rb_node;
5634 prev = NULL;
5635 while (node) {
5636 prev = node;
5637 entry = rb_entry(node, struct btrfs_inode, rb_node);
5638
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005639 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005640 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005641 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005642 node = node->rb_right;
5643 else
5644 break;
5645 }
5646 if (!node) {
5647 while (prev) {
5648 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005649 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005650 node = prev;
5651 break;
5652 }
5653 prev = rb_next(prev);
5654 }
5655 }
5656 while (node) {
5657 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005658 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005659 inode = igrab(&entry->vfs_inode);
5660 if (inode) {
5661 spin_unlock(&root->inode_lock);
5662 if (atomic_read(&inode->i_count) > 1)
5663 d_prune_aliases(inode);
5664 /*
Al Viro45321ac2010-06-07 13:43:19 -04005665 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005666 * the inode cache when its usage count
5667 * hits zero.
5668 */
5669 iput(inode);
5670 cond_resched();
5671 spin_lock(&root->inode_lock);
5672 goto again;
5673 }
5674
5675 if (cond_resched_lock(&root->inode_lock))
5676 goto again;
5677
5678 node = rb_next(node);
5679 }
5680 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005681}
5682
Chris Masone02119d2008-09-05 16:13:11 -04005683static int btrfs_init_locked_inode(struct inode *inode, void *p)
5684{
5685 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005686 inode->i_ino = args->location->objectid;
5687 memcpy(&BTRFS_I(inode)->location, args->location,
5688 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005689 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005690 return 0;
5691}
5692
5693static int btrfs_find_actor(struct inode *inode, void *opaque)
5694{
5695 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005696 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005697 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005698}
5699
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005700static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005701 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005702 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005703{
5704 struct inode *inode;
5705 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005706 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005707
Chris Mason90d3e592014-01-09 17:28:00 -08005708 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005709 args.root = root;
5710
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005711 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005712 btrfs_init_locked_inode,
5713 (void *)&args);
5714 return inode;
5715}
5716
Balaji Rao1a54ef82008-07-21 02:01:04 +05305717/* Get an inode object given its location and corresponding root.
5718 * Returns in *is_new if the inode was read from disk
5719 */
5720struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005721 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305722{
5723 struct inode *inode;
5724
Chris Mason90d3e592014-01-09 17:28:00 -08005725 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305726 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005727 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305728
5729 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005730 int ret;
5731
5732 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005733 if (!is_bad_inode(inode)) {
5734 inode_tree_add(inode);
5735 unlock_new_inode(inode);
5736 if (new)
5737 *new = 1;
5738 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005739 unlock_new_inode(inode);
5740 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005741 ASSERT(ret < 0);
5742 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005743 }
5744 }
5745
Balaji Rao1a54ef82008-07-21 02:01:04 +05305746 return inode;
5747}
5748
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749static struct inode *new_simple_dir(struct super_block *s,
5750 struct btrfs_key *key,
5751 struct btrfs_root *root)
5752{
5753 struct inode *inode = new_inode(s);
5754
5755 if (!inode)
5756 return ERR_PTR(-ENOMEM);
5757
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 BTRFS_I(inode)->root = root;
5759 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005760 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761
5762 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005763 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005764 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765 inode->i_fop = &simple_dir_operations;
5766 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005767 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305768 inode->i_atime = inode->i_mtime;
5769 inode->i_ctime = inode->i_mtime;
5770 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771
5772 return inode;
5773}
5774
Chris Mason3de45862008-11-17 21:02:50 -05005775struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005777 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005778 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005779 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005780 struct btrfs_root *sub_root = root;
5781 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005782 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005783 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
5785 if (dentry->d_name.len > BTRFS_NAME_LEN)
5786 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005787
Jeff Layton39e3c952012-11-28 11:30:53 -05005788 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005789 if (ret < 0)
5790 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005791
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005792 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005793 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794
5795 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005796 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005797 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005798 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799
5800 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5801
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005802 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005803 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005804 &location, &sub_root);
5805 if (ret < 0) {
5806 if (ret != -ENOENT)
5807 inode = ERR_PTR(ret);
5808 else
5809 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5810 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005811 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005812 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005814
Julia Lawall34d19ba2011-01-24 19:55:19 +00005815 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005816 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005817 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005818 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005819 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005820 if (ret) {
5821 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005822 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005823 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005824 }
5825
Chris Mason3de45862008-11-17 21:02:50 -05005826 return inode;
5827}
5828
Nick Pigginfe15ce42011-01-07 17:49:23 +11005829static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005830{
5831 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005832 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005833
Li Zefan848cce02012-02-21 17:04:28 +08005834 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005835 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005836
Li Zefan848cce02012-02-21 17:04:28 +08005837 if (inode) {
5838 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005839 if (btrfs_root_refs(&root->root_item) == 0)
5840 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005841
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005842 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005843 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005844 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005845 return 0;
5846}
5847
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005848static void btrfs_dentry_release(struct dentry *dentry)
5849{
Daeseok Youn944a4512014-04-14 15:37:02 +09005850 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005851}
5852
Chris Mason3de45862008-11-17 21:02:50 -05005853static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005854 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005855{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005856 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005857
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005858 inode = btrfs_lookup_dentry(dir, dentry);
5859 if (IS_ERR(inode)) {
5860 if (PTR_ERR(inode) == -ENOENT)
5861 inode = NULL;
5862 else
5863 return ERR_CAST(inode);
5864 }
5865
Al Viro41d28bc2014-10-12 22:24:21 -04005866 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005867}
5868
Miao Xie16cdcec2011-04-22 18:12:22 +08005869unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005870 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5871};
5872
Al Viro9cdda8d2013-05-22 16:48:09 -04005873static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005874{
Al Viro9cdda8d2013-05-22 16:48:09 -04005875 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005878 struct btrfs_dir_item *di;
5879 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005880 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005881 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005882 struct list_head ins_list;
5883 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005885 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005886 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005887 unsigned char d_type;
5888 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005889 char tmp_name[32];
5890 char *name_ptr;
5891 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005892 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005893 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005894
Al Viro9cdda8d2013-05-22 16:48:09 -04005895 if (!dir_emit_dots(file, ctx))
5896 return 0;
5897
David Woodhouse49593bf2008-08-17 17:08:36 +01005898 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005899 if (!path)
5900 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005901
David Sterbae4058b52015-11-27 16:31:35 +01005902 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005903
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005904 INIT_LIST_HEAD(&ins_list);
5905 INIT_LIST_HEAD(&del_list);
5906 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005907
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005908 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005909 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005910 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005911
Chris Mason39279cc2007-06-12 06:35:45 -04005912 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5913 if (ret < 0)
5914 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005915
5916 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005917 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005918 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005919 if (slot >= btrfs_header_nritems(leaf)) {
5920 ret = btrfs_next_leaf(root, path);
5921 if (ret < 0)
5922 goto err;
5923 else if (ret > 0)
5924 break;
5925 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 }
Chris Mason3de45862008-11-17 21:02:50 -05005927
Chris Mason5f39d392007-10-15 16:14:19 -04005928 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5929
5930 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005931 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005932 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005933 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005934 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005935 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005936 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005937 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005938
Al Viro9cdda8d2013-05-22 16:48:09 -04005939 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005940
Chris Mason39279cc2007-06-12 06:35:45 -04005941 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005942 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005944
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005945 name_len = btrfs_dir_name_len(leaf, di);
5946 if (name_len <= sizeof(tmp_name)) {
5947 name_ptr = tmp_name;
5948 } else {
5949 name_ptr = kmalloc(name_len, GFP_KERNEL);
5950 if (!name_ptr) {
5951 ret = -ENOMEM;
5952 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005953 }
Chris Mason39279cc2007-06-12 06:35:45 -04005954 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005955 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5956 name_len);
5957
5958 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5959 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5960
5961 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5962 d_type);
5963
5964 if (name_ptr != tmp_name)
5965 kfree(name_ptr);
5966
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005967 if (over)
5968 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005969 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005970next:
5971 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005972 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005973
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005974 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005975 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005976 goto nopos;
5977
Zach Browndb62efb2013-07-11 16:19:42 -07005978 /*
5979 * Stop new entries from being returned after we return the last
5980 * entry.
5981 *
5982 * New directory entries are assigned a strictly increasing
5983 * offset. This means that new entries created during readdir
5984 * are *guaranteed* to be seen in the future by that readdir.
5985 * This has broken buggy programs which operate on names as
5986 * they're returned by readdir. Until we re-use freed offsets
5987 * we have this hack to stop new entries from being returned
5988 * under the assumption that they'll never reach this huge
5989 * offset.
5990 *
5991 * This is being careful not to overflow 32bit loff_t unless the
5992 * last entry requires it because doing so has broken 32bit apps
5993 * in the past.
5994 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005995 if (ctx->pos >= INT_MAX)
5996 ctx->pos = LLONG_MAX;
5997 else
5998 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005999nopos:
6000 ret = 0;
6001err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006002 if (put)
6003 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006004 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006005 return ret;
6006}
6007
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006008int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006009{
6010 struct btrfs_root *root = BTRFS_I(inode)->root;
6011 struct btrfs_trans_handle *trans;
6012 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006013 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006014
Josef Bacik72ac3c02012-05-23 14:13:11 -04006015 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006016 return 0;
6017
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006018 if (btrfs_fs_closing(root->fs_info) &&
6019 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006020 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006021
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006022 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006023 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006024 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006025 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006026 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006027 if (IS_ERR(trans))
6028 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006029 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006030 }
6031 return ret;
6032}
6033
6034/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006035 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006036 * inode changes. But, it is most likely to find the inode in cache.
6037 * FIXME, needs more benchmarking...there are no reasons other than performance
6038 * to keep or drop this code.
6039 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006040static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006041{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006043 struct btrfs_root *root = BTRFS_I(inode)->root;
6044 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006045 int ret;
6046
Josef Bacik72ac3c02012-05-23 14:13:11 -04006047 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006048 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006049
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006050 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006051 if (IS_ERR(trans))
6052 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006053
6054 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006055 if (ret && ret == -ENOSPC) {
6056 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006057 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006058 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006059 if (IS_ERR(trans))
6060 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006061
Chris Mason94b60442010-05-26 11:02:00 -04006062 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006063 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006064 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006065 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006066 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006067
6068 return ret;
6069}
6070
6071/*
6072 * This is a copy of file_update_time. We need this so we can return error on
6073 * ENOSPC for updating the inode in the case of file write and mmap writes.
6074 */
Josef Bacike41f9412012-03-26 09:46:47 -04006075static int btrfs_update_time(struct inode *inode, struct timespec *now,
6076 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006077{
Alexander Block2bc5565282012-06-15 09:49:33 +02006078 struct btrfs_root *root = BTRFS_I(inode)->root;
6079
6080 if (btrfs_root_readonly(root))
6081 return -EROFS;
6082
Josef Bacike41f9412012-03-26 09:46:47 -04006083 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006084 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006085 if (flags & S_CTIME)
6086 inode->i_ctime = *now;
6087 if (flags & S_MTIME)
6088 inode->i_mtime = *now;
6089 if (flags & S_ATIME)
6090 inode->i_atime = *now;
6091 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006092}
6093
Chris Masond352ac62008-09-29 15:18:18 -04006094/*
6095 * find the highest existing sequence number in a directory
6096 * and then set the in-memory index_cnt variable to reflect
6097 * free sequence numbers
6098 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006099static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006100{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006101 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006102 struct btrfs_key key, found_key;
6103 struct btrfs_path *path;
6104 struct extent_buffer *leaf;
6105 int ret;
6106
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006107 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006108 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006109 key.offset = (u64)-1;
6110
6111 path = btrfs_alloc_path();
6112 if (!path)
6113 return -ENOMEM;
6114
6115 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6116 if (ret < 0)
6117 goto out;
6118 /* FIXME: we should be able to handle this */
6119 if (ret == 0)
6120 goto out;
6121 ret = 0;
6122
6123 /*
6124 * MAGIC NUMBER EXPLANATION:
6125 * since we search a directory based on f_pos we have to start at 2
6126 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6127 * else has to start at 2
6128 */
6129 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006130 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006131 goto out;
6132 }
6133
6134 path->slots[0]--;
6135
6136 leaf = path->nodes[0];
6137 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6138
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006139 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006140 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006141 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006142 goto out;
6143 }
6144
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006146out:
6147 btrfs_free_path(path);
6148 return ret;
6149}
6150
Chris Masond352ac62008-09-29 15:18:18 -04006151/*
6152 * helper to find a free sequence number in a given directory. This current
6153 * code is very simple, later versions will do smarter things in the btree
6154 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006155int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006156{
6157 int ret = 0;
6158
Nikolay Borisov877574e2017-02-20 13:50:33 +02006159 if (dir->index_cnt == (u64)-1) {
6160 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006161 if (ret) {
6162 ret = btrfs_set_inode_index_count(dir);
6163 if (ret)
6164 return ret;
6165 }
Josef Bacikaec74772008-07-24 12:12:38 -04006166 }
6167
Nikolay Borisov877574e2017-02-20 13:50:33 +02006168 *index = dir->index_cnt;
6169 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006170
6171 return ret;
6172}
6173
Chris Masonb0d5d102014-09-08 13:08:51 -07006174static int btrfs_insert_inode_locked(struct inode *inode)
6175{
6176 struct btrfs_iget_args args;
6177 args.location = &BTRFS_I(inode)->location;
6178 args.root = BTRFS_I(inode)->root;
6179
6180 return insert_inode_locked4(inode,
6181 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6182 btrfs_find_actor, &args);
6183}
6184
Chris Mason39279cc2007-06-12 06:35:45 -04006185static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6186 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006187 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006188 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006189 u64 ref_objectid, u64 objectid,
6190 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006192 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006193 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006194 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006195 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006196 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006197 struct btrfs_inode_ref *ref;
6198 struct btrfs_key key[2];
6199 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006200 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006201 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006202 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006203
Chris Mason5f39d392007-10-15 16:14:19 -04006204 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006205 if (!path)
6206 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006207
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006208 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006209 if (!inode) {
6210 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006211 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006212 }
Chris Mason39279cc2007-06-12 06:35:45 -04006213
Li Zefan581bb052011-04-20 10:06:11 +08006214 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006215 * O_TMPFILE, set link count to 0, so that after this point,
6216 * we fill in an inode item with the correct link count.
6217 */
6218 if (!name)
6219 set_nlink(inode, 0);
6220
6221 /*
Li Zefan581bb052011-04-20 10:06:11 +08006222 * we have to initialize this early, so we can reclaim the inode
6223 * number if we fail afterwards in this function.
6224 */
6225 inode->i_ino = objectid;
6226
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006228 trace_btrfs_inode_request(dir);
6229
Nikolay Borisov877574e2017-02-20 13:50:33 +02006230 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006231 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006232 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006233 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006234 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006235 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006236 } else if (dir) {
6237 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006238 }
6239 /*
6240 * index_cnt is ignored for everything but a dir,
6241 * btrfs_get_inode_index_count has an explanation for the magic
6242 * number
6243 */
6244 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006245 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006246 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006247 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006248 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006249
Josef Bacik5dc562c2012-08-17 13:14:17 -04006250 /*
6251 * We could have gotten an inode number from somebody who was fsynced
6252 * and then removed in this same transaction, so let's just set full
6253 * sync since it will be a full sync anyway and this will blow away the
6254 * old info in the log.
6255 */
6256 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6257
Chris Mason9c583092008-01-29 15:15:18 -05006258 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006259 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006260 key[0].offset = 0;
6261
Chris Mason9c583092008-01-29 15:15:18 -05006262 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006263
6264 if (name) {
6265 /*
6266 * Start new inodes with an inode_ref. This is slightly more
6267 * efficient for small numbers of hard links since they will
6268 * be packed into one item. Extended refs will kick in if we
6269 * add more hard links than can fit in the ref item.
6270 */
6271 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006272 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006273 key[1].offset = ref_objectid;
6274
6275 sizes[1] = name_len + sizeof(*ref);
6276 }
Chris Mason9c583092008-01-29 15:15:18 -05006277
Chris Masonb0d5d102014-09-08 13:08:51 -07006278 location = &BTRFS_I(inode)->location;
6279 location->objectid = objectid;
6280 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006281 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006282
6283 ret = btrfs_insert_inode_locked(inode);
6284 if (ret < 0)
6285 goto fail;
6286
Chris Masonb9473432009-03-13 11:00:37 -04006287 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006288 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006289 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006290 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006291
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006292 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006293 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306294
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006295 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306296 inode->i_atime = inode->i_mtime;
6297 inode->i_ctime = inode->i_mtime;
6298 BTRFS_I(inode)->i_otime = inode->i_mtime;
6299
Chris Mason5f39d392007-10-15 16:14:19 -04006300 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6301 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006302 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006303 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006304 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006305
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006306 if (name) {
6307 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6308 struct btrfs_inode_ref);
6309 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6310 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6311 ptr = (unsigned long)(ref + 1);
6312 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6313 }
Chris Mason9c583092008-01-29 15:15:18 -05006314
Chris Mason5f39d392007-10-15 16:14:19 -04006315 btrfs_mark_buffer_dirty(path->nodes[0]);
6316 btrfs_free_path(path);
6317
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006318 btrfs_inherit_iflags(inode, dir);
6319
Al Viro569254b2011-07-24 17:08:40 -04006320 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006321 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006322 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006323 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006324 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6325 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006326 }
6327
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006328 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006329
6330 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006331 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006332
Alexander Block8ea05e32012-07-25 17:35:53 +02006333 btrfs_update_root_times(trans, root);
6334
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006335 ret = btrfs_inode_inherit_props(trans, inode, dir);
6336 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006337 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006338 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006339 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006340
Chris Mason39279cc2007-06-12 06:35:45 -04006341 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006342
6343fail_unlock:
6344 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006345fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006346 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006347 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006348 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006349 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006350 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006351}
6352
6353static inline u8 btrfs_inode_type(struct inode *inode)
6354{
6355 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6356}
6357
Chris Masond352ac62008-09-29 15:18:18 -04006358/*
6359 * utility function to add 'inode' into 'parent_inode' with
6360 * a give name and a given sequence number.
6361 * if 'add_backref' is true, also insert a backref from the
6362 * inode to the parent directory.
6363 */
Chris Masone02119d2008-09-05 16:13:11 -04006364int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006365 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006366 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006367{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006369 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006370 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006371 struct btrfs_root *root = parent_inode->root;
6372 u64 ino = btrfs_ino(inode);
6373 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006374
Li Zefan33345d012011-04-20 10:31:50 +08006375 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006376 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006377 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006378 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006379 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006380 key.offset = 0;
6381 }
Chris Mason39279cc2007-06-12 06:35:45 -04006382
Li Zefan33345d012011-04-20 10:31:50 +08006383 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006384 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6385 root->root_key.objectid, parent_ino,
6386 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006387 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006388 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6389 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006390 }
6391
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006392 /* Nothing to clean up yet */
6393 if (ret)
6394 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006395
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006396 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6397 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006398 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006399 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006400 goto fail_dir_item;
6401 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006402 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006403 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006404 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006405
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006406 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006407 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006408 inode_inc_iversion(&parent_inode->vfs_inode);
6409 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6410 current_time(&parent_inode->vfs_inode);
6411 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006412 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006413 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006414 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006415
6416fail_dir_item:
6417 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6418 u64 local_index;
6419 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006420 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6421 root->root_key.objectid, parent_ino,
6422 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006423
6424 } else if (add_backref) {
6425 u64 local_index;
6426 int err;
6427
6428 err = btrfs_del_inode_ref(trans, root, name, name_len,
6429 ino, parent_ino, &local_index);
6430 }
6431 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006432}
6433
6434static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006435 struct btrfs_inode *dir, struct dentry *dentry,
6436 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006437{
Josef Bacika1b075d2010-11-19 20:36:11 +00006438 int err = btrfs_add_link(trans, dir, inode,
6439 dentry->d_name.name, dentry->d_name.len,
6440 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006441 if (err > 0)
6442 err = -EEXIST;
6443 return err;
6444}
6445
Josef Bacik618e21d2007-07-11 10:18:17 -04006446static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006447 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006448{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006449 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006450 struct btrfs_trans_handle *trans;
6451 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006452 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006453 int err;
6454 int drop_inode = 0;
6455 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006456 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006457
Josef Bacik9ed74f22009-09-11 16:12:44 -04006458 /*
6459 * 2 for inode item and ref
6460 * 2 for dir items
6461 * 1 for xattr if selinux is on
6462 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006463 trans = btrfs_start_transaction(root, 5);
6464 if (IS_ERR(trans))
6465 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006466
Li Zefan581bb052011-04-20 10:06:11 +08006467 err = btrfs_find_free_ino(root, &objectid);
6468 if (err)
6469 goto out_unlock;
6470
Josef Bacikaec74772008-07-24 12:12:38 -04006471 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006472 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6473 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006474 if (IS_ERR(inode)) {
6475 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006476 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006477 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006478
Casey Schauflerad19db72011-12-15 10:09:07 -05006479 /*
6480 * If the active LSM wants to access the inode during
6481 * d_instantiate it needs these. Smack checks to see
6482 * if the filesystem supports xattrs by looking at the
6483 * ops vector.
6484 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006485 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006486 init_special_inode(inode, inode->i_mode, rdev);
6487
6488 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006489 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006490 goto out_unlock_inode;
6491
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006492 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6493 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006494 if (err) {
6495 goto out_unlock_inode;
6496 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006497 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006498 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006499 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006500 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006501
Josef Bacik618e21d2007-07-11 10:18:17 -04006502out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006503 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006504 btrfs_balance_delayed_items(fs_info);
6505 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006506 if (drop_inode) {
6507 inode_dec_link_count(inode);
6508 iput(inode);
6509 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006510 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006511
6512out_unlock_inode:
6513 drop_inode = 1;
6514 unlock_new_inode(inode);
6515 goto out_unlock;
6516
Josef Bacik618e21d2007-07-11 10:18:17 -04006517}
6518
Chris Mason39279cc2007-06-12 06:35:45 -04006519static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006520 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006521{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006522 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006523 struct btrfs_trans_handle *trans;
6524 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006525 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006526 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006527 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006528 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006529 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006530
Josef Bacik9ed74f22009-09-11 16:12:44 -04006531 /*
6532 * 2 for inode item and ref
6533 * 2 for dir items
6534 * 1 for xattr if selinux is on
6535 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006536 trans = btrfs_start_transaction(root, 5);
6537 if (IS_ERR(trans))
6538 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006539
Li Zefan581bb052011-04-20 10:06:11 +08006540 err = btrfs_find_free_ino(root, &objectid);
6541 if (err)
6542 goto out_unlock;
6543
Josef Bacikaec74772008-07-24 12:12:38 -04006544 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006545 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6546 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006547 if (IS_ERR(inode)) {
6548 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006550 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006551 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006552 /*
6553 * If the active LSM wants to access the inode during
6554 * d_instantiate it needs these. Smack checks to see
6555 * if the filesystem supports xattrs by looking at the
6556 * ops vector.
6557 */
6558 inode->i_fop = &btrfs_file_operations;
6559 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006560 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006561
6562 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6563 if (err)
6564 goto out_unlock_inode;
6565
6566 err = btrfs_update_inode(trans, root, inode);
6567 if (err)
6568 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006569
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006570 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6571 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006572 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006573 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006574
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006575 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006576 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006577 d_instantiate(dentry, inode);
6578
Chris Mason39279cc2007-06-12 06:35:45 -04006579out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006580 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006581 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006582 inode_dec_link_count(inode);
6583 iput(inode);
6584 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006585 btrfs_balance_delayed_items(fs_info);
6586 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006587 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006588
6589out_unlock_inode:
6590 unlock_new_inode(inode);
6591 goto out_unlock;
6592
Chris Mason39279cc2007-06-12 06:35:45 -04006593}
6594
6595static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6596 struct dentry *dentry)
6597{
Filipe Manana271dba452016-01-05 16:24:05 +00006598 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006599 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006600 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006601 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006602 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006603 int err;
6604 int drop_inode = 0;
6605
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006606 /* do not allow sys_link's with other subvols of the same device */
6607 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006608 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006609
Mark Fashehf1863732012-08-08 11:32:27 -07006610 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006611 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006612
Nikolay Borisov877574e2017-02-20 13:50:33 +02006613 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006614 if (err)
6615 goto fail;
6616
Yan, Zhenga22285a2010-05-16 10:48:46 -04006617 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006618 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006619 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006620 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006622 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006623 if (IS_ERR(trans)) {
6624 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006625 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006626 goto fail;
6627 }
Chris Mason5f39d392007-10-15 16:14:19 -04006628
Miao Xie67de1172013-12-26 13:07:06 +08006629 /* There are several dir indexes for this inode, clear the cache. */
6630 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006631 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006632 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006633 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006634 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006635 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006636
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006637 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6638 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006639
Yan, Zhenga5719522009-09-24 09:17:31 -04006640 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006641 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006642 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006643 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006644 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006645 if (err)
6646 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006647 if (inode->i_nlink == 1) {
6648 /*
6649 * If new hard link count is 1, it's a file created
6650 * with open(2) O_TMPFILE flag.
6651 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006652 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006653 if (err)
6654 goto fail;
6655 }
Al Viro08c422c2011-12-23 07:58:13 -05006656 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006657 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006658 }
Chris Mason39279cc2007-06-12 06:35:45 -04006659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006660 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006661fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006662 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006663 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006664 if (drop_inode) {
6665 inode_dec_link_count(inode);
6666 iput(inode);
6667 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006668 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006669 return err;
6670}
6671
Al Viro18bb1db2011-07-26 01:41:39 -04006672static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006673{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006675 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006676 struct btrfs_trans_handle *trans;
6677 struct btrfs_root *root = BTRFS_I(dir)->root;
6678 int err = 0;
6679 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006680 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006681 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006682
Josef Bacik9ed74f22009-09-11 16:12:44 -04006683 /*
6684 * 2 items for inode and ref
6685 * 2 items for dir items
6686 * 1 for xattr if selinux is on
6687 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006688 trans = btrfs_start_transaction(root, 5);
6689 if (IS_ERR(trans))
6690 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006691
Li Zefan581bb052011-04-20 10:06:11 +08006692 err = btrfs_find_free_ino(root, &objectid);
6693 if (err)
6694 goto out_fail;
6695
Josef Bacikaec74772008-07-24 12:12:38 -04006696 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006697 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6698 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006699 if (IS_ERR(inode)) {
6700 err = PTR_ERR(inode);
6701 goto out_fail;
6702 }
Chris Mason5f39d392007-10-15 16:14:19 -04006703
Chris Mason39279cc2007-06-12 06:35:45 -04006704 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006705 /* these must be set before we unlock the inode */
6706 inode->i_op = &btrfs_dir_inode_operations;
6707 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006708
Eric Paris2a7dba32011-02-01 11:05:39 -05006709 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006710 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006711 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006712
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006713 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006714 err = btrfs_update_inode(trans, root, inode);
6715 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006716 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006717
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006718 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6719 dentry->d_name.name,
6720 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006721 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006722 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006723
Chris Mason39279cc2007-06-12 06:35:45 -04006724 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006725 /*
6726 * mkdir is special. We're unlocking after we call d_instantiate
6727 * to avoid a race with nfsd calling d_instantiate.
6728 */
6729 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006730 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006731
6732out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006733 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006734 if (drop_on_err) {
6735 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006736 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006737 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006738 btrfs_balance_delayed_items(fs_info);
6739 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006741
6742out_fail_inode:
6743 unlock_new_inode(inode);
6744 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006745}
6746
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006747/* Find next extent map of a given extent map, caller needs to ensure locks */
6748static struct extent_map *next_extent_map(struct extent_map *em)
6749{
6750 struct rb_node *next;
6751
6752 next = rb_next(&em->rb_node);
6753 if (!next)
6754 return NULL;
6755 return container_of(next, struct extent_map, rb_node);
6756}
6757
6758static struct extent_map *prev_extent_map(struct extent_map *em)
6759{
6760 struct rb_node *prev;
6761
6762 prev = rb_prev(&em->rb_node);
6763 if (!prev)
6764 return NULL;
6765 return container_of(prev, struct extent_map, rb_node);
6766}
6767
Chris Masond352ac62008-09-29 15:18:18 -04006768/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006769 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006770 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006771 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006772 */
Chris Mason3b951512008-04-17 11:29:12 -04006773static int merge_extent_mapping(struct extent_map_tree *em_tree,
6774 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006775 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006776 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006777{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006778 struct extent_map *prev;
6779 struct extent_map *next;
6780 u64 start;
6781 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006782 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006783
Chris Masone6dcd2d2008-07-17 12:53:50 -04006784 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006785
6786 if (existing->start > map_start) {
6787 next = existing;
6788 prev = prev_extent_map(next);
6789 } else {
6790 prev = existing;
6791 next = next_extent_map(prev);
6792 }
6793
6794 start = prev ? extent_map_end(prev) : em->start;
6795 start = max_t(u64, start, em->start);
6796 end = next ? next->start : extent_map_end(em);
6797 end = min_t(u64, end, extent_map_end(em));
6798 start_diff = start - em->start;
6799 em->start = start;
6800 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006801 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6802 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006803 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006804 em->block_len -= start_diff;
6805 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006806 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006807}
6808
Chris Masonc8b97812008-10-29 14:49:59 -04006809static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006810 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006811 size_t pg_offset, u64 extent_offset,
6812 struct btrfs_file_extent_item *item)
6813{
6814 int ret;
6815 struct extent_buffer *leaf = path->nodes[0];
6816 char *tmp;
6817 size_t max_size;
6818 unsigned long inline_size;
6819 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006820 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006821
6822 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006823 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006824 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6825 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006826 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006827 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006828 if (!tmp)
6829 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006830 ptr = btrfs_file_extent_inline_start(item);
6831
6832 read_extent_buffer(leaf, tmp, ptr, inline_size);
6833
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006834 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006835 ret = btrfs_decompress(compress_type, tmp, page,
6836 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006837
6838 /*
6839 * decompression code contains a memset to fill in any space between the end
6840 * of the uncompressed data and the end of max_size in case the decompressed
6841 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6842 * the end of an inline extent and the beginning of the next block, so we
6843 * cover that region here.
6844 */
6845
6846 if (max_size + pg_offset < PAGE_SIZE) {
6847 char *map = kmap(page);
6848 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6849 kunmap(page);
6850 }
Chris Masonc8b97812008-10-29 14:49:59 -04006851 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006852 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006853}
6854
Chris Masond352ac62008-09-29 15:18:18 -04006855/*
6856 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006857 * the ugly parts come from merging extents from the disk with the in-ram
6858 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006859 * where the in-ram extents might be locked pending data=ordered completion.
6860 *
6861 * This also copies inline extents directly into the page.
6862 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006863struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6864 struct page *page,
6865 size_t pg_offset, u64 start, u64 len,
6866 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006867{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006869 int ret;
6870 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006871 u64 extent_start = 0;
6872 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006873 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006874 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006875 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006876 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006877 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006878 struct extent_buffer *leaf;
6879 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006880 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006881 struct extent_map_tree *em_tree = &inode->extent_tree;
6882 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006883 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006884 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006885
Chris Masona52d9a82007-08-27 16:49:44 -04006886again:
Chris Mason890871b2009-09-02 16:24:52 -04006887 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006888 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006889 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006890 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006891 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006892
Chris Masona52d9a82007-08-27 16:49:44 -04006893 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006894 if (em->start > start || em->start + em->len <= start)
6895 free_extent_map(em);
6896 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006897 free_extent_map(em);
6898 else
6899 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006900 }
David Sterba172ddd62011-04-21 00:48:27 +02006901 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006902 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006903 err = -ENOMEM;
6904 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006905 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006906 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006907 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006908 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006909 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006910 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006911
6912 if (!path) {
6913 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006914 if (!path) {
6915 err = -ENOMEM;
6916 goto out;
6917 }
6918 /*
6919 * Chances are we'll be called again, so go ahead and do
6920 * readahead
6921 */
David Sterbae4058b52015-11-27 16:31:35 +01006922 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006923 }
6924
Chris Mason179e29e2007-11-01 11:28:41 -04006925 ret = btrfs_lookup_file_extent(trans, root, path,
6926 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006927 if (ret < 0) {
6928 err = ret;
6929 goto out;
6930 }
6931
6932 if (ret != 0) {
6933 if (path->slots[0] == 0)
6934 goto not_found;
6935 path->slots[0]--;
6936 }
6937
Chris Mason5f39d392007-10-15 16:14:19 -04006938 leaf = path->nodes[0];
6939 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006940 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006941 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006942 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006943 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006944 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006945 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006946 /*
6947 * If we backup past the first extent we want to move forward
6948 * and see if there is an extent in front of us, otherwise we'll
6949 * say there is a hole for our whole search range which can
6950 * cause problems.
6951 */
6952 extent_end = start;
6953 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006954 }
6955
Chris Mason5f39d392007-10-15 16:14:19 -04006956 found_type = btrfs_file_extent_type(leaf, item);
6957 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006958 if (found_type == BTRFS_FILE_EXTENT_REG ||
6959 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006960 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006961 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006962
6963 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6964 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006965 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6966 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006967 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006968 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006969 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006970
6971 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6972 path->slots[0],
6973 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006974 }
Josef Bacik25a50342013-10-14 12:08:38 -04006975next:
Yan Zheng9036c102008-10-30 14:19:41 -04006976 if (start >= extent_end) {
6977 path->slots[0]++;
6978 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6979 ret = btrfs_next_leaf(root, path);
6980 if (ret < 0) {
6981 err = ret;
6982 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006983 }
Yan Zheng9036c102008-10-30 14:19:41 -04006984 if (ret > 0)
6985 goto not_found;
6986 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006987 }
Yan Zheng9036c102008-10-30 14:19:41 -04006988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6989 if (found_key.objectid != objectid ||
6990 found_key.type != BTRFS_EXTENT_DATA_KEY)
6991 goto not_found;
6992 if (start + len <= found_key.offset)
6993 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006994 if (start > found_key.offset)
6995 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006996 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006997 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006998 em->len = found_key.offset - start;
6999 goto not_found_em;
7000 }
7001
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007002 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007003 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007004
Yan Zhengd899e052008-10-30 14:25:28 -04007005 if (found_type == BTRFS_FILE_EXTENT_REG ||
7006 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007007 goto insert;
7008 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007009 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007010 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007011 size_t size;
7012 size_t extent_offset;
7013 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007014
Filipe Manana7ffbb592014-06-09 03:48:05 +01007015 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007016 goto out;
Yan689f9342007-10-29 11:41:07 -04007017
Chris Mason514ac8a2014-01-03 21:07:00 -08007018 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007019 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007020 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7021 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007022 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007023 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007024 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007025 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007026 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007027 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007028 if (btrfs_file_extent_compression(leaf, item) !=
7029 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007030 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007031 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007032 if (ret) {
7033 err = ret;
7034 goto out;
7035 }
Chris Masonc8b97812008-10-29 14:49:59 -04007036 } else {
7037 map = kmap(page);
7038 read_extent_buffer(leaf, map + pg_offset, ptr,
7039 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007040 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007041 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007042 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007043 copy_size);
7044 }
Chris Masonc8b97812008-10-29 14:49:59 -04007045 kunmap(page);
7046 }
Chris Mason179e29e2007-11-01 11:28:41 -04007047 flush_dcache_page(page);
7048 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007049 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007050 if (!trans) {
7051 kunmap(page);
7052 free_extent_map(em);
7053 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007054
David Sterbab3b4aa72011-04-21 01:20:15 +02007055 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007056 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007057
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007058 if (IS_ERR(trans))
7059 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007060 goto again;
7061 }
Chris Masonc8b97812008-10-29 14:49:59 -04007062 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007063 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007064 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007065 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007066 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Masond1310b22008-01-24 16:13:08 -05007068 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007069 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007071 }
7072not_found:
7073 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007074 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007075 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007076not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007077 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007078 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007079insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007080 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007081 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007082 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007083 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7084 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007085 err = -EIO;
7086 goto out;
7087 }
Chris Masond1310b22008-01-24 16:13:08 -05007088
7089 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007090 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007091 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007092 /* it is possible that someone inserted the extent into the tree
7093 * while we had the lock dropped. It is also possible that
7094 * an overlapping map exists in the tree
7095 */
Chris Masona52d9a82007-08-27 16:49:44 -04007096 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007097 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007098
7099 ret = 0;
7100
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007101 existing = search_extent_mapping(em_tree, start, len);
7102 /*
7103 * existing will always be non-NULL, since there must be
7104 * extent causing the -EEXIST.
7105 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007106 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007107 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007108 em->block_start == existing->block_start) {
7109 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007110 * The existing extent map already encompasses the
7111 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007112 */
7113 free_extent_map(em);
7114 em = existing;
7115 err = 0;
7116
7117 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007118 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007119 /*
7120 * The existing extent map is the one nearest to
7121 * the [start, start + len) range which overlaps
7122 */
7123 err = merge_extent_mapping(em_tree, existing,
7124 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007125 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007126 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007127 free_extent_map(em);
7128 em = NULL;
7129 }
7130 } else {
7131 free_extent_map(em);
7132 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007133 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
Chris Masona52d9a82007-08-27 16:49:44 -04007135 }
Chris Mason890871b2009-09-02 16:24:52 -04007136 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007137out:
liubo1abe9b82011-03-24 11:18:59 +00007138
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007139 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007140
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007141 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007142 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007143 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007144 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007145 err = ret;
7146 }
Chris Masona52d9a82007-08-27 16:49:44 -04007147 if (err) {
7148 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007149 return ERR_PTR(err);
7150 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007151 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007152 return em;
7153}
7154
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007155struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7156 struct page *page,
7157 size_t pg_offset, u64 start, u64 len,
7158 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007159{
7160 struct extent_map *em;
7161 struct extent_map *hole_em = NULL;
7162 u64 range_start = start;
7163 u64 end;
7164 u64 found;
7165 u64 found_end;
7166 int err = 0;
7167
7168 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7169 if (IS_ERR(em))
7170 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007171 /*
7172 * If our em maps to:
7173 * - a hole or
7174 * - a pre-alloc extent,
7175 * there might actually be delalloc bytes behind it.
7176 */
7177 if (em->block_start != EXTENT_MAP_HOLE &&
7178 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7179 return em;
7180 else
7181 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007182
7183 /* check to see if we've wrapped (len == -1 or similar) */
7184 end = start + len;
7185 if (end < start)
7186 end = (u64)-1;
7187 else
7188 end -= 1;
7189
7190 em = NULL;
7191
7192 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007193 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007194 end, len, EXTENT_DELALLOC, 1);
7195 found_end = range_start + found;
7196 if (found_end < range_start)
7197 found_end = (u64)-1;
7198
7199 /*
7200 * we didn't find anything useful, return
7201 * the original results from get_extent()
7202 */
7203 if (range_start > end || found_end <= start) {
7204 em = hole_em;
7205 hole_em = NULL;
7206 goto out;
7207 }
7208
7209 /* adjust the range_start to make sure it doesn't
7210 * go backwards from the start they passed in
7211 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307212 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007213 found = found_end - range_start;
7214
7215 if (found > 0) {
7216 u64 hole_start = start;
7217 u64 hole_len = len;
7218
David Sterba172ddd62011-04-21 00:48:27 +02007219 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007220 if (!em) {
7221 err = -ENOMEM;
7222 goto out;
7223 }
7224 /*
7225 * when btrfs_get_extent can't find anything it
7226 * returns one huge hole
7227 *
7228 * make sure what it found really fits our range, and
7229 * adjust to make sure it is based on the start from
7230 * the caller
7231 */
7232 if (hole_em) {
7233 u64 calc_end = extent_map_end(hole_em);
7234
7235 if (calc_end <= start || (hole_em->start > end)) {
7236 free_extent_map(hole_em);
7237 hole_em = NULL;
7238 } else {
7239 hole_start = max(hole_em->start, start);
7240 hole_len = calc_end - hole_start;
7241 }
7242 }
7243 em->bdev = NULL;
7244 if (hole_em && range_start > hole_start) {
7245 /* our hole starts before our delalloc, so we
7246 * have to return just the parts of the hole
7247 * that go until the delalloc starts
7248 */
7249 em->len = min(hole_len,
7250 range_start - hole_start);
7251 em->start = hole_start;
7252 em->orig_start = hole_start;
7253 /*
7254 * don't adjust block start at all,
7255 * it is fixed at EXTENT_MAP_HOLE
7256 */
7257 em->block_start = hole_em->block_start;
7258 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007259 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7260 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007261 } else {
7262 em->start = range_start;
7263 em->len = found;
7264 em->orig_start = range_start;
7265 em->block_start = EXTENT_MAP_DELALLOC;
7266 em->block_len = found;
7267 }
7268 } else if (hole_em) {
7269 return hole_em;
7270 }
7271out:
7272
7273 free_extent_map(hole_em);
7274 if (err) {
7275 free_extent_map(em);
7276 return ERR_PTR(err);
7277 }
7278 return em;
7279}
7280
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007281static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7282 const u64 start,
7283 const u64 len,
7284 const u64 orig_start,
7285 const u64 block_start,
7286 const u64 block_len,
7287 const u64 orig_block_len,
7288 const u64 ram_bytes,
7289 const int type)
7290{
7291 struct extent_map *em = NULL;
7292 int ret;
7293
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007294 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007295 em = create_io_em(inode, start, len, orig_start,
7296 block_start, block_len, orig_block_len,
7297 ram_bytes,
7298 BTRFS_COMPRESS_NONE, /* compress_type */
7299 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007300 if (IS_ERR(em))
7301 goto out;
7302 }
7303 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7304 len, block_len, type);
7305 if (ret) {
7306 if (em) {
7307 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007308 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007309 start + len - 1, 0);
7310 }
7311 em = ERR_PTR(ret);
7312 }
7313 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007314
7315 return em;
7316}
7317
Josef Bacik4b46fce2010-05-23 11:00:55 -04007318static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7319 u64 start, u64 len)
7320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007323 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007324 struct btrfs_key ins;
7325 u64 alloc_hint;
7326 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007327
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007329 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007330 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007331 if (ret)
7332 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007333
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007334 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7335 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007336 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007337 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007338 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007339 btrfs_free_reserved_extent(fs_info, ins.objectid,
7340 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007341
Josef Bacik4b46fce2010-05-23 11:00:55 -04007342 return em;
7343}
7344
Chris Mason46bfbb52010-05-26 11:04:10 -04007345/*
7346 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7347 * block must be cow'd
7348 */
Josef Bacik00361582013-08-14 14:02:47 -04007349noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007350 u64 *orig_start, u64 *orig_block_len,
7351 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007352{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007354 struct btrfs_path *path;
7355 int ret;
7356 struct extent_buffer *leaf;
7357 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007358 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007359 struct btrfs_file_extent_item *fi;
7360 struct btrfs_key key;
7361 u64 disk_bytenr;
7362 u64 backref_offset;
7363 u64 extent_end;
7364 u64 num_bytes;
7365 int slot;
7366 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007368
Chris Mason46bfbb52010-05-26 11:04:10 -04007369 path = btrfs_alloc_path();
7370 if (!path)
7371 return -ENOMEM;
7372
David Sterbaf85b7372017-01-20 14:54:07 +01007373 ret = btrfs_lookup_file_extent(NULL, root, path,
7374 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007375 if (ret < 0)
7376 goto out;
7377
7378 slot = path->slots[0];
7379 if (ret == 1) {
7380 if (slot == 0) {
7381 /* can't find the item, must cow */
7382 ret = 0;
7383 goto out;
7384 }
7385 slot--;
7386 }
7387 ret = 0;
7388 leaf = path->nodes[0];
7389 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007390 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 key.type != BTRFS_EXTENT_DATA_KEY) {
7392 /* not our file or wrong item type, must cow */
7393 goto out;
7394 }
7395
7396 if (key.offset > offset) {
7397 /* Wrong offset, must cow */
7398 goto out;
7399 }
7400
7401 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7402 found_type = btrfs_file_extent_type(leaf, fi);
7403 if (found_type != BTRFS_FILE_EXTENT_REG &&
7404 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7405 /* not a regular extent, must cow */
7406 goto out;
7407 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007408
7409 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7410 goto out;
7411
Miao Xiee77751a2013-12-27 21:11:50 +08007412 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7413 if (extent_end <= offset)
7414 goto out;
7415
Chris Mason46bfbb52010-05-26 11:04:10 -04007416 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007417 if (disk_bytenr == 0)
7418 goto out;
7419
7420 if (btrfs_file_extent_compression(leaf, fi) ||
7421 btrfs_file_extent_encryption(leaf, fi) ||
7422 btrfs_file_extent_other_encoding(leaf, fi))
7423 goto out;
7424
Chris Mason46bfbb52010-05-26 11:04:10 -04007425 backref_offset = btrfs_file_extent_offset(leaf, fi);
7426
Josef Bacik7ee9e442013-06-21 16:37:03 -04007427 if (orig_start) {
7428 *orig_start = key.offset - backref_offset;
7429 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7430 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7431 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007432
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007433 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007434 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007435
7436 num_bytes = min(offset + *len, extent_end) - offset;
7437 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7438 u64 range_end;
7439
Jeff Mahoneyda170662016-06-15 09:22:56 -04007440 range_end = round_up(offset + num_bytes,
7441 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007442 ret = test_range_bit(io_tree, offset, range_end,
7443 EXTENT_DELALLOC, 0, NULL);
7444 if (ret) {
7445 ret = -EAGAIN;
7446 goto out;
7447 }
7448 }
7449
Josef Bacik1bda19e2013-10-18 12:10:36 -04007450 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007451
7452 /*
7453 * look for other files referencing this extent, if we
7454 * find any we must cow
7455 */
Josef Bacik00361582013-08-14 14:02:47 -04007456
Liu Boe4c3b2d2017-01-30 12:25:28 -08007457 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007458 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007459 if (ret) {
7460 ret = 0;
7461 goto out;
7462 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007463
7464 /*
7465 * adjust disk_bytenr and num_bytes to cover just the bytes
7466 * in this extent we are about to write. If there
7467 * are any csums in that range we have to cow in order
7468 * to keep the csums correct
7469 */
7470 disk_bytenr += backref_offset;
7471 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007472 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7473 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 /*
7475 * all of the above have passed, it is safe to overwrite this extent
7476 * without cow
7477 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007478 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 ret = 1;
7480out:
7481 btrfs_free_path(path);
7482 return ret;
7483}
7484
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007485bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7486{
7487 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007488 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007489 void **pagep = NULL;
7490 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007491 unsigned long start_idx;
7492 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007493
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007494 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007495
7496 /*
7497 * end is the last byte in the last page. end == start is legal
7498 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007499 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007500
7501 rcu_read_lock();
7502
7503 /* Most of the code in this while loop is lifted from
7504 * find_get_page. It's been modified to begin searching from a
7505 * page and return just the first page found in that range. If the
7506 * found idx is less than or equal to the end idx then we know that
7507 * a page exists. If no pages are found or if those pages are
7508 * outside of the range then we're fine (yay!) */
7509 while (page == NULL &&
7510 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7511 page = radix_tree_deref_slot(pagep);
7512 if (unlikely(!page))
7513 break;
7514
7515 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007516 if (radix_tree_deref_retry(page)) {
7517 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007518 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007519 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007520 /*
7521 * Otherwise, shmem/tmpfs must be storing a swap entry
7522 * here as an exceptional entry: so return it without
7523 * attempting to raise page count.
7524 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007525 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007526 break; /* TODO: Is this relevant for this use case? */
7527 }
7528
Filipe Manana91405152014-06-05 13:22:24 +01007529 if (!page_cache_get_speculative(page)) {
7530 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007531 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007532 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007533
7534 /*
7535 * Has the page moved?
7536 * This is part of the lockless pagecache protocol. See
7537 * include/linux/pagemap.h for details.
7538 */
7539 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007540 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007541 page = NULL;
7542 }
7543 }
7544
7545 if (page) {
7546 if (page->index <= end_idx)
7547 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007548 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007549 }
7550
7551 rcu_read_unlock();
7552 return found;
7553}
7554
Josef Bacikeb838e72012-07-31 16:28:48 -04007555static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7556 struct extent_state **cached_state, int writing)
7557{
7558 struct btrfs_ordered_extent *ordered;
7559 int ret = 0;
7560
7561 while (1) {
7562 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007563 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007564 /*
7565 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007566 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 * extents in this range.
7568 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007569 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007570 lockend - lockstart + 1);
7571
7572 /*
7573 * We need to make sure there are no buffered pages in this
7574 * range either, we could have raced between the invalidate in
7575 * generic_file_direct_write and locking the extent. The
7576 * invalidate needs to happen so that reads after a write do not
7577 * get stale data.
7578 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007579 if (!ordered &&
7580 (!writing ||
7581 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007582 break;
7583
7584 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7585 cached_state, GFP_NOFS);
7586
7587 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007588 /*
7589 * If we are doing a DIO read and the ordered extent we
7590 * found is for a buffered write, we can not wait for it
7591 * to complete and retry, because if we do so we can
7592 * deadlock with concurrent buffered writes on page
7593 * locks. This happens only if our DIO read covers more
7594 * than one extent map, if at this point has already
7595 * created an ordered extent for a previous extent map
7596 * and locked its range in the inode's io tree, and a
7597 * concurrent write against that previous extent map's
7598 * range and this range started (we unlock the ranges
7599 * in the io tree only when the bios complete and
7600 * buffered writes always lock pages before attempting
7601 * to lock range in the io tree).
7602 */
7603 if (writing ||
7604 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7605 btrfs_start_ordered_extent(inode, ordered, 1);
7606 else
7607 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007608 btrfs_put_ordered_extent(ordered);
7609 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007611 * We could trigger writeback for this range (and wait
7612 * for it to complete) and then invalidate the pages for
7613 * this range (through invalidate_inode_pages2_range()),
7614 * but that can lead us to a deadlock with a concurrent
7615 * call to readpages() (a buffered read or a defrag call
7616 * triggered a readahead) on a page lock due to an
7617 * ordered dio extent we created before but did not have
7618 * yet a corresponding bio submitted (whence it can not
7619 * complete), which makes readpages() wait for that
7620 * ordered extent to complete while holding a lock on
7621 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007623 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 }
7625
Filipe Mananaade77022016-02-18 14:28:55 +00007626 if (ret)
7627 break;
7628
Josef Bacikeb838e72012-07-31 16:28:48 -04007629 cond_resched();
7630 }
7631
7632 return ret;
7633}
7634
Liu Bo6f9994d2017-01-31 07:50:22 -08007635/* The callers of this must take lock_extent() */
7636static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7637 u64 orig_start, u64 block_start,
7638 u64 block_len, u64 orig_block_len,
7639 u64 ram_bytes, int compress_type,
7640 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007641{
7642 struct extent_map_tree *em_tree;
7643 struct extent_map *em;
7644 struct btrfs_root *root = BTRFS_I(inode)->root;
7645 int ret;
7646
Liu Bo6f9994d2017-01-31 07:50:22 -08007647 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7648 type == BTRFS_ORDERED_COMPRESSED ||
7649 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007650 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007651
Josef Bacik69ffb542012-09-11 15:40:07 -04007652 em_tree = &BTRFS_I(inode)->extent_tree;
7653 em = alloc_extent_map();
7654 if (!em)
7655 return ERR_PTR(-ENOMEM);
7656
7657 em->start = start;
7658 em->orig_start = orig_start;
7659 em->len = len;
7660 em->block_len = block_len;
7661 em->block_start = block_start;
7662 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007663 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007664 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007665 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007666 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007667 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007668 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007669 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007670 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7671 em->compress_type = compress_type;
7672 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007673
7674 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007675 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007676 em->start + em->len - 1, 0);
7677 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007678 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007679 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007680 /*
7681 * The caller has taken lock_extent(), who could race with us
7682 * to add em?
7683 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007684 } while (ret == -EEXIST);
7685
7686 if (ret) {
7687 free_extent_map(em);
7688 return ERR_PTR(ret);
7689 }
7690
Liu Bo6f9994d2017-01-31 07:50:22 -08007691 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007692 return em;
7693}
7694
Filipe Manana9c9464c2015-11-04 09:52:04 +00007695static void adjust_dio_outstanding_extents(struct inode *inode,
7696 struct btrfs_dio_data *dio_data,
7697 const u64 len)
7698{
David Sterba823bb202017-01-04 11:09:51 +01007699 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007700
Filipe Manana9c9464c2015-11-04 09:52:04 +00007701 /*
7702 * If we have an outstanding_extents count still set then we're
7703 * within our reservation, otherwise we need to adjust our inode
7704 * counter appropriately.
7705 */
Liu Boc2931662016-12-22 17:13:54 -08007706 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007707 dio_data->outstanding_extents -= num_extents;
7708 } else {
Liu Boc2931662016-12-22 17:13:54 -08007709 /*
7710 * If dio write length has been split due to no large enough
7711 * contiguous space, we need to compensate our inode counter
7712 * appropriately.
7713 */
7714 u64 num_needed = num_extents - dio_data->outstanding_extents;
7715
Filipe Manana9c9464c2015-11-04 09:52:04 +00007716 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007717 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007718 spin_unlock(&BTRFS_I(inode)->lock);
7719 }
7720}
7721
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7723 struct buffer_head *bh_result, int create)
7724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307728 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007730 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007731 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007732 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007733 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007734
Miao Xie172a5042013-02-21 02:48:22 -07007735 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007736 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007737 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007738 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007739
Josef Bacikc3298612012-08-03 16:49:19 -04007740 lockstart = start;
7741 lockend = start + len - 1;
7742
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007743 if (current->journal_info) {
7744 /*
7745 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007746 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007747 * confused.
7748 */
chandan50745b02015-08-28 21:10:13 +05307749 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007750 current->journal_info = NULL;
7751 }
7752
Josef Bacikeb838e72012-07-31 16:28:48 -04007753 /*
7754 * If this errors out it's because we couldn't invalidate pagecache for
7755 * this range and we need to fallback to buffered.
7756 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007757 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7758 create)) {
7759 ret = -ENOTBLK;
7760 goto err;
7761 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007762
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007763 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007764 if (IS_ERR(em)) {
7765 ret = PTR_ERR(em);
7766 goto unlock_err;
7767 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768
7769 /*
7770 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7771 * io. INLINE is special, and we could probably kludge it in here, but
7772 * it's still buffered so for safety lets just fall back to the generic
7773 * buffered path.
7774 *
7775 * For COMPRESSED we _have_ to read the entire extent in so we can
7776 * decompress it, so there will be buffering required no matter what we
7777 * do, so go ahead and fallback to buffered.
7778 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007779 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007780 * to buffered IO. Don't blame me, this is the price we pay for using
7781 * the generic code.
7782 */
7783 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7784 em->block_start == EXTENT_MAP_INLINE) {
7785 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007786 ret = -ENOTBLK;
7787 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007788 }
7789
7790 /* Just a good old fashioned hole, return */
7791 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7792 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7793 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007794 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007795 }
7796
7797 /*
7798 * We don't allocate a new extent in the following cases
7799 *
7800 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7801 * existing extent.
7802 * 2) The extent is marked as PREALLOC. We're good to go here and can
7803 * just use the extent.
7804 *
7805 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007806 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007807 len = min(len, em->len - (start - em->start));
7808 lockstart = start + len;
7809 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007810 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811
7812 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7813 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7814 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007815 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007816 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817
7818 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7819 type = BTRFS_ORDERED_PREALLOC;
7820 else
7821 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007822 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007823 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007824
Josef Bacik00361582013-08-14 14:02:47 -04007825 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007826 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007827 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007828 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007829
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007830 em2 = btrfs_create_dio_extent(inode, start, len,
7831 orig_start, block_start,
7832 len, orig_block_len,
7833 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007834 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007835 if (type == BTRFS_ORDERED_PREALLOC) {
7836 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007837 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007838 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007839 if (em2 && IS_ERR(em2)) {
7840 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007841 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007842 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007843 /*
7844 * For inode marked NODATACOW or extent marked PREALLOC,
7845 * use the existing or preallocated extent, so does not
7846 * need to adjust btrfs_space_info's bytes_may_use.
7847 */
7848 btrfs_free_reserved_data_space_noquota(inode,
7849 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007850 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852 }
Josef Bacik00361582013-08-14 14:02:47 -04007853
Chris Mason46bfbb52010-05-26 11:04:10 -04007854 /*
7855 * this will cow the extent, reset the len in case we changed
7856 * it above
7857 */
7858 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007859 free_extent_map(em);
7860 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007861 if (IS_ERR(em)) {
7862 ret = PTR_ERR(em);
7863 goto unlock_err;
7864 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007865 len = min(len, em->len - (start - em->start));
7866unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007867 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7868 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007869 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007870 bh_result->b_bdev = em->bdev;
7871 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007872 if (create) {
7873 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7874 set_buffer_new(bh_result);
7875
7876 /*
7877 * Need to update the i_size under the extent lock so buffered
7878 * readers will get the updated i_size when we unlock.
7879 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007880 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007881 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007882
Filipe Manana9c9464c2015-11-04 09:52:04 +00007883 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307884 WARN_ON(dio_data->reserve < len);
7885 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007886 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307887 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007888 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 /*
7891 * In the case of write we need to clear and unlock the entire range,
7892 * in the case of read we need to unlock only the end area that we
7893 * aren't using if there is any left over space.
7894 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007895 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007896 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7897 lockend, unlock_bits, 1, 0,
7898 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007899 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007900 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007901 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007902
Josef Bacik4b46fce2010-05-23 11:00:55 -04007903 free_extent_map(em);
7904
7905 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007906
7907unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007908 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7909 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007910err:
chandan50745b02015-08-28 21:10:13 +05307911 if (dio_data)
7912 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007913 /*
7914 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7915 * write less data then expected, so that we don't underflow our inode's
7916 * outstanding extents counter.
7917 */
7918 if (create && dio_data)
7919 adjust_dio_outstanding_extents(inode, dio_data, len);
7920
Josef Bacikeb838e72012-07-31 16:28:48 -04007921 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922}
7923
Miao Xie8b110e32014-09-12 18:44:03 +08007924static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007925 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007926{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007927 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007928 int ret;
7929
Mike Christie37226b22016-06-05 14:31:52 -05007930 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007931
7932 bio_get(bio);
7933
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007934 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007935 if (ret)
7936 goto err;
7937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007938 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007939err:
7940 bio_put(bio);
7941 return ret;
7942}
7943
7944static int btrfs_check_dio_repairable(struct inode *inode,
7945 struct bio *failed_bio,
7946 struct io_failure_record *failrec,
7947 int failed_mirror)
7948{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007950 int num_copies;
7951
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007952 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007953 if (num_copies == 1) {
7954 /*
7955 * we only have a single copy of the data, so don't bother with
7956 * all the retry and error correction code that follows. no
7957 * matter what the error is, it is very likely to persist.
7958 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007959 btrfs_debug(fs_info,
7960 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7961 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007962 return 0;
7963 }
7964
7965 failrec->failed_mirror = failed_mirror;
7966 failrec->this_mirror++;
7967 if (failrec->this_mirror == failed_mirror)
7968 failrec->this_mirror++;
7969
7970 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007971 btrfs_debug(fs_info,
7972 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7973 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007974 return 0;
7975 }
7976
7977 return 1;
7978}
7979
7980static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 struct page *page, unsigned int pgoff,
7982 u64 start, u64 end, int failed_mirror,
7983 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007984{
7985 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7987 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007988 struct bio *bio;
7989 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007990 int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007991 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 int ret;
7993
Mike Christie37226b22016-06-05 14:31:52 -05007994 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007995
7996 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7997 if (ret)
7998 return ret;
7999
8000 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8001 failed_mirror);
8002 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008003 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008004 return -EIO;
8005 }
8006
Liu Bo17347ce2017-05-15 15:33:27 -07008007 segs = bio_segments(failed_bio);
8008 if (segs > 1 ||
8009 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008010 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008011
8012 isector = start - btrfs_io_bio(failed_bio)->logical;
8013 isector >>= inode->i_sb->s_blocksize_bits;
8014 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008016 if (!bio) {
Josef Bacik7870d082017-05-05 11:57:15 -04008017 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008018 return -EIO;
8019 }
Mike Christie37226b22016-06-05 14:31:52 -05008020 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008021
8022 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8023 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8024 read_mode, failrec->this_mirror, failrec->in_validation);
8025
Mike Christie81a75f672016-06-05 14:31:54 -05008026 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008027 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008028 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008029 bio_put(bio);
8030 }
8031
8032 return ret;
8033}
8034
8035struct btrfs_retry_complete {
8036 struct completion done;
8037 struct inode *inode;
8038 u64 start;
8039 int uptodate;
8040};
8041
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008042static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008043{
8044 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008045 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008046 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008047 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008048 int i;
8049
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008050 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008051 goto end;
8052
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008054 io_tree = &BTRFS_I(inode)->io_tree;
8055 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8056 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057
Miao Xie8b110e32014-09-12 18:44:03 +08008058 done->uptodate = 1;
8059 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008060 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8061 io_tree, done->start, bvec->bv_page,
8062 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008063end:
8064 complete(&done->done);
8065 bio_put(bio);
8066}
8067
8068static int __btrfs_correct_data_nocsum(struct inode *inode,
8069 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008070{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008072 struct bio_vec bvec;
8073 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008074 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008075 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 unsigned int pgoff;
8077 u32 sectorsize;
8078 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008079 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008080 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008081
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008083 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084
Miao Xiec1dc0892014-09-12 18:43:56 +08008085 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008086 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008087 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008088
Liu Bo17347ce2017-05-15 15:33:27 -07008089 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8090 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8091 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308092
8093next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008094 done.uptodate = 0;
8095 done.start = start;
8096 init_completion(&done.done);
8097
Liu Bo17347ce2017-05-15 15:33:27 -07008098 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099 pgoff, start, start + sectorsize - 1,
8100 io_bio->mirror_num,
8101 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008102 if (ret) {
8103 err = ret;
8104 goto next;
8105 }
Miao Xie8b110e32014-09-12 18:44:03 +08008106
8107 wait_for_completion(&done.done);
8108
8109 if (!done.uptodate) {
8110 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008112 }
8113
Liu Bo629ebf42017-05-15 17:20:07 -07008114next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308115 start += sectorsize;
8116
Liu Bo97bf5a52017-04-07 13:11:10 -07008117 nr_sectors--;
8118 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308119 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008120 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 goto next_block_or_try_again;
8122 }
Miao Xie8b110e32014-09-12 18:44:03 +08008123 }
8124
Liu Bo629ebf42017-05-15 17:20:07 -07008125 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008126}
8127
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008128static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008129{
8130 struct btrfs_retry_complete *done = bio->bi_private;
8131 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008132 struct extent_io_tree *io_tree, *failure_tree;
8133 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008134 struct bio_vec *bvec;
8135 int uptodate;
8136 int ret;
8137 int i;
8138
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008139 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008140 goto end;
8141
8142 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308143
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308144 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008145 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146
Josef Bacik7870d082017-05-05 11:57:15 -04008147 io_tree = &BTRFS_I(inode)->io_tree;
8148 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8149
Miao Xie8b110e32014-09-12 18:44:03 +08008150 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008151 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8152 bvec->bv_offset, done->start,
8153 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008154 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008155 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8156 failure_tree, io_tree, done->start,
8157 bvec->bv_page,
8158 btrfs_ino(BTRFS_I(inode)),
8159 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008160 else
8161 uptodate = 0;
8162 }
8163
8164 done->uptodate = uptodate;
8165end:
8166 complete(&done->done);
8167 bio_put(bio);
8168}
8169
8170static int __btrfs_subio_endio_read(struct inode *inode,
8171 struct btrfs_io_bio *io_bio, int err)
8172{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308173 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008174 struct bio_vec bvec;
8175 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008176 struct btrfs_retry_complete done;
8177 u64 start;
8178 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308179 u32 sectorsize;
8180 int nr_sectors;
8181 unsigned int pgoff;
8182 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008183 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008184 int ret;
8185
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308186 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008187 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308188
Miao Xie8b110e32014-09-12 18:44:03 +08008189 err = 0;
8190 start = io_bio->logical;
8191 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008192 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008193
Liu Bo17347ce2017-05-15 15:33:27 -07008194 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8195 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308196
Liu Bo17347ce2017-05-15 15:33:27 -07008197 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308198next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008199 if (uptodate) {
8200 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8201 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8202 bvec.bv_page, pgoff, start, sectorsize);
8203 if (likely(!ret))
8204 goto next;
8205 }
Miao Xie8b110e32014-09-12 18:44:03 +08008206try_again:
8207 done.uptodate = 0;
8208 done.start = start;
8209 init_completion(&done.done);
8210
Liu Bo17347ce2017-05-15 15:33:27 -07008211 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308212 pgoff, start, start + sectorsize - 1,
8213 io_bio->mirror_num,
8214 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008215 if (ret) {
8216 err = ret;
8217 goto next;
8218 }
8219
8220 wait_for_completion(&done.done);
8221
8222 if (!done.uptodate) {
8223 /* We might have another mirror, so try again */
8224 goto try_again;
8225 }
8226next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308227 offset += sectorsize;
8228 start += sectorsize;
8229
8230 ASSERT(nr_sectors);
8231
Liu Bo97bf5a52017-04-07 13:11:10 -07008232 nr_sectors--;
8233 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308234 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008235 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308236 goto next_block;
8237 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008238 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008239
8240 return err;
8241}
8242
Miao Xie8b110e32014-09-12 18:44:03 +08008243static int btrfs_subio_endio_read(struct inode *inode,
8244 struct btrfs_io_bio *io_bio, int err)
8245{
8246 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8247
8248 if (skip_csum) {
8249 if (unlikely(err))
8250 return __btrfs_correct_data_nocsum(inode, io_bio);
8251 else
8252 return 0;
8253 } else {
8254 return __btrfs_subio_endio_read(inode, io_bio, err);
8255 }
8256}
8257
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008258static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008259{
8260 struct btrfs_dio_private *dip = bio->bi_private;
8261 struct inode *inode = dip->inode;
8262 struct bio *dio_bio;
8263 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008264 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008265
Liu Boe3d37fa2017-05-17 15:42:00 -06008266 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008267 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008268 if (!err)
8269 bio->bi_error = 0;
8270 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008271
Josef Bacik4b46fce2010-05-23 11:00:55 -04008272 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008273 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008274 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008275
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008277
Filipe Manana1636d1d2016-02-15 16:20:26 +00008278 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008279 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008280
8281 if (io_bio->end_io)
8282 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008283 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008284}
8285
Qu Wenruo524272602017-03-08 10:25:52 +08008286static void __endio_write_update_ordered(struct inode *inode,
8287 const u64 offset, const u64 bytes,
8288 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008290 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008291 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008292 struct btrfs_workqueue *wq;
8293 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008294 u64 ordered_offset = offset;
8295 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008296 int ret;
8297
Qu Wenruo524272602017-03-08 10:25:52 +08008298 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8299 wq = fs_info->endio_freespace_worker;
8300 func = btrfs_freespace_write_helper;
8301 } else {
8302 wq = fs_info->endio_write_workers;
8303 func = btrfs_endio_write_helper;
8304 }
8305
Chris Mason163cf092010-11-28 19:56:33 -05008306again:
8307 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8308 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008309 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008310 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008311 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008312 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008313
Qu Wenruo524272602017-03-08 10:25:52 +08008314 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8315 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008316out_test:
8317 /*
8318 * our bio might span multiple ordered extents. If we haven't
8319 * completed the accounting for the whole dio, go back and try again
8320 */
Filipe Manana14543772015-11-24 16:23:54 +00008321 if (ordered_offset < offset + bytes) {
8322 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008323 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008324 goto again;
8325 }
Filipe Manana14543772015-11-24 16:23:54 +00008326}
8327
8328static void btrfs_endio_direct_write(struct bio *bio)
8329{
8330 struct btrfs_dio_private *dip = bio->bi_private;
8331 struct bio *dio_bio = dip->dio_bio;
8332
Qu Wenruo524272602017-03-08 10:25:52 +08008333 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8334 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008335
Josef Bacik4b46fce2010-05-23 11:00:55 -04008336 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008337
Filipe Manana1636d1d2016-02-15 16:20:26 +00008338 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008339 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008340 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008341}
8342
Josef Bacikc6100a42017-05-05 11:57:13 -04008343static int __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008344 struct bio *bio, int mirror_num,
8345 unsigned long bio_flags, u64 offset)
8346{
Josef Bacikc6100a42017-05-05 11:57:13 -04008347 struct inode *inode = private_data;
Chris Masoneaf25d92010-05-25 09:48:28 -04008348 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008349 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008350 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008351 return 0;
8352}
8353
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008354static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008355{
8356 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008357 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008358
Miao Xie8b110e32014-09-12 18:44:03 +08008359 if (err)
8360 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008361 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008362 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8363 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008364 (unsigned long long)bio->bi_iter.bi_sector,
8365 bio->bi_iter.bi_size, err);
8366
8367 if (dip->subio_endio)
8368 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008369
8370 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008371 dip->errors = 1;
8372
8373 /*
8374 * before atomic variable goto zero, we must make sure
8375 * dip->errors is perceived to be set.
8376 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008377 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008378 }
8379
8380 /* if there are more bios still pending for this dio, just exit */
8381 if (!atomic_dec_and_test(&dip->pending_bios))
8382 goto out;
8383
Chris Mason9be33952013-05-17 18:30:14 -04008384 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008385 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008386 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008387 dip->dio_bio->bi_error = 0;
8388 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008389 }
8390out:
8391 bio_put(bio);
8392}
8393
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008394static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008395 struct btrfs_dio_private *dip,
8396 struct bio *bio,
8397 u64 file_offset)
8398{
8399 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8400 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8401 int ret;
8402
8403 /*
8404 * We load all the csum data we need when we submit
8405 * the first bio to reduce the csum tree search and
8406 * contention.
8407 */
8408 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008409 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008410 file_offset);
8411 if (ret)
8412 return ret;
8413 }
8414
8415 if (bio == dip->orig_bio)
8416 return 0;
8417
8418 file_offset -= dip->logical_offset;
8419 file_offset >>= inode->i_sb->s_blocksize_bits;
8420 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8421
8422 return 0;
8423}
8424
Miao Xiee65e1532010-11-22 03:04:43 +00008425static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008426 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008427 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008430 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008431 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008432 int ret;
8433
Josef Bacikb812ce22012-11-16 13:56:32 -05008434 if (async_submit)
8435 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8436
Miao Xiee65e1532010-11-22 03:04:43 +00008437 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008438
8439 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008440 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008441 if (ret)
8442 goto err;
8443 }
Miao Xiee65e1532010-11-22 03:04:43 +00008444
Josef Bacik1ae39932011-04-06 14:41:34 -04008445 if (skip_sum)
8446 goto map;
8447
8448 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008449 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8450 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008451 __btrfs_submit_bio_start_direct_io,
8452 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008453 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008454 } else if (write) {
8455 /*
8456 * If we aren't doing async submit, calculate the csum of the
8457 * bio now.
8458 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008459 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008460 if (ret)
8461 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008462 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008463 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008464 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008465 if (ret)
8466 goto err;
8467 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008468map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008469 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008470err:
8471 bio_put(bio);
8472 return ret;
8473}
8474
Mike Christie81a75f672016-06-05 14:31:54 -05008475static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008476 int skip_sum)
8477{
8478 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008479 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008480 struct bio *bio;
8481 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008482 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008483 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008484 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008485 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008486 u64 submit_len;
8487 int clone_offset = 0;
8488 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308489 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008490
Kent Overstreet4f024f32013-10-11 15:44:27 -07008491 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008492 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008493 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8494 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008495 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008496 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008497
Liu Bo725130b2017-05-16 09:51:39 -07008498 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008499 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008500 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008501 goto submit;
8502 }
8503
David Woodhouse53b381b2013-01-29 18:40:14 -05008504 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008505 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008506 async_submit = 0;
8507 else
8508 async_submit = 1;
8509
Liu Bo725130b2017-05-16 09:51:39 -07008510 /* bio split */
8511 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008512 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008513 do {
Liu Bo725130b2017-05-16 09:51:39 -07008514 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008515
Liu Bo725130b2017-05-16 09:51:39 -07008516 /*
8517 * This will never fail as it's passing GPF_NOFS and
8518 * the allocation is backed by btrfs_bioset.
8519 */
Liu Boe4770942017-05-16 10:57:14 -07008520 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008521 clone_len);
8522 bio->bi_private = dip;
8523 bio->bi_end_io = btrfs_end_dio_bio;
8524 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008525
Liu Bo725130b2017-05-16 09:51:39 -07008526 ASSERT(submit_len >= clone_len);
8527 submit_len -= clone_len;
8528 if (submit_len == 0)
8529 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008530
Liu Bo725130b2017-05-16 09:51:39 -07008531 /*
8532 * Increase the count before we submit the bio so we know
8533 * the end IO handler won't happen before we increase the
8534 * count. Otherwise, the dip might get freed before we're
8535 * done setting it up.
8536 */
8537 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008538
Liu Bo725130b2017-05-16 09:51:39 -07008539 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8540 async_submit);
8541 if (ret) {
8542 bio_put(bio);
8543 atomic_dec(&dip->pending_bios);
8544 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008545 }
Liu Bo725130b2017-05-16 09:51:39 -07008546
8547 clone_offset += clone_len;
8548 start_sector += clone_len >> 9;
8549 file_offset += clone_len;
8550
8551 map_length = submit_len;
8552 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8553 start_sector << 9, &map_length, NULL, 0);
8554 if (ret)
8555 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008556 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008557
Josef Bacik02f57c72011-04-06 14:25:44 -04008558submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008559 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008560 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008561 if (!ret)
8562 return 0;
8563
8564 bio_put(bio);
8565out_err:
8566 dip->errors = 1;
8567 /*
8568 * before atomic variable goto zero, we must
8569 * make sure dip->errors is perceived to be set.
8570 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008571 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008572 if (atomic_dec_and_test(&dip->pending_bios))
8573 bio_io_error(dip->orig_bio);
8574
8575 /* bio_end_io() will handle error, so we needn't return it */
8576 return 0;
8577}
8578
Mike Christie8a4c1e42016-06-05 14:31:50 -05008579static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8580 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008581{
Filipe Manana61de7182015-07-01 12:13:10 +01008582 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008583 struct bio *bio = NULL;
8584 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008585 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008586 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587 int ret = 0;
8588
8589 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8590
David Sterba8b6c1d52017-06-02 17:48:13 +02008591 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008592
Miao Xiec1dc0892014-09-12 18:43:56 +08008593 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008594 if (!dip) {
8595 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008596 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008597 }
8598
8599 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008600 dip->inode = inode;
8601 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008602 dip->bytes = dio_bio->bi_iter.bi_size;
8603 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008604 bio->bi_private = dip;
8605 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008606 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008607 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008608 io_bio = btrfs_io_bio(bio);
8609 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008610
Miao Xiec1dc0892014-09-12 18:43:56 +08008611 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008612 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008613 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008614 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008615 dip->subio_endio = btrfs_subio_endio_read;
8616 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008617
Filipe Mananaf28a4922015-12-08 19:23:20 +00008618 /*
8619 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8620 * even if we fail to submit a bio, because in such case we do the
8621 * corresponding error handling below and it must not be done a second
8622 * time by btrfs_direct_IO().
8623 */
8624 if (write) {
8625 struct btrfs_dio_data *dio_data = current->journal_info;
8626
8627 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8628 dip->bytes;
8629 dio_data->unsubmitted_oe_range_start =
8630 dio_data->unsubmitted_oe_range_end;
8631 }
8632
Mike Christie81a75f672016-06-05 14:31:54 -05008633 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008634 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008635 return;
Chris Mason9be33952013-05-17 18:30:14 -04008636
Liu Bo3892ac92017-04-17 15:00:28 -07008637 if (io_bio->end_io)
8638 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008639
Josef Bacik4b46fce2010-05-23 11:00:55 -04008640free_ordered:
8641 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008642 * If we arrived here it means either we failed to submit the dip
8643 * or we either failed to clone the dio_bio or failed to allocate the
8644 * dip. If we cloned the dio_bio and allocated the dip, we can just
8645 * call bio_endio against our io_bio so that we get proper resource
8646 * cleanup if we fail to submit the dip, otherwise, we must do the
8647 * same as btrfs_endio_direct_[write|read] because we can't call these
8648 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008649 */
Liu Bo3892ac92017-04-17 15:00:28 -07008650 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008651 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008652 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008653 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008654 * and all the cleanup and final put for dio_bio (through
8655 * dio_end_io()).
8656 */
8657 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008658 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008659 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008660 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008661 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008662 file_offset,
8663 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008664 false);
Filipe Manana14543772015-11-24 16:23:54 +00008665 else
Filipe Manana61de7182015-07-01 12:13:10 +01008666 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8667 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008668
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008669 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008670 /*
8671 * Releases and cleans up our dio_bio, no need to bio_put()
8672 * nor bio_endio()/bio_io_error() against dio_bio.
8673 */
8674 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008675 }
Liu Bo3892ac92017-04-17 15:00:28 -07008676 if (bio)
8677 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008678 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008679}
8680
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008681static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8682 struct kiocb *iocb,
8683 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008684{
8685 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008686 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008687 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008688 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008689
8690 if (offset & blocksize_mask)
8691 goto out;
8692
Al Viro28060d52014-03-22 05:15:17 -04008693 if (iov_iter_alignment(iter) & blocksize_mask)
8694 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008695
Al Viro28060d52014-03-22 05:15:17 -04008696 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008697 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008698 return 0;
8699 /*
8700 * Check to make sure we don't have duplicate iov_base's in this
8701 * iovec, if so return EINVAL, otherwise we'll get csum errors
8702 * when reading back.
8703 */
8704 for (seg = 0; seg < iter->nr_segs; seg++) {
8705 for (i = seg + 1; i < iter->nr_segs; i++) {
8706 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008707 goto out;
8708 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008709 }
8710 retval = 0;
8711out:
8712 return retval;
8713}
Josef Bacikeb838e72012-07-31 16:28:48 -04008714
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008715static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008716{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008717 struct file *file = iocb->ki_filp;
8718 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308720 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008721 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008722 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008723 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008724 bool wakeup = true;
8725 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008726 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008727
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008728 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008729 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008730
Jens Axboefe0f07d2015-04-15 17:05:48 -06008731 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008732 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008733
Josef Bacik0e267c42013-07-02 10:38:02 -04008734 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008735 * The generic stuff only does filemap_write_and_wait_range, which
8736 * isn't enough if we've written compressed pages to this area, so
8737 * we need to flush the dirty pages again to make absolutely sure
8738 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008739 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008740 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008741 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8742 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008743 filemap_fdatawrite_range(inode->i_mapping, offset,
8744 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008745
Omar Sandoval6f673762015-03-16 04:33:52 -07008746 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008747 /*
8748 * If the write DIO is beyond the EOF, we need update
8749 * the isize, but it is protected by i_mutex. So we can
8750 * not unlock the i_mutex at this case.
8751 */
8752 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008753 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008754 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008755 relock = true;
8756 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008757 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008758 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008759 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008760 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008761
8762 /*
8763 * We need to know how many extents we reserved so that we can
8764 * do the accounting properly if we go over the number we
8765 * originally calculated. Abuse current->journal_info for this.
8766 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008767 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008768 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008769 dio_data.unsubmitted_oe_range_start = (u64)offset;
8770 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308771 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308772 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008773 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8774 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008775 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008776 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8777 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008778 }
8779
Omar Sandoval17f8c842015-03-16 04:33:50 -07008780 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008781 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008782 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008783 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008784 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308785 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008786 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008787 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308788 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008789 btrfs_delalloc_release_space(inode, offset,
8790 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008791 /*
8792 * On error we might have left some ordered extents
8793 * without submitting corresponding bios for them, so
8794 * cleanup them up to avoid other tasks getting them
8795 * and waiting for them to complete forever.
8796 */
8797 if (dio_data.unsubmitted_oe_range_start <
8798 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008799 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008800 dio_data.unsubmitted_oe_range_start,
8801 dio_data.unsubmitted_oe_range_end -
8802 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008803 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008804 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008805 btrfs_delalloc_release_space(inode, offset,
8806 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008807 }
Miao Xie38851cc2013-02-08 07:04:11 +00008808out:
Miao Xie2e60a512013-02-08 07:01:08 +00008809 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008810 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008811 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008812 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008813
8814 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008815}
8816
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008817#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8818
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008819static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8820 __u64 start, __u64 len)
8821{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008822 int ret;
8823
8824 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8825 if (ret)
8826 return ret;
8827
Chris Masonec29ed52011-02-23 16:23:20 -05008828 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008829}
8830
Chris Mason9ebefb182007-06-15 13:50:00 -04008831int btrfs_readpage(struct file *file, struct page *page)
8832{
Chris Masond1310b22008-01-24 16:13:08 -05008833 struct extent_io_tree *tree;
8834 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008835 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008836}
Chris Mason1832a6d2007-12-21 16:27:21 -05008837
Chris Mason39279cc2007-06-12 06:35:45 -04008838static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8839{
Chris Masond1310b22008-01-24 16:13:08 -05008840 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008841 struct inode *inode = page->mapping->host;
8842 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008843
8844 if (current->flags & PF_MEMALLOC) {
8845 redirty_page_for_writepage(wbc, page);
8846 unlock_page(page);
8847 return 0;
8848 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008849
8850 /*
8851 * If we are under memory pressure we will call this directly from the
8852 * VM, we need to make sure we have the inode referenced for the ordered
8853 * extent. If not just return like we didn't do anything.
8854 */
8855 if (!igrab(inode)) {
8856 redirty_page_for_writepage(wbc, page);
8857 return AOP_WRITEPAGE_ACTIVATE;
8858 }
Chris Masond1310b22008-01-24 16:13:08 -05008859 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008860 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8861 btrfs_add_delayed_iput(inode);
8862 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008863}
Chris Mason39279cc2007-06-12 06:35:45 -04008864
Eric Sandeen48a3b632013-04-25 20:41:01 +00008865static int btrfs_writepages(struct address_space *mapping,
8866 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008867{
Chris Masond1310b22008-01-24 16:13:08 -05008868 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008869
Chris Masond1310b22008-01-24 16:13:08 -05008870 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008871 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8872}
8873
Chris Mason3ab2fb52007-11-08 10:59:22 -05008874static int
8875btrfs_readpages(struct file *file, struct address_space *mapping,
8876 struct list_head *pages, unsigned nr_pages)
8877{
Chris Masond1310b22008-01-24 16:13:08 -05008878 struct extent_io_tree *tree;
8879 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008880 return extent_readpages(tree, mapping, pages, nr_pages,
8881 btrfs_get_extent);
8882}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008883static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008884{
Chris Masond1310b22008-01-24 16:13:08 -05008885 struct extent_io_tree *tree;
8886 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008887 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008888
Chris Masond1310b22008-01-24 16:13:08 -05008889 tree = &BTRFS_I(page->mapping->host)->io_tree;
8890 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008891 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008892 if (ret == 1) {
8893 ClearPagePrivate(page);
8894 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008895 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008896 }
8897 return ret;
8898}
Chris Mason39279cc2007-06-12 06:35:45 -04008899
Chris Masone6dcd2d2008-07-17 12:53:50 -04008900static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8901{
Chris Mason98509cf2008-09-11 15:51:43 -04008902 if (PageWriteback(page) || PageDirty(page))
8903 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008904 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905}
8906
Lukas Czernerd47992f2013-05-21 23:17:23 -04008907static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8908 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008909{
Josef Bacik5fd02042012-05-02 14:00:54 -04008910 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008911 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008913 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008914 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308916 u64 start;
8917 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008918 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008919
Chris Mason8b62b722009-09-02 16:53:46 -04008920 /*
8921 * we have the page locked, so new writeback can't start,
8922 * and the dirty bit won't be cleared while we are here.
8923 *
8924 * Wait for IO on this page so that we can safely clear
8925 * the PagePrivate2 bit and do ordered accounting
8926 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008927 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008928
Josef Bacik5fd02042012-05-02 14:00:54 -04008929 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930 if (offset) {
8931 btrfs_releasepage(page, GFP_NOFS);
8932 return;
8933 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008934
8935 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008936 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308937again:
8938 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008939 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308940 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008941 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308942 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008943 /*
8944 * IO on this page will never be started, so we need
8945 * to account for any ordered extents now
8946 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008947 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308948 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008949 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008950 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008951 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8952 EXTENT_DEFRAG, 1, 0, &cached_state,
8953 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008954 /*
8955 * whoever cleared the private bit is responsible
8956 * for the finish_ordered_io
8957 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008958 if (TestClearPagePrivate2(page)) {
8959 struct btrfs_ordered_inode_tree *tree;
8960 u64 new_len;
8961
8962 tree = &BTRFS_I(inode)->ordered_tree;
8963
8964 spin_lock_irq(&tree->lock);
8965 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308966 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008967 if (new_len < ordered->truncated_len)
8968 ordered->truncated_len = new_len;
8969 spin_unlock_irq(&tree->lock);
8970
8971 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308972 start,
8973 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008974 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008975 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008977 if (!inode_evicting) {
8978 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308979 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008980 &cached_state);
8981 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308982
8983 start = end + 1;
8984 if (start < page_end)
8985 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008986 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008987
Qu Wenruob9d0b382015-09-29 10:35:16 +08008988 /*
8989 * Qgroup reserved space handler
8990 * Page here will be either
8991 * 1) Already written to disk
8992 * In this case, its reserved space is released from data rsv map
8993 * and will be freed by delayed_ref handler finally.
8994 * So even we call qgroup_free_data(), it won't decrease reserved
8995 * space.
8996 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008997 * This means the reserved space should be freed here. However,
8998 * if a truncate invalidates the page (by clearing PageDirty)
8999 * and the page is accounted for while allocating extent
9000 * in btrfs_check_data_free_space() we let delayed_ref to
9001 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009002 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009003 if (PageDirty(page))
9004 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009005 if (!inode_evicting) {
9006 clear_extent_bit(tree, page_start, page_end,
9007 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009008 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9009 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009010 &cached_state, GFP_NOFS);
9011
9012 __btrfs_releasepage(page, GFP_NOFS);
9013 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014
Chris Mason4a096752008-07-21 10:29:44 -04009015 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009016 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009017 ClearPagePrivate(page);
9018 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009020 }
Chris Mason39279cc2007-06-12 06:35:45 -04009021}
9022
Chris Mason9ebefb182007-06-15 13:50:00 -04009023/*
9024 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9025 * called from a page fault handler when a page is first dirtied. Hence we must
9026 * be careful to check for EOF conditions here. We set the page up correctly
9027 * for a written page which means we get ENOSPC checking when writing into
9028 * holes and correct delalloc and unwritten extent mapping on filesystems that
9029 * support these features.
9030 *
9031 * We are not allowed to take the i_mutex here so we have to play games to
9032 * protect against truncate races as the page could now be beyond EOF. Because
9033 * vmtruncate() writes the inode size before removing pages, once we have the
9034 * page lock we can determine safely if the page is beyond EOF. If it is not
9035 * beyond EOF, then the page is guaranteed safe against truncation until we
9036 * unlock the page.
9037 */
Dave Jiang11bac802017-02-24 14:56:41 -08009038int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009039{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009040 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009041 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009043 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9044 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009045 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009046 char *kaddr;
9047 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009048 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009049 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009050 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309051 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009052 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009053 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309054 u64 end;
9055
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009056 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009057
Jan Karab2b5ef52012-06-12 16:20:45 +02009058 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009059 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309061 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009062
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309063 /*
9064 * Reserving delalloc space after obtaining the page lock can lead to
9065 * deadlock. For example, if a dirty page is locked by this function
9066 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9067 * dirty page write out, then the btrfs_writepage() function could
9068 * end up waiting indefinitely to get a lock on the page currently
9069 * being processed by btrfs_page_mkwrite() function.
9070 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009071 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309072 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009073 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009074 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009075 reserved = 1;
9076 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009077 if (ret) {
9078 if (ret == -ENOMEM)
9079 ret = VM_FAULT_OOM;
9080 else /* -ENOSPC, -EIO, etc */
9081 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009082 if (reserved)
9083 goto out;
9084 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009085 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009086
Nick Piggin56a76f82009-03-31 15:23:23 -07009087 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009088again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009089 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009090 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009091
Chris Mason9ebefb182007-06-15 13:50:00 -04009092 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009093 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009094 /* page got truncated out from underneath us */
9095 goto out_unlock;
9096 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009097 wait_on_page_writeback(page);
9098
David Sterbaff13db42015-12-03 14:30:40 +01009099 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009100 set_page_extent_mapped(page);
9101
Chris Masoneb84ae02008-07-17 13:53:27 -04009102 /*
9103 * we can't set the delalloc bits if there are pending ordered
9104 * extents. Drop our locks and wait for them to finish
9105 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009106 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9107 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009108 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009109 unlock_extent_cached(io_tree, page_start, page_end,
9110 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009111 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009112 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009113 btrfs_put_ordered_extent(ordered);
9114 goto again;
9115 }
9116
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009117 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009118 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009119 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009120 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309121 end = page_start + reserved_space - 1;
9122 spin_lock(&BTRFS_I(inode)->lock);
9123 BTRFS_I(inode)->outstanding_extents++;
9124 spin_unlock(&BTRFS_I(inode)->lock);
9125 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009126 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309127 }
9128 }
9129
Josef Bacikfbf19082009-10-01 17:10:23 -04009130 /*
Liu Bo54160342016-12-13 12:15:19 -08009131 * page_mkwrite gets called when the page is firstly dirtied after it's
9132 * faulted in, but write(2) could also dirty a page and set delalloc
9133 * bits, thus in this case for space account reason, we still need to
9134 * clear any delalloc bits within this page range since we have to
9135 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009136 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309137 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009138 EXTENT_DIRTY | EXTENT_DELALLOC |
9139 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009140 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009141
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309142 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009143 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009144 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009145 unlock_extent_cached(io_tree, page_start, page_end,
9146 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009147 ret = VM_FAULT_SIGBUS;
9148 goto out_unlock;
9149 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009150 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009151
9152 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009153 if (page_start + PAGE_SIZE > size)
9154 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009155 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009156 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009157
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009158 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009159 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009160 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009161 flush_dcache_page(page);
9162 kunmap(page);
9163 }
Chris Mason247e7432008-07-17 12:53:51 -04009164 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009165 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009166 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009167
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009168 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009169 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009170 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009171
Josef Bacik2ac55d42010-02-03 19:33:23 +00009172 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009173
9174out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009175 if (!ret) {
9176 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009177 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009178 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009179 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009180out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309181 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009182out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009183 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009184 return ret;
9185}
9186
Josef Bacika41ad392011-01-31 15:30:16 -05009187static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009188{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009189 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009190 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009191 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009192 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009193 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009194 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009195 u64 mask = fs_info->sectorsize - 1;
9196 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009197
Josef Bacik0ef8b722013-10-25 16:13:35 -04009198 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9199 (u64)-1);
9200 if (ret)
9201 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009202
Josef Bacikfcb80c22011-05-03 10:40:22 -04009203 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009204 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009205 * 3 things going on here
9206 *
9207 * 1) We need to reserve space for our orphan item and the space to
9208 * delete our orphan item. Lord knows we don't want to have a dangling
9209 * orphan item because we didn't reserve space to remove it.
9210 *
9211 * 2) We need to reserve space to update our inode.
9212 *
9213 * 3) We need to have something to cache all the space that is going to
9214 * be free'd up by the truncate operation, but also have some slack
9215 * space reserved in case it uses space during the truncate (thank you
9216 * very much snapshotting).
9217 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009218 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009220 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221 * doesn't end up using space reserved for updating the inode or
9222 * removing the orphan item. We also need to be able to stop the
9223 * transaction and start a new one, which means we need to be able to
9224 * update the inode several times, and we have no idea of knowing how
9225 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009226 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227 * Then there is the orphan item, which does indeed need to be held on
9228 * to for the whole operation, and we need nobody to touch this reserved
9229 * space except the orphan code.
9230 *
9231 * So that leaves us with
9232 *
9233 * 1) root->orphan_block_rsv - for the orphan deletion.
9234 * 2) rsv - for the truncate reservation, which we will steal from the
9235 * transaction reservation.
9236 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9237 * updating the inode.
9238 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009239 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009240 if (!rsv)
9241 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009242 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009243 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009244
Josef Bacik907cbce2011-08-08 13:46:15 -04009245 /*
Josef Bacik07127182011-08-19 10:29:59 -04009246 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009247 * 1 for updating the inode.
9248 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009249 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 if (IS_ERR(trans)) {
9251 err = PTR_ERR(trans);
9252 goto out;
9253 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009254
Josef Bacik907cbce2011-08-08 13:46:15 -04009255 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009256 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009257 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009258 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009259
Chris Mason5a3f23d2009-03-31 13:27:11 -04009260 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009261 * So if we truncate and then write and fsync we normally would just
9262 * write the extents that changed, which is a problem if we need to
9263 * first truncate that entire inode. So set this flag so we write out
9264 * all of the extents in the inode to the sync log so we're completely
9265 * safe.
9266 */
9267 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009268 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009269
Yan, Zheng80825102009-11-12 09:35:36 +00009270 while (1) {
9271 ret = btrfs_truncate_inode_items(trans, root, inode,
9272 inode->i_size,
9273 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009274 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009275 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009276 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009277 }
Chris Mason39279cc2007-06-12 06:35:45 -04009278
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009279 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009280 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009281 if (ret) {
9282 err = ret;
9283 break;
9284 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009285
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009286 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009287 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009288
9289 trans = btrfs_start_transaction(root, 2);
9290 if (IS_ERR(trans)) {
9291 ret = err = PTR_ERR(trans);
9292 trans = NULL;
9293 break;
9294 }
9295
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009296 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009297 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009298 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009299 BUG_ON(ret); /* shouldn't happen */
9300 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009301 }
9302
9303 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009304 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009305 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009306 if (ret)
9307 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009308 }
9309
Chris Mason917c16b2011-11-08 14:49:59 -05009310 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009311 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009312 ret = btrfs_update_inode(trans, root, inode);
9313 if (ret && !err)
9314 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009315
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009316 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009317 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009318 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009319out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009320 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009321
Josef Bacik3893e332011-01-31 16:03:11 -05009322 if (ret && !err)
9323 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009324
Josef Bacik3893e332011-01-31 16:03:11 -05009325 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009326}
9327
Sven Wegener3b963622008-06-09 21:57:42 -04009328/*
Chris Masond352ac62008-09-29 15:18:18 -04009329 * create a new subvolume directory/inode (helper for the ioctl).
9330 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009331int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009332 struct btrfs_root *new_root,
9333 struct btrfs_root *parent_root,
9334 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009335{
Chris Mason39279cc2007-06-12 06:35:45 -04009336 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009337 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009338 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009339
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009340 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9341 new_dirid, new_dirid,
9342 S_IFDIR | (~current_umask() & S_IRWXUGO),
9343 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009344 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009345 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009346 inode->i_op = &btrfs_dir_inode_operations;
9347 inode->i_fop = &btrfs_dir_file_operations;
9348
Miklos Szeredibfe86842011-10-28 14:13:29 +02009349 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009350 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009351 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009352
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009353 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9354 if (err)
9355 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009356 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009357 new_root->root_key.objectid, err);
9358
Yan, Zheng76dda932009-09-21 16:00:26 -04009359 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009360
Yan, Zheng76dda932009-09-21 16:00:26 -04009361 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009362 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009363}
9364
Chris Mason39279cc2007-06-12 06:35:45 -04009365struct inode *btrfs_alloc_inode(struct super_block *sb)
9366{
9367 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009368 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009369
9370 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9371 if (!ei)
9372 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009373
9374 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009375 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009376 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009377 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009378 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009379 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009380 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009381 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009382 ei->disk_i_size = 0;
9383 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009384 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009385 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009386 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009387 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009388 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009389 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009390
Josef Bacik9e0baf62011-07-15 15:16:44 +00009391 spin_lock_init(&ei->lock);
9392 ei->outstanding_extents = 0;
9393 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009394
Josef Bacik72ac3c02012-05-23 14:13:11 -04009395 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009396 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009397
Miao Xie16cdcec2011-04-22 18:12:22 +08009398 ei->delayed_node = NULL;
9399
chandan r9cc97d62012-07-04 12:48:07 +05309400 ei->i_otime.tv_sec = 0;
9401 ei->i_otime.tv_nsec = 0;
9402
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009403 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009404 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009405 extent_io_tree_init(&ei->io_tree, inode);
9406 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009407 ei->io_tree.track_uptodate = 1;
9408 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009409 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009410 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009411 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009412 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009414 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009415 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009416 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009417
9418 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009419}
9420
Josef Bacikaaedb552013-10-11 14:44:09 -04009421#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9422void btrfs_test_destroy_inode(struct inode *inode)
9423{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009424 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009425 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9426}
9427#endif
9428
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009429static void btrfs_i_callback(struct rcu_head *head)
9430{
9431 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009432 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9433}
9434
Chris Mason39279cc2007-06-12 06:35:45 -04009435void btrfs_destroy_inode(struct inode *inode)
9436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009437 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009438 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009439 struct btrfs_root *root = BTRFS_I(inode)->root;
9440
Al Virob3d9b7a2012-06-09 13:51:19 -04009441 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009442 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009443 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9444 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009445 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009446 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009447 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009448 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009449
Chris Mason5a3f23d2009-03-31 13:27:11 -04009450 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009451 * This can happen where we create an inode, but somebody else also
9452 * created the same inode and we need to destroy the one we already
9453 * created.
9454 */
9455 if (!root)
9456 goto free;
9457
Josef Bacik8a35d952012-05-23 14:26:42 -04009458 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9459 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009460 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009461 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009462 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009463 }
Josef Bacik7b128762008-07-24 12:17:14 -04009464
Chris Masond3977122009-01-05 21:25:51 -05009465 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009466 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9467 if (!ordered)
9468 break;
9469 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009471 "found ordered extent %llu %llu on inode cleanup",
9472 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009473 btrfs_remove_ordered_extent(inode, ordered);
9474 btrfs_put_ordered_extent(ordered);
9475 btrfs_put_ordered_extent(ordered);
9476 }
9477 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009478 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009479 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009480 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009481free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009482 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009483}
9484
Al Viro45321ac2010-06-07 13:43:19 -04009485int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009486{
9487 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009488
Naohiro Aota6379ef92013-06-06 09:56:34 +00009489 if (root == NULL)
9490 return 1;
9491
Liu Bofa6ac872013-02-20 14:10:23 +00009492 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009493 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009494 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009495 else
Al Viro45321ac2010-06-07 13:43:19 -04009496 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009497}
9498
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009499static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009500{
9501 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9502
9503 inode_init_once(&ei->vfs_inode);
9504}
9505
9506void btrfs_destroy_cachep(void)
9507{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009508 /*
9509 * Make sure all delayed rcu free inodes are flushed before we
9510 * destroy cache.
9511 */
9512 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009513 kmem_cache_destroy(btrfs_inode_cachep);
9514 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009515 kmem_cache_destroy(btrfs_path_cachep);
9516 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009517}
9518
9519int btrfs_init_cachep(void)
9520{
David Sterba837e1972012-09-07 03:00:48 -06009521 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009522 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009523 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9524 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009525 if (!btrfs_inode_cachep)
9526 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009527
David Sterba837e1972012-09-07 03:00:48 -06009528 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009529 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009530 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009531 if (!btrfs_trans_handle_cachep)
9532 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009533
David Sterba837e1972012-09-07 03:00:48 -06009534 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009535 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009536 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009537 if (!btrfs_path_cachep)
9538 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009539
David Sterba837e1972012-09-07 03:00:48 -06009540 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009541 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009542 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009543 if (!btrfs_free_space_cachep)
9544 goto fail;
9545
Chris Mason39279cc2007-06-12 06:35:45 -04009546 return 0;
9547fail:
9548 btrfs_destroy_cachep();
9549 return -ENOMEM;
9550}
9551
David Howellsa528d352017-01-31 16:46:22 +00009552static int btrfs_getattr(const struct path *path, struct kstat *stat,
9553 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009554{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009555 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009556 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009557 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009558 u32 bi_flags = BTRFS_I(inode)->flags;
9559
9560 stat->result_mask |= STATX_BTIME;
9561 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9562 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9563 if (bi_flags & BTRFS_INODE_APPEND)
9564 stat->attributes |= STATX_ATTR_APPEND;
9565 if (bi_flags & BTRFS_INODE_COMPRESS)
9566 stat->attributes |= STATX_ATTR_COMPRESSED;
9567 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9568 stat->attributes |= STATX_ATTR_IMMUTABLE;
9569 if (bi_flags & BTRFS_INODE_NODUMP)
9570 stat->attributes |= STATX_ATTR_NODUMP;
9571
9572 stat->attributes_mask |= (STATX_ATTR_APPEND |
9573 STATX_ATTR_COMPRESSED |
9574 STATX_ATTR_IMMUTABLE |
9575 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009576
Chris Mason39279cc2007-06-12 06:35:45 -04009577 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009578 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009579
9580 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009581 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009582 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009583 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009584 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009585 return 0;
9586}
9587
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588static int btrfs_rename_exchange(struct inode *old_dir,
9589 struct dentry *old_dentry,
9590 struct inode *new_dir,
9591 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009592{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009593 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009594 struct btrfs_trans_handle *trans;
9595 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009596 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 struct inode *new_inode = new_dentry->d_inode;
9598 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009599 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009601 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9602 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 u64 old_idx = 0;
9604 u64 new_idx = 0;
9605 u64 root_objectid;
9606 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009607 bool root_log_pinned = false;
9608 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609
9610 /* we only allow rename subvolume link between subvolumes */
9611 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9612 return -EXDEV;
9613
9614 /* close the race window with snapshot create/destroy ioctl */
9615 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009616 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009618 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619
9620 /*
9621 * We want to reserve the absolute worst case amount of items. So if
9622 * both inodes are subvols and we need to unlink them then that would
9623 * require 4 item modifications, but if they are both normal inodes it
9624 * would require 5 item modifications, so we'll assume their normal
9625 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9626 * should cover the worst case number of items we'll modify.
9627 */
9628 trans = btrfs_start_transaction(root, 12);
9629 if (IS_ERR(trans)) {
9630 ret = PTR_ERR(trans);
9631 goto out_notrans;
9632 }
9633
9634 /*
9635 * We need to find a free sequence number both in the source and
9636 * in the destination directory for the exchange.
9637 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009638 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009639 if (ret)
9640 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009641 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009642 if (ret)
9643 goto out_fail;
9644
9645 BTRFS_I(old_inode)->dir_index = 0ULL;
9646 BTRFS_I(new_inode)->dir_index = 0ULL;
9647
9648 /* Reference for the source. */
9649 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9650 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009651 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009653 btrfs_pin_log_trans(root);
9654 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 ret = btrfs_insert_inode_ref(trans, dest,
9656 new_dentry->d_name.name,
9657 new_dentry->d_name.len,
9658 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009659 btrfs_ino(BTRFS_I(new_dir)),
9660 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 if (ret)
9662 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 }
9664
9665 /* And now for the dest. */
9666 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9667 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009668 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009670 btrfs_pin_log_trans(dest);
9671 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 ret = btrfs_insert_inode_ref(trans, root,
9673 old_dentry->d_name.name,
9674 old_dentry->d_name.len,
9675 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009676 btrfs_ino(BTRFS_I(old_dir)),
9677 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 if (ret)
9679 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 }
9681
9682 /* Update inode version and ctime/mtime. */
9683 inode_inc_iversion(old_dir);
9684 inode_inc_iversion(new_dir);
9685 inode_inc_iversion(old_inode);
9686 inode_inc_iversion(new_inode);
9687 old_dir->i_ctime = old_dir->i_mtime = ctime;
9688 new_dir->i_ctime = new_dir->i_mtime = ctime;
9689 old_inode->i_ctime = ctime;
9690 new_inode->i_ctime = ctime;
9691
9692 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009693 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9694 BTRFS_I(old_inode), 1);
9695 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9696 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 }
9698
9699 /* src is a subvolume */
9700 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9701 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9702 ret = btrfs_unlink_subvol(trans, root, old_dir,
9703 root_objectid,
9704 old_dentry->d_name.name,
9705 old_dentry->d_name.len);
9706 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009707 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9708 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 old_dentry->d_name.name,
9710 old_dentry->d_name.len);
9711 if (!ret)
9712 ret = btrfs_update_inode(trans, root, old_inode);
9713 }
9714 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009715 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 goto out_fail;
9717 }
9718
9719 /* dest is a subvolume */
9720 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9721 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9722 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9723 root_objectid,
9724 new_dentry->d_name.name,
9725 new_dentry->d_name.len);
9726 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009727 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9728 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009729 new_dentry->d_name.name,
9730 new_dentry->d_name.len);
9731 if (!ret)
9732 ret = btrfs_update_inode(trans, dest, new_inode);
9733 }
9734 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009735 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 goto out_fail;
9737 }
9738
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009739 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009740 new_dentry->d_name.name,
9741 new_dentry->d_name.len, 0, old_idx);
9742 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009743 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 goto out_fail;
9745 }
9746
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009747 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 old_dentry->d_name.name,
9749 old_dentry->d_name.len, 0, new_idx);
9750 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009751 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752 goto out_fail;
9753 }
9754
9755 if (old_inode->i_nlink == 1)
9756 BTRFS_I(old_inode)->dir_index = old_idx;
9757 if (new_inode->i_nlink == 1)
9758 BTRFS_I(new_inode)->dir_index = new_idx;
9759
Filipe Manana86e8aa02016-05-05 02:02:27 +01009760 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009762 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9763 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009764 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009765 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009766 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009767 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009769 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9770 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009771 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009772 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009773 }
9774out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009775 /*
9776 * If we have pinned a log and an error happened, we unpin tasks
9777 * trying to sync the log and force them to fallback to a transaction
9778 * commit if the log currently contains any of the inodes involved in
9779 * this rename operation (to ensure we do not persist a log with an
9780 * inconsistent state for any of these inodes or leading to any
9781 * inconsistencies when replayed). If the transaction was aborted, the
9782 * abortion reason is propagated to userspace when attempting to commit
9783 * the transaction. If the log does not contain any of these inodes, we
9784 * allow the tasks to sync it.
9785 */
9786 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009787 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9788 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9789 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009790 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009791 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009792 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009793
9794 if (root_log_pinned) {
9795 btrfs_end_log_trans(root);
9796 root_log_pinned = false;
9797 }
9798 if (dest_log_pinned) {
9799 btrfs_end_log_trans(dest);
9800 dest_log_pinned = false;
9801 }
9802 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009803 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804out_notrans:
9805 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009806 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009808 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809
9810 return ret;
9811}
9812
9813static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9814 struct btrfs_root *root,
9815 struct inode *dir,
9816 struct dentry *dentry)
9817{
9818 int ret;
9819 struct inode *inode;
9820 u64 objectid;
9821 u64 index;
9822
9823 ret = btrfs_find_free_ino(root, &objectid);
9824 if (ret)
9825 return ret;
9826
9827 inode = btrfs_new_inode(trans, root, dir,
9828 dentry->d_name.name,
9829 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009830 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009831 objectid,
9832 S_IFCHR | WHITEOUT_MODE,
9833 &index);
9834
9835 if (IS_ERR(inode)) {
9836 ret = PTR_ERR(inode);
9837 return ret;
9838 }
9839
9840 inode->i_op = &btrfs_special_inode_operations;
9841 init_special_inode(inode, inode->i_mode,
9842 WHITEOUT_DEV);
9843
9844 ret = btrfs_init_inode_security(trans, inode, dir,
9845 &dentry->d_name);
9846 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009847 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009849 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9850 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009852 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009853
9854 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009855out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009856 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009857 if (ret)
9858 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009859 iput(inode);
9860
Filipe Mananac9901612016-05-05 01:41:57 +01009861 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009862}
9863
Chris Mason39279cc2007-06-12 06:35:45 -04009864static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865 struct inode *new_dir, struct dentry *new_dentry,
9866 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009868 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009869 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009870 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009871 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9872 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009873 struct inode *new_inode = d_inode(new_dentry);
9874 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009875 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009876 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009877 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009878 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009879 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009880
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009881 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009882 return -EPERM;
9883
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009884 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009885 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009886 return -EXDEV;
9887
Li Zefan33345d012011-04-20 10:31:50 +08009888 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009889 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 return -ENOTEMPTY;
9891
Chris Mason39279cc2007-06-12 06:35:45 -04009892 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009893 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009894 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009895
9896
9897 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009898 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009899 new_dentry->d_name.name,
9900 new_dentry->d_name.len);
9901
9902 if (ret) {
9903 if (ret == -EEXIST) {
9904 /* we shouldn't get
9905 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309906 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009907 return ret;
9908 }
9909 } else {
9910 /* maybe -EOVERFLOW */
9911 return ret;
9912 }
9913 }
9914 ret = 0;
9915
Chris Mason5a3f23d2009-03-31 13:27:11 -04009916 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009917 * we're using rename to replace one file with another. Start IO on it
9918 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009919 */
Chris Mason8d875f92014-08-12 10:47:42 -07009920 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009921 filemap_flush(old_inode->i_mapping);
9922
Yan, Zheng76dda932009-09-21 16:00:26 -04009923 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009925 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009926 /*
9927 * We want to reserve the absolute worst case amount of items. So if
9928 * both inodes are subvols and we need to unlink them then that would
9929 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009930 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009931 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9932 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009933 * If our rename has the whiteout flag, we need more 5 units for the
9934 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9935 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009936 */
Filipe Manana5062af32016-05-05 10:26:26 +01009937 trans_num_items = 11;
9938 if (flags & RENAME_WHITEOUT)
9939 trans_num_items += 5;
9940 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009941 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009942 ret = PTR_ERR(trans);
9943 goto out_notrans;
9944 }
Chris Mason5f39d392007-10-15 16:14:19 -04009945
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 if (dest != root)
9947 btrfs_record_root_in_trans(trans, dest);
9948
Nikolay Borisov877574e2017-02-20 13:50:33 +02009949 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009950 if (ret)
9951 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009952
Miao Xie67de1172013-12-26 13:07:06 +08009953 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009954 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009955 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009956 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009957 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009958 btrfs_pin_log_trans(root);
9959 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009960 ret = btrfs_insert_inode_ref(trans, dest,
9961 new_dentry->d_name.name,
9962 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009963 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009964 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009965 if (ret)
9966 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009967 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009968
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009969 inode_inc_iversion(old_dir);
9970 inode_inc_iversion(new_dir);
9971 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009972 old_dir->i_ctime = old_dir->i_mtime =
9973 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009974 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009975
Chris Mason12fcfd22009-03-24 10:24:20 -04009976 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009977 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9978 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009979
Li Zefan33345d012011-04-20 10:31:50 +08009980 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009981 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9982 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9983 old_dentry->d_name.name,
9984 old_dentry->d_name.len);
9985 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009986 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9987 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009988 old_dentry->d_name.name,
9989 old_dentry->d_name.len);
9990 if (!ret)
9991 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009993 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009994 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009995 goto out_fail;
9996 }
Chris Mason39279cc2007-06-12 06:35:45 -04009997
9998 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009999 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010000 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010001 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010002 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10003 root_objectid = BTRFS_I(new_inode)->location.objectid;
10004 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10005 root_objectid,
10006 new_dentry->d_name.name,
10007 new_dentry->d_name.len);
10008 BUG_ON(new_inode->i_nlink == 0);
10009 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010010 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10011 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010012 new_dentry->d_name.name,
10013 new_dentry->d_name.len);
10014 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010015 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010016 ret = btrfs_orphan_add(trans,
10017 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010018 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010019 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010020 goto out_fail;
10021 }
Chris Mason39279cc2007-06-12 06:35:45 -040010022 }
Josef Bacikaec74772008-07-24 12:12:38 -040010023
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010024 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010025 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010026 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010027 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010028 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010029 goto out_fail;
10030 }
Chris Mason39279cc2007-06-12 06:35:45 -040010031
Miao Xie67de1172013-12-26 13:07:06 +080010032 if (old_inode->i_nlink == 1)
10033 BTRFS_I(old_inode)->dir_index = index;
10034
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010035 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010036 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010037
David Sterbaf85b7372017-01-20 14:54:07 +010010038 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10039 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010040 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010041 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010042 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010043
10044 if (flags & RENAME_WHITEOUT) {
10045 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10046 old_dentry);
10047
10048 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010049 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010050 goto out_fail;
10051 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010052 }
Chris Mason39279cc2007-06-12 06:35:45 -040010053out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010054 /*
10055 * If we have pinned the log and an error happened, we unpin tasks
10056 * trying to sync the log and force them to fallback to a transaction
10057 * commit if the log currently contains any of the inodes involved in
10058 * this rename operation (to ensure we do not persist a log with an
10059 * inconsistent state for any of these inodes or leading to any
10060 * inconsistencies when replayed). If the transaction was aborted, the
10061 * abortion reason is propagated to userspace when attempting to commit
10062 * the transaction. If the log does not contain any of these inodes, we
10063 * allow the tasks to sync it.
10064 */
10065 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010066 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10067 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10068 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010069 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010070 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010071 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010072
10073 btrfs_end_log_trans(root);
10074 log_pinned = false;
10075 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010076 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010077out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010078 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010079 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010080
Chris Mason39279cc2007-06-12 06:35:45 -040010081 return ret;
10082}
10083
Miklos Szeredi80ace852014-07-23 15:15:32 +020010084static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10085 struct inode *new_dir, struct dentry *new_dentry,
10086 unsigned int flags)
10087{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010088 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010089 return -EINVAL;
10090
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010091 if (flags & RENAME_EXCHANGE)
10092 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10093 new_dentry);
10094
10095 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010096}
10097
Miao Xie8ccf6f192012-10-25 09:28:04 +000010098static void btrfs_run_delalloc_work(struct btrfs_work *work)
10099{
10100 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010101 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010102
10103 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10104 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010105 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010106 filemap_flush(inode->i_mapping);
10107 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10108 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010109 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110
10111 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010112 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010113 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010114 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010115 complete(&delalloc_work->completion);
10116}
10117
10118struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010119 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010120{
10121 struct btrfs_delalloc_work *work;
10122
David Sterba100d5702015-12-08 14:39:32 +010010123 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010124 if (!work)
10125 return NULL;
10126
10127 init_completion(&work->completion);
10128 INIT_LIST_HEAD(&work->list);
10129 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010130 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010131 WARN_ON_ONCE(!inode);
10132 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10133 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010134
10135 return work;
10136}
10137
10138void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10139{
10140 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010141 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010142}
10143
Chris Masond352ac62008-09-29 15:18:18 -040010144/*
10145 * some fairly slow code that needs optimization. This walks the list
10146 * of all the inodes with pending delalloc and forces them to disk.
10147 */
Miao Xie6c255e62014-03-06 13:55:01 +080010148static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10149 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010150{
Chris Masonea8c2812008-08-04 23:17:27 -040010151 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010152 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010153 struct btrfs_delalloc_work *work, *next;
10154 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010155 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010157
Miao Xie8ccf6f192012-10-25 09:28:04 +000010158 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010159 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010160
Miao Xie573bfb72014-03-06 13:55:03 +080010161 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010162 spin_lock(&root->delalloc_lock);
10163 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010164 while (!list_empty(&splice)) {
10165 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010166 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010167
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 list_move_tail(&binode->delalloc_inodes,
10169 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010170 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010171 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010173 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010174 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010176
David Sterba651d4942015-11-27 19:24:16 +010010177 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010178 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010179 if (delay_iput)
10180 btrfs_add_delayed_iput(inode);
10181 else
10182 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010183 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010184 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010185 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010186 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010187 btrfs_queue_work(root->fs_info->flush_workers,
10188 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010189 ret++;
10190 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010191 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010192 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010194 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010195 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010196
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010197out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010198 list_for_each_entry_safe(work, next, &works, list) {
10199 list_del_init(&work->list);
10200 btrfs_wait_and_free_delalloc_work(work);
10201 }
10202
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 if (!list_empty_careful(&splice)) {
10204 spin_lock(&root->delalloc_lock);
10205 list_splice_tail(&splice, &root->delalloc_inodes);
10206 spin_unlock(&root->delalloc_lock);
10207 }
Miao Xie573bfb72014-03-06 13:55:03 +080010208 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 return ret;
10210}
10211
10212int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010214 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010215 int ret;
10216
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010217 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 return -EROFS;
10219
Miao Xie6c255e62014-03-06 13:55:01 +080010220 ret = __start_delalloc_inodes(root, delay_iput, -1);
10221 if (ret > 0)
10222 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010223 /*
10224 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010225 * we have to make sure the IO is actually started and that
10226 * ordered extents get created before we return
10227 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010228 atomic_inc(&fs_info->async_submit_draining);
10229 while (atomic_read(&fs_info->nr_async_submits) ||
10230 atomic_read(&fs_info->async_delalloc_pages)) {
10231 wait_event(fs_info->async_submit_wait,
10232 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10233 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010234 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010235 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236 return ret;
10237}
10238
Miao Xie6c255e62014-03-06 13:55:01 +080010239int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10240 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241{
10242 struct btrfs_root *root;
10243 struct list_head splice;
10244 int ret;
10245
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010246 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 return -EROFS;
10248
10249 INIT_LIST_HEAD(&splice);
10250
Miao Xie573bfb72014-03-06 13:55:03 +080010251 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010252 spin_lock(&fs_info->delalloc_root_lock);
10253 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010254 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 root = list_first_entry(&splice, struct btrfs_root,
10256 delalloc_root);
10257 root = btrfs_grab_fs_root(root);
10258 BUG_ON(!root);
10259 list_move_tail(&root->delalloc_root,
10260 &fs_info->delalloc_roots);
10261 spin_unlock(&fs_info->delalloc_root_lock);
10262
Miao Xie6c255e62014-03-06 13:55:01 +080010263 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010265 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010266 goto out;
10267
Miao Xie6c255e62014-03-06 13:55:01 +080010268 if (nr != -1) {
10269 nr -= ret;
10270 WARN_ON(nr < 0);
10271 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010272 spin_lock(&fs_info->delalloc_root_lock);
10273 }
10274 spin_unlock(&fs_info->delalloc_root_lock);
10275
Miao Xie6c255e62014-03-06 13:55:01 +080010276 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010277 atomic_inc(&fs_info->async_submit_draining);
10278 while (atomic_read(&fs_info->nr_async_submits) ||
10279 atomic_read(&fs_info->async_delalloc_pages)) {
10280 wait_event(fs_info->async_submit_wait,
10281 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10282 atomic_read(&fs_info->async_delalloc_pages) == 0));
10283 }
10284 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010285out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010286 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010287 spin_lock(&fs_info->delalloc_root_lock);
10288 list_splice_tail(&splice, &fs_info->delalloc_roots);
10289 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010290 }
Miao Xie573bfb72014-03-06 13:55:03 +080010291 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010292 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010293}
10294
Chris Mason39279cc2007-06-12 06:35:45 -040010295static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10296 const char *symname)
10297{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010298 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010299 struct btrfs_trans_handle *trans;
10300 struct btrfs_root *root = BTRFS_I(dir)->root;
10301 struct btrfs_path *path;
10302 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010303 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010304 int err;
10305 int drop_inode = 0;
10306 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010307 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010308 int name_len;
10309 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010310 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010311 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010312 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010313
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010314 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010316 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010317
Josef Bacik9ed74f22009-09-11 16:12:44 -040010318 /*
10319 * 2 items for inode item and ref
10320 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010321 * 1 item for updating parent inode item
10322 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010323 * 1 item for xattr if selinux is on
10324 */
Filipe Manana9269d122015-12-31 18:16:29 +000010325 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010326 if (IS_ERR(trans))
10327 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010328
Li Zefan581bb052011-04-20 10:06:11 +080010329 err = btrfs_find_free_ino(root, &objectid);
10330 if (err)
10331 goto out_unlock;
10332
Josef Bacikaec74772008-07-24 12:12:38 -040010333 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010334 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10335 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010336 if (IS_ERR(inode)) {
10337 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010338 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010339 }
Chris Mason39279cc2007-06-12 06:35:45 -040010340
Casey Schauflerad19db72011-12-15 10:09:07 -050010341 /*
10342 * If the active LSM wants to access the inode during
10343 * d_instantiate it needs these. Smack checks to see
10344 * if the filesystem supports xattrs by looking at the
10345 * ops vector.
10346 */
10347 inode->i_fop = &btrfs_file_operations;
10348 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010349 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010350 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10351
10352 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10353 if (err)
10354 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010355
Chris Mason39279cc2007-06-12 06:35:45 -040010356 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010357 if (!path) {
10358 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010359 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010360 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010361 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010362 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010363 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010364 datasize = btrfs_file_extent_calc_inline_size(name_len);
10365 err = btrfs_insert_empty_item(trans, root, path, &key,
10366 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010367 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010368 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010369 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010370 }
Chris Mason5f39d392007-10-15 16:14:19 -040010371 leaf = path->nodes[0];
10372 ei = btrfs_item_ptr(leaf, path->slots[0],
10373 struct btrfs_file_extent_item);
10374 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10375 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010376 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010377 btrfs_set_file_extent_encryption(leaf, ei, 0);
10378 btrfs_set_file_extent_compression(leaf, ei, 0);
10379 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10380 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10381
Chris Mason39279cc2007-06-12 06:35:45 -040010382 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010383 write_extent_buffer(leaf, symname, ptr, name_len);
10384 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010385 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010386
Chris Mason39279cc2007-06-12 06:35:45 -040010387 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010388 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010389 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010390 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010391 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010392 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010393 /*
10394 * Last step, add directory indexes for our symlink inode. This is the
10395 * last step to avoid extra cleanup of these indexes if an error happens
10396 * elsewhere above.
10397 */
10398 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010399 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10400 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010401 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010402 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010403 goto out_unlock_inode;
10404 }
10405
10406 unlock_new_inode(inode);
10407 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010408
10409out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010410 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010411 if (drop_inode) {
10412 inode_dec_link_count(inode);
10413 iput(inode);
10414 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010415 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010416 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010417
10418out_unlock_inode:
10419 drop_inode = 1;
10420 unlock_new_inode(inode);
10421 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010422}
Chris Mason16432982008-04-10 10:23:21 -040010423
Josef Bacik0af3d002010-06-21 14:48:16 -040010424static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10425 u64 start, u64 num_bytes, u64 min_size,
10426 loff_t actual_len, u64 *alloc_hint,
10427 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010430 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10431 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010432 struct btrfs_root *root = BTRFS_I(inode)->root;
10433 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010434 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010435 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010436 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010437 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010438 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010439 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010440 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010441
Josef Bacik0af3d002010-06-21 14:48:16 -040010442 if (trans)
10443 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010444 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010445 if (own_trans) {
10446 trans = btrfs_start_transaction(root, 3);
10447 if (IS_ERR(trans)) {
10448 ret = PTR_ERR(trans);
10449 break;
10450 }
Yan Zhengd899e052008-10-30 14:25:28 -040010451 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010452
Byongho Leeee221842015-12-15 01:42:10 +090010453 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010454 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010455 /*
10456 * If we are severely fragmented we could end up with really
10457 * small allocations, so if the allocator is returning small
10458 * chunks lets make its job easier by only searching for those
10459 * sized chunks.
10460 */
10461 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010462 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10463 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010464 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010465 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010466 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010467 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010468 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010469 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010470
Josef Bacik0b670dc2015-09-23 17:11:16 -040010471 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010472 ret = insert_reserved_file_extent(trans, inode,
10473 cur_offset, ins.objectid,
10474 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010475 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010476 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010477 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010478 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010479 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010480 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010481 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010482 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010483 break;
10484 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010485
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010486 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010487 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010488
Josef Bacik5dc562c2012-08-17 13:14:17 -040010489 em = alloc_extent_map();
10490 if (!em) {
10491 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10492 &BTRFS_I(inode)->runtime_flags);
10493 goto next;
10494 }
10495
10496 em->start = cur_offset;
10497 em->orig_start = cur_offset;
10498 em->len = ins.offset;
10499 em->block_start = ins.objectid;
10500 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010501 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010502 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010503 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010504 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10505 em->generation = trans->transid;
10506
10507 while (1) {
10508 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010509 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010510 write_unlock(&em_tree->lock);
10511 if (ret != -EEXIST)
10512 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010513 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010514 cur_offset + ins.offset - 1,
10515 0);
10516 }
10517 free_extent_map(em);
10518next:
Yan Zhengd899e052008-10-30 14:25:28 -040010519 num_bytes -= ins.offset;
10520 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010521 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010522
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010523 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010524 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010525 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010526 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010527 (actual_len > inode->i_size) &&
10528 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010529 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010530 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010531 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010532 i_size = cur_offset;
10533 i_size_write(inode, i_size);
10534 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010535 }
10536
Yan Zhengd899e052008-10-30 14:25:28 -040010537 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010538
10539 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010540 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010541 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010542 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010543 break;
10544 }
Yan Zhengd899e052008-10-30 14:25:28 -040010545
Josef Bacik0af3d002010-06-21 14:48:16 -040010546 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010547 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010548 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010549 if (cur_offset < end)
10550 btrfs_free_reserved_data_space(inode, cur_offset,
10551 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010552 return ret;
10553}
10554
Josef Bacik0af3d002010-06-21 14:48:16 -040010555int btrfs_prealloc_file_range(struct inode *inode, int mode,
10556 u64 start, u64 num_bytes, u64 min_size,
10557 loff_t actual_len, u64 *alloc_hint)
10558{
10559 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10560 min_size, actual_len, alloc_hint,
10561 NULL);
10562}
10563
10564int btrfs_prealloc_file_range_trans(struct inode *inode,
10565 struct btrfs_trans_handle *trans, int mode,
10566 u64 start, u64 num_bytes, u64 min_size,
10567 loff_t actual_len, u64 *alloc_hint)
10568{
10569 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10570 min_size, actual_len, alloc_hint, trans);
10571}
10572
Chris Masone6dcd2d2008-07-17 12:53:50 -040010573static int btrfs_set_page_dirty(struct page *page)
10574{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010575 return __set_page_dirty_nobuffers(page);
10576}
10577
Al Viro10556cb22011-06-20 19:28:19 -040010578static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010579{
Li Zefanb83cc962010-12-20 16:04:08 +080010580 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010581 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010582
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010583 if (mask & MAY_WRITE &&
10584 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10585 if (btrfs_root_readonly(root))
10586 return -EROFS;
10587 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10588 return -EACCES;
10589 }
Al Viro2830ba72011-06-20 19:16:29 -040010590 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010591}
Chris Mason39279cc2007-06-12 06:35:45 -040010592
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010593static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10594{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010595 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010596 struct btrfs_trans_handle *trans;
10597 struct btrfs_root *root = BTRFS_I(dir)->root;
10598 struct inode *inode = NULL;
10599 u64 objectid;
10600 u64 index;
10601 int ret = 0;
10602
10603 /*
10604 * 5 units required for adding orphan entry
10605 */
10606 trans = btrfs_start_transaction(root, 5);
10607 if (IS_ERR(trans))
10608 return PTR_ERR(trans);
10609
10610 ret = btrfs_find_free_ino(root, &objectid);
10611 if (ret)
10612 goto out;
10613
10614 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010615 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010616 if (IS_ERR(inode)) {
10617 ret = PTR_ERR(inode);
10618 inode = NULL;
10619 goto out;
10620 }
10621
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010622 inode->i_fop = &btrfs_file_operations;
10623 inode->i_op = &btrfs_file_inode_operations;
10624
10625 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010626 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10627
Chris Masonb0d5d102014-09-08 13:08:51 -070010628 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10629 if (ret)
10630 goto out_inode;
10631
10632 ret = btrfs_update_inode(trans, root, inode);
10633 if (ret)
10634 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010635 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010636 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010637 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010638
Filipe Manana5762b5c2014-08-01 00:10:32 +010010639 /*
10640 * We set number of links to 0 in btrfs_new_inode(), and here we set
10641 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10642 * through:
10643 *
10644 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10645 */
10646 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010647 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010648 d_tmpfile(dentry, inode);
10649 mark_inode_dirty(inode);
10650
10651out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010652 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010653 if (ret)
10654 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010655 btrfs_balance_delayed_items(fs_info);
10656 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010657 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010658
10659out_inode:
10660 unlock_new_inode(inode);
10661 goto out;
10662
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010663}
10664
David Sterba20a7db82017-02-17 16:24:29 +010010665__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010666static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010667{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010668 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010669}
10670
Josef Bacikc6100a42017-05-05 11:57:13 -040010671static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10672{
10673 struct inode *inode = private_data;
10674 return btrfs_sb(inode->i_sb);
10675}
10676
10677static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10678 u64 start, u64 end)
10679{
10680 struct inode *inode = private_data;
10681 u64 isize;
10682
10683 isize = i_size_read(inode);
10684 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10685 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10686 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10687 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10688 }
10689}
10690
10691void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10692{
10693 struct inode *inode = private_data;
10694 unsigned long index = start >> PAGE_SHIFT;
10695 unsigned long end_index = end >> PAGE_SHIFT;
10696 struct page *page;
10697
10698 while (index <= end_index) {
10699 page = find_get_page(inode->i_mapping, index);
10700 ASSERT(page); /* Pages should be in the extent_io_tree */
10701 set_page_writeback(page);
10702 put_page(page);
10703 index++;
10704 }
10705}
10706
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010707static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010708 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010709 .lookup = btrfs_lookup,
10710 .create = btrfs_create,
10711 .unlink = btrfs_unlink,
10712 .link = btrfs_link,
10713 .mkdir = btrfs_mkdir,
10714 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010715 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010716 .symlink = btrfs_symlink,
10717 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010718 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010719 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010720 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010721 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010722 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010723 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010724 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010725};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010726static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010727 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010728 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010729 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010730};
Yan, Zheng76dda932009-09-21 16:00:26 -040010731
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010732static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010733 .llseek = generic_file_llseek,
10734 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010735 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010736 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010737#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010738 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010739#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010740 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010741 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010742};
10743
David Sterba20e55062015-11-19 11:42:28 +010010744static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010745 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010746 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010747 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010748 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010749 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010750 .tree_fs_info = iotree_fs_info,
10751 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010752
10753 /* optional callbacks */
10754 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010755 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010756 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010757 .set_bit_hook = btrfs_set_bit_hook,
10758 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010759 .merge_extent_hook = btrfs_merge_extent_hook,
10760 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010761 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010762};
10763
Chris Mason35054392009-01-21 13:11:13 -050010764/*
10765 * btrfs doesn't support the bmap operation because swapfiles
10766 * use bmap to make a mapping of extents in the file. They assume
10767 * these extents won't change over the life of the file and they
10768 * use the bmap result to do IO directly to the drive.
10769 *
10770 * the btrfs bmap call would return logical addresses that aren't
10771 * suitable for IO and they also will change frequently as COW
10772 * operations happen. So, swapfile + btrfs == corruption.
10773 *
10774 * For now we're avoiding this by dropping bmap.
10775 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010776static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010777 .readpage = btrfs_readpage,
10778 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010779 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010780 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010781 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010782 .invalidatepage = btrfs_invalidatepage,
10783 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010784 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010785 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010786};
10787
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010788static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010789 .readpage = btrfs_readpage,
10790 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010791 .invalidatepage = btrfs_invalidatepage,
10792 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010793};
10794
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010795static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010796 .getattr = btrfs_getattr,
10797 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010798 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010799 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010800 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010801 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010802 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010803 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010804};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010805static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010806 .getattr = btrfs_getattr,
10807 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010808 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010809 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010810 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010811 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010812 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010813};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010814static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010815 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010816 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010817 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010818 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010819 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010820 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010821};
Yan, Zheng76dda932009-09-21 16:00:26 -040010822
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010823const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010824 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010825 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010826};