blob: 6b8d34136044ca193f10f1ab0f2f113c42134c84 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Qu Wenruo524272602017-03-08 10:25:52 +0800118static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
121
122/*
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
125 *
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
134 */
135static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
136 const u64 offset,
137 const u64 bytes)
138{
139 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
140 bytes - PAGE_SIZE, false);
141}
142
Eric Sandeen48a3b632013-04-25 20:41:01 +0000143static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400144
Josef Bacik6a3891c2015-03-16 17:38:52 -0400145#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
146void btrfs_test_inode_set_ops(struct inode *inode)
147{
148 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
149}
150#endif
151
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000152static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500153 struct inode *inode, struct inode *dir,
154 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500155{
156 int err;
157
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000158 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500159 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500160 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500161 return err;
162}
163
Chris Masond352ac62008-09-29 15:18:18 -0400164/*
Chris Masonc8b97812008-10-29 14:49:59 -0400165 * this does all the hard work for inserting an inline extent into
166 * the btree. The caller should have done a btrfs_drop_extents so that
167 * no overlapping inline items exist in the btree
168 */
Chris Mason40f76582014-05-21 13:35:51 -0700169static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000170 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct btrfs_root *root, struct inode *inode,
172 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000173 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct page **compressed_pages)
175{
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct extent_buffer *leaf;
177 struct page *page = NULL;
178 char *kaddr;
179 unsigned long ptr;
180 struct btrfs_file_extent_item *ei;
181 int err = 0;
182 int ret;
183 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400184 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Li Zefanfe3f5662011-03-28 08:30:38 +0000186 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400187 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400188
Chris Masonc8b97812008-10-29 14:49:59 -0400189 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190
191 if (!extent_inserted) {
192 struct btrfs_key key;
193 size_t datasize;
194
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200195 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200197 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198
199 datasize = btrfs_file_extent_calc_inline_size(cur_size);
200 path->leave_spinning = 1;
201 ret = btrfs_insert_empty_item(trans, root, path, &key,
202 datasize);
203 if (ret) {
204 err = ret;
205 goto fail;
206 }
Chris Masonc8b97812008-10-29 14:49:59 -0400207 }
208 leaf = path->nodes[0];
209 ei = btrfs_item_ptr(leaf, path->slots[0],
210 struct btrfs_file_extent_item);
211 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
212 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
213 btrfs_set_file_extent_encryption(leaf, ei, 0);
214 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
215 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
216 ptr = btrfs_file_extent_inline_start(ei);
217
Li Zefan261507a02010-12-17 14:21:50 +0800218 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 struct page *cpage;
220 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500221 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400222 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500223 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300224 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
Cong Wang7ac687d2011-11-25 23:14:28 +0800226 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400227 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800228 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400229
230 i++;
231 ptr += cur_size;
232 compressed_size -= cur_size;
233 }
234 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800235 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 } else {
237 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300238 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800243 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 }
246 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000247 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400248
Yan, Zhengc2167752009-11-12 09:34:21 +0000249 /*
250 * we're an inline extent, so nobody can
251 * extend the file past i_size without locking
252 * a page we already have locked.
253 *
254 * We must do any isize and inode updates
255 * before we unlock the pages. Otherwise we
256 * could end up racing with unlink.
257 */
Chris Masonc8b97812008-10-29 14:49:59 -0400258 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000260
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400262fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400263 return err;
264}
265
266
267/*
268 * conditionally insert an inline extent into the file. This
269 * does the checks required to make sure the data is small enough
270 * to fit as an inline extent.
271 */
Josef Bacik00361582013-08-14 14:02:47 -0400272static noinline int cow_file_range_inline(struct btrfs_root *root,
273 struct inode *inode, u64 start,
274 u64 end, size_t compressed_size,
275 int compress_type,
276 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400278 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400279 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400280 u64 isize = i_size_read(inode);
281 u64 actual_end = min(end + 1, isize);
282 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400284 u64 data_len = inline_len;
285 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 struct btrfs_path *path;
287 int extent_inserted = 0;
288 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400289
290 if (compressed_size)
291 data_len = compressed_size;
292
293 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 actual_end > fs_info->sectorsize ||
295 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400296 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400298 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400300 return 1;
301 }
302
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 path = btrfs_alloc_path();
304 if (!path)
305 return -ENOMEM;
306
Josef Bacik00361582013-08-14 14:02:47 -0400307 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 if (IS_ERR(trans)) {
309 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400310 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400312 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400313
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 if (compressed_size && compressed_pages)
315 extent_item_size = btrfs_file_extent_calc_inline_size(
316 compressed_size);
317 else
318 extent_item_size = btrfs_file_extent_calc_inline_size(
319 inline_len);
320
321 ret = __btrfs_drop_extents(trans, root, inode, path,
322 start, aligned_end, NULL,
323 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400324 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400325 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400326 goto out;
327 }
Chris Masonc8b97812008-10-29 14:49:59 -0400328
329 if (isize > actual_end)
330 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000331 ret = insert_inline_extent(trans, path, extent_inserted,
332 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400333 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000334 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400335 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400336 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400337 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400338 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400339 ret = 1;
340 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100341 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400342
Josef Bacikbdc20e62013-02-28 13:23:38 -0500343 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200344 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200345 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400346out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800347 /*
348 * Don't forget to free the reserved space, as for inlined extent
349 * it won't count as data extent, free them directly here.
350 * And at reserve time, it's always aligned to page size, so
351 * just free one page here.
352 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300353 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000354 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400355 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400356 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400357}
358
Chris Mason771ed682008-11-06 22:02:51 -0500359struct async_extent {
360 u64 start;
361 u64 ram_size;
362 u64 compressed_size;
363 struct page **pages;
364 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800365 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head list;
367};
368
369struct async_cow {
370 struct inode *inode;
371 struct btrfs_root *root;
372 struct page *locked_page;
373 u64 start;
374 u64 end;
375 struct list_head extents;
376 struct btrfs_work work;
377};
378
379static noinline int add_async_extent(struct async_cow *cow,
380 u64 start, u64 ram_size,
381 u64 compressed_size,
382 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800383 unsigned long nr_pages,
384 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500385{
386 struct async_extent *async_extent;
387
388 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100389 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500390 async_extent->start = start;
391 async_extent->ram_size = ram_size;
392 async_extent->compressed_size = compressed_size;
393 async_extent->pages = pages;
394 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800395 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500396 list_add_tail(&async_extent->list, &cow->extents);
397 return 0;
398}
399
Wang Shilongf79707b2014-07-17 11:44:09 +0800400static inline int inode_need_compress(struct inode *inode)
401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400402 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800403
404 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400405 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800406 return 1;
407 /* bad compression ratios */
408 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
409 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800411 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
412 BTRFS_I(inode)->force_compress)
413 return 1;
414 return 0;
415}
416
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200417static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800418 u64 start, u64 end, u64 num_bytes, u64 small_write)
419{
420 /* If this is a small write inside eof, kick off a defrag */
421 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200422 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800423 btrfs_add_inode_defrag(NULL, inode);
424}
425
Chris Masonc8b97812008-10-29 14:49:59 -0400426/*
Chris Mason771ed682008-11-06 22:02:51 -0500427 * we create compressed extents in two phases. The first
428 * phase compresses a range of pages that have already been
429 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * This is done inside an ordered work queue, and the compression
432 * is spread across many cpus. The actual IO submission is step
433 * two, and the ordered work queue takes care of making sure that
434 * happens in the same order things were put onto the queue by
435 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400436 *
Chris Mason771ed682008-11-06 22:02:51 -0500437 * If this code finds it can't get good compression, it puts an
438 * entry onto the work queue to write the uncompressed bytes. This
439 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300440 * are written in the same order that the flusher thread sent them
441 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400442 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100443static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500444 struct page *locked_page,
445 u64 start, u64 end,
446 struct async_cow *async_cow,
447 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400449 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400450 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400451 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400452 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400453 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500454 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400455 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 struct page **pages = NULL;
457 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400458 unsigned long total_compressed = 0;
459 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400460 int i;
461 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400463 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400464
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200465 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
466 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400467
Chris Mason42dc7ba2008-12-15 11:44:56 -0500468 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400469again:
470 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300471 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100472 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
473 nr_pages = min_t(unsigned long, nr_pages,
474 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400475
Chris Masonf03d9301f2009-02-04 09:31:06 -0500476 /*
477 * we don't want to send crud past the end of i_size through
478 * compression, that's just a waste of CPU time. So, if the
479 * end of the file is before the start of our current
480 * requested range of bytes, we bail out to the uncompressed
481 * cleanup code that can deal with all of this.
482 *
483 * It isn't really the fastest way to fix things, but this is a
484 * very uncommon corner.
485 */
486 if (actual_end <= start)
487 goto cleanup_and_bail_uncompressed;
488
Chris Masonc8b97812008-10-29 14:49:59 -0400489 total_compressed = actual_end - start;
490
Shilong Wang4bcbb332014-10-07 18:44:35 -0400491 /*
492 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400493 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400494 */
495 if (total_compressed <= blocksize &&
496 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
497 goto cleanup_and_bail_uncompressed;
498
David Sterba069eac72017-02-14 19:36:54 +0100499 total_compressed = min_t(unsigned long, total_compressed,
500 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000501 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500502 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400503 total_in = 0;
504 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400505
Chris Mason771ed682008-11-06 22:02:51 -0500506 /*
507 * we do compression for mount -o compress and when the
508 * inode has not been flagged as nocompress. This flag can
509 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400510 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800511 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400512 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100513 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800514 if (!pages) {
515 /* just bail out to the uncompressed code */
516 goto cont;
517 }
Chris Mason179e29e2007-11-01 11:28:41 -0400518
Li Zefan261507a02010-12-17 14:21:50 +0800519 if (BTRFS_I(inode)->force_compress)
520 compress_type = BTRFS_I(inode)->force_compress;
521
Chris Mason4adaa612013-03-26 13:07:00 -0400522 /*
523 * we need to call clear_page_dirty_for_io on each
524 * page in the range. Otherwise applications with the file
525 * mmap'd can wander in and change the page contents while
526 * we are compressing them.
527 *
528 * If the compression fails for any reason, we set the pages
529 * dirty again later on.
530 */
531 extent_range_clear_dirty_for_io(inode, start, end);
532 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800533 ret = btrfs_compress_pages(compress_type,
534 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100535 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100536 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800537 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100538 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 if (!ret) {
541 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300542 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100543 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400544 char *kaddr;
545
546 /* zero the tail end of the last page, we might be
547 * sending it down to disk
548 */
549 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800550 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
555 will_compress = 1;
556 }
557 }
Li Zefan560f7d72011-09-08 10:22:01 +0800558cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (start == 0) {
560 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500561 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500563 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400564 */
Josef Bacik00361582013-08-14 14:02:47 -0400565 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800566 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500568 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400569 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000570 total_compressed,
571 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400574 unsigned long clear_flags = EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100575 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 unsigned long page_error_op;
577
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100579 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400580
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 * inline extent creation worked or returned error,
583 * we don't need to create any more async work items.
584 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500585 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800586 extent_clear_unlock_delalloc(inode, start, end, end,
587 NULL, clear_flags,
588 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400589 PAGE_CLEAR_DIRTY |
590 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400592 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000593 if (ret == 0)
594 btrfs_free_reserved_data_space_noquota(inode,
595 start,
596 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 goto free_pages_out;
598 }
599 }
600
601 if (will_compress) {
602 /*
603 * we aren't doing an inline extent round the compressed size
604 * up to a block size boundary so the allocator does sane
605 * things
606 */
Qu Wenruofda28322013-02-26 08:10:22 +0000607 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400608
609 /*
610 * one last check to make sure the compression is really a
611 * win, compare the page count read with the blocks on disk
612 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300613 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400614 if (total_compressed >= total_in) {
615 will_compress = 0;
616 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700618 *num_added += 1;
619
620 /*
621 * The async work queues will take care of doing actual
622 * allocation on disk for these compressed pages, and
623 * will submit them to the elevator.
624 */
625 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100626 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700627 compress_type);
628
629 if (start + num_bytes < end) {
630 start += num_bytes;
631 pages = NULL;
632 cond_resched();
633 goto again;
634 }
635 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400636 }
637 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400639 /*
640 * the compression code ran but failed to make things smaller,
641 * free any pages it allocated and our page pointer array
642 */
David Sterba4d3a8002017-02-14 19:04:07 +0100643 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400644 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300645 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400646 }
647 kfree(pages);
648 pages = NULL;
649 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100650 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400651
652 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400653 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500654 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500655 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500656 }
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500658cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700659 /*
660 * No compression, but we still need to write the pages in the file
661 * we've been given so far. redirty the locked page if it corresponds
662 * to our extent and set things up for the async work queue to run
663 * cow_file_range to do the normal delalloc dance.
664 */
665 if (page_offset(locked_page) >= start &&
666 page_offset(locked_page) <= end)
667 __set_page_dirty_nobuffers(locked_page);
668 /* unlocked later on in the async handlers */
669
670 if (redirty)
671 extent_range_redirty_for_io(inode, start, end);
672 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
673 BTRFS_COMPRESS_NONE);
674 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500675
Filipe Mananac44f6492014-10-09 21:15:44 +0100676 return;
Chris Mason771ed682008-11-06 22:02:51 -0500677
678free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100679 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500680 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300681 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500682 }
Chris Masond3977122009-01-05 21:25:51 -0500683 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500684}
Chris Mason771ed682008-11-06 22:02:51 -0500685
Filipe Manana40ae8372014-10-06 22:14:24 +0100686static void free_async_extent_pages(struct async_extent *async_extent)
687{
688 int i;
689
690 if (!async_extent->pages)
691 return;
692
693 for (i = 0; i < async_extent->nr_pages; i++) {
694 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100696 }
697 kfree(async_extent->pages);
698 async_extent->nr_pages = 0;
699 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500700}
701
702/*
703 * phase two of compressed writeback. This is the ordered portion
704 * of the code, which only gets called in the order the work was
705 * queued. We walk all the async extents created by compress_file_range
706 * and send them down to the disk.
707 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100708static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500709 struct async_cow *async_cow)
710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500712 struct async_extent *async_extent;
713 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500714 struct btrfs_key ins;
715 struct extent_map *em;
716 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500717 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500718 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500719
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720again:
Chris Masond3977122009-01-05 21:25:51 -0500721 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent = list_entry(async_cow->extents.next,
723 struct async_extent, list);
724 list_del(&async_extent->list);
725
726 io_tree = &BTRFS_I(inode)->io_tree;
727
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728retry:
Chris Mason771ed682008-11-06 22:02:51 -0500729 /* did the compression code fall back to uncompressed IO? */
730 if (!async_extent->pages) {
731 int page_started = 0;
732 unsigned long nr_written = 0;
733
734 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000735 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100736 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500737
738 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 ret = cow_file_range(inode, async_cow->locked_page,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800743 async_extent->start +
744 async_extent->ram_size - 1,
745 &page_started, &nr_written, 0,
746 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 /* JDM XXX */
749
Chris Mason771ed682008-11-06 22:02:51 -0500750 /*
751 * if page_started, cow_file_range inserted an
752 * inline extent and took care of all the unlocking
753 * and IO for us. Otherwise, we need to submit
754 * all those pages down to the drive.
755 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500757 extent_write_locked_range(io_tree,
758 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500760 async_extent->ram_size - 1,
761 btrfs_get_extent,
762 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763 else if (ret)
764 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500765 kfree(async_extent);
766 cond_resched();
767 continue;
768 }
769
770 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100771 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500772
Wang Xiaoguang18513092016-07-25 15:51:40 +0800773 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500774 async_extent->compressed_size,
775 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800776 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500777 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100778 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500779
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400780 if (ret == -ENOSPC) {
781 unlock_extent(io_tree, async_extent->start,
782 async_extent->start +
783 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800784
785 /*
786 * we need to redirty the pages if we decide to
787 * fallback to uncompressed IO, otherwise we
788 * will not submit these pages down to lower
789 * layers.
790 */
791 extent_range_redirty_for_io(inode,
792 async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1);
795
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100796 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400797 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500799 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000800 /*
801 * here we're doing allocation and writeback of the
802 * compressed pages
803 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800804 em = create_io_em(inode, async_extent->start,
805 async_extent->ram_size, /* len */
806 async_extent->start, /* orig_start */
807 ins.objectid, /* block_start */
808 ins.offset, /* block_len */
809 ins.offset, /* orig_block_len */
810 async_extent->ram_size, /* ram_bytes */
811 async_extent->compress_type,
812 BTRFS_ORDERED_COMPRESSED);
813 if (IS_ERR(em))
814 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500815 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800816 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500817
Li Zefan261507a02010-12-17 14:21:50 +0800818 ret = btrfs_add_ordered_extent_compress(inode,
819 async_extent->start,
820 ins.objectid,
821 async_extent->ram_size,
822 ins.offset,
823 BTRFS_ORDERED_COMPRESSED,
824 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100825 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200826 btrfs_drop_extent_cache(BTRFS_I(inode),
827 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100828 async_extent->start +
829 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100831 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400832 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500833
Chris Mason771ed682008-11-06 22:02:51 -0500834 /*
835 * clear dirty, set writeback and unlock the pages.
836 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400837 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400838 async_extent->start +
839 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800840 async_extent->start +
841 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400842 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
843 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400844 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500845 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500846 async_extent->start,
847 async_extent->ram_size,
848 ins.objectid,
849 ins.offset, async_extent->pages,
850 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100851 if (ret) {
852 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
853 struct page *p = async_extent->pages[0];
854 const u64 start = async_extent->start;
855 const u64 end = start + async_extent->ram_size - 1;
856
857 p->mapping = inode->i_mapping;
858 tree->ops->writepage_end_io_hook(p, start, end,
859 NULL, 0);
860 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800861 extent_clear_unlock_delalloc(inode, start, end, end,
862 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 PAGE_END_WRITEBACK |
864 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100865 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 }
Chris Mason771ed682008-11-06 22:02:51 -0500867 alloc_hint = ins.objectid + ins.offset;
868 kfree(async_extent);
869 cond_resched();
870 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100871 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400873 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400874 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500877 async_extent->start +
878 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800879 async_extent->start +
880 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400881 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100882 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400883 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
884 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100885 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
886 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100887 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100888 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500889 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500890}
891
Josef Bacik4b46fce2010-05-23 11:00:55 -0400892static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
893 u64 num_bytes)
894{
895 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
896 struct extent_map *em;
897 u64 alloc_hint = 0;
898
899 read_lock(&em_tree->lock);
900 em = search_extent_mapping(em_tree, start, num_bytes);
901 if (em) {
902 /*
903 * if block start isn't an actual block number then find the
904 * first block in this inode and use that as a hint. If that
905 * block is also bogus then just don't worry about it.
906 */
907 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
908 free_extent_map(em);
909 em = search_extent_mapping(em_tree, 0, 0);
910 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
911 alloc_hint = em->block_start;
912 if (em)
913 free_extent_map(em);
914 } else {
915 alloc_hint = em->block_start;
916 free_extent_map(em);
917 }
918 }
919 read_unlock(&em_tree->lock);
920
921 return alloc_hint;
922}
923
Chris Mason771ed682008-11-06 22:02:51 -0500924/*
925 * when extent_io.c finds a delayed allocation range in the file,
926 * the call backs end up in this code. The basic idea is to
927 * allocate extents on disk for the range, and create ordered data structs
928 * in ram to track those extents.
929 *
930 * locked_page is the page that writepage had locked already. We use
931 * it to make sure we don't do extra locks or unlocks.
932 *
933 * *page_started is set to one if we unlock locked_page and do everything
934 * required to start IO on it. It may be clean and already done with
935 * IO when we return.
936 */
Josef Bacik00361582013-08-14 14:02:47 -0400937static noinline int cow_file_range(struct inode *inode,
938 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800939 u64 start, u64 end, u64 delalloc_end,
940 int *page_started, unsigned long *nr_written,
941 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400944 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500945 u64 alloc_hint = 0;
946 u64 num_bytes;
947 unsigned long ram_size;
948 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000949 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400950 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500951 struct btrfs_key ins;
952 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000953 unsigned clear_bits;
954 unsigned long page_ops;
955 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500956 int ret = 0;
957
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200958 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500967
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200968 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400969
Chris Mason771ed682008-11-06 22:02:51 -0500970 if (start == 0) {
971 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800972 ret = cow_file_range_inline(root, inode, start, end, 0,
973 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500974 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800975 extent_clear_unlock_delalloc(inode, start, end,
976 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400977 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100978 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400979 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
981 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800982 btrfs_free_reserved_data_space_noquota(inode, start,
983 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500984 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300985 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500986 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500987 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100988 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100989 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500990 }
991 }
Chris Masonc8b97812008-10-29 14:49:59 -0400992
993 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400994 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400995
Josef Bacik4b46fce2010-05-23 11:00:55 -0400996 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200997 btrfs_drop_extent_cache(BTRFS_I(inode), start,
998 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400999
Chris Masond3977122009-01-05 21:25:51 -05001000 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001001 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001002 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001003 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001004 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001005 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001007 cur_alloc_size = ins.offset;
1008 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001009
Chris Mason771ed682008-11-06 22:02:51 -05001010 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001011 em = create_io_em(inode, start, ins.offset, /* len */
1012 start, /* orig_start */
1013 ins.objectid, /* block_start */
1014 ins.offset, /* block_len */
1015 ins.offset, /* orig_block_len */
1016 ram_size, /* ram_bytes */
1017 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001018 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001019 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001020 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001027
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (root->root_key.objectid ==
1029 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1030 ret = btrfs_reloc_clone_csums(inode, start,
1031 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001032 /*
1033 * Only drop cache here, and process as normal.
1034 *
1035 * We must not allow extent_clear_unlock_delalloc()
1036 * at out_unlock label to free meta of this ordered
1037 * extent, as its meta should be freed by
1038 * btrfs_finish_ordered_io().
1039 *
1040 * So we must continue until @start is increased to
1041 * skip current ordered extent.
1042 */
Josef Bacik00361582013-08-14 14:02:47 -04001043 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001044 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1045 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001046 }
1047
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001048 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001049
Chris Masonc8b97812008-10-29 14:49:59 -04001050 /* we're not doing compressed IO, don't unlock the first
1051 * page (which the caller expects to stay locked), don't
1052 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001053 *
1054 * Do set the Private2 bit so we know this page was properly
1055 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001056 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001057 page_ops = unlock ? PAGE_UNLOCK : 0;
1058 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001059
Josef Bacikc2790a22013-07-29 11:20:47 -04001060 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001061 start + ram_size - 1,
1062 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001063 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001064 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001065 if (disk_num_bytes < cur_alloc_size)
1066 disk_num_bytes = 0;
1067 else
1068 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001069 num_bytes -= cur_alloc_size;
1070 alloc_hint = ins.objectid + ins.offset;
1071 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001073
1074 /*
1075 * btrfs_reloc_clone_csums() error, since start is increased
1076 * extent_clear_unlock_delalloc() at out_unlock label won't
1077 * free metadata of current ordered extent, we're OK to exit.
1078 */
1079 if (ret)
1080 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001081 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001082out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001083 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001084
Filipe Mananad9f85962014-08-25 10:43:00 +01001085out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001086 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001087out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001088 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001089 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001090out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001091 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1092 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001093 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1094 PAGE_END_WRITEBACK;
1095 /*
1096 * If we reserved an extent for our delalloc range (or a subrange) and
1097 * failed to create the respective ordered extent, then it means that
1098 * when we reserved the extent we decremented the extent's size from
1099 * the data space_info's bytes_may_use counter and incremented the
1100 * space_info's bytes_reserved counter by the same amount. We must make
1101 * sure extent_clear_unlock_delalloc() does not try to decrement again
1102 * the data space_info's bytes_may_use counter, therefore we do not pass
1103 * it the flag EXTENT_CLEAR_DATA_RESV.
1104 */
1105 if (extent_reserved) {
1106 extent_clear_unlock_delalloc(inode, start,
1107 start + cur_alloc_size,
1108 start + cur_alloc_size,
1109 locked_page,
1110 clear_bits,
1111 page_ops);
1112 start += cur_alloc_size;
1113 if (start >= end)
1114 goto out;
1115 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001116 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1117 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001118 clear_bits | EXTENT_CLEAR_DATA_RESV,
1119 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001120 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001121}
Chris Masonc8b97812008-10-29 14:49:59 -04001122
Chris Mason771ed682008-11-06 22:02:51 -05001123/*
1124 * work queue call back to started compression on a file and pages
1125 */
1126static noinline void async_cow_start(struct btrfs_work *work)
1127{
1128 struct async_cow *async_cow;
1129 int num_added = 0;
1130 async_cow = container_of(work, struct async_cow, work);
1131
1132 compress_file_range(async_cow->inode, async_cow->locked_page,
1133 async_cow->start, async_cow->end, async_cow,
1134 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001135 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001136 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001137 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001138 }
Chris Mason771ed682008-11-06 22:02:51 -05001139}
1140
1141/*
1142 * work queue call back to submit previously compressed pages
1143 */
1144static noinline void async_cow_submit(struct btrfs_work *work)
1145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001146 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001147 struct async_cow *async_cow;
1148 struct btrfs_root *root;
1149 unsigned long nr_pages;
1150
1151 async_cow = container_of(work, struct async_cow, work);
1152
1153 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001154 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001155 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1156 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001157
David Sterbaee863952015-02-16 19:41:40 +01001158 /*
1159 * atomic_sub_return implies a barrier for waitqueue_active
1160 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001162 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001163 waitqueue_active(&fs_info->async_submit_wait))
1164 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001165
Chris Masond3977122009-01-05 21:25:51 -05001166 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001167 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001168}
Chris Masonc8b97812008-10-29 14:49:59 -04001169
Chris Mason771ed682008-11-06 22:02:51 -05001170static noinline void async_cow_free(struct btrfs_work *work)
1171{
1172 struct async_cow *async_cow;
1173 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001174 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001175 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001176 kfree(async_cow);
1177}
1178
1179static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1180 u64 start, u64 end, int *page_started,
1181 unsigned long *nr_written)
1182{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001184 struct async_cow *async_cow;
1185 struct btrfs_root *root = BTRFS_I(inode)->root;
1186 unsigned long nr_pages;
1187 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001188
Chris Masona3429ab2009-10-08 12:30:20 -04001189 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1190 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001191 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001192 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001193 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001194 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001195 async_cow->root = root;
1196 async_cow->locked_page = locked_page;
1197 async_cow->start = start;
1198
Wang Shilongf79707b2014-07-17 11:44:09 +08001199 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001201 cur_end = end;
1202 else
Byongho Leeee221842015-12-15 01:42:10 +09001203 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001204
1205 async_cow->end = cur_end;
1206 INIT_LIST_HEAD(&async_cow->extents);
1207
Liu Bo9e0af232014-08-15 23:36:53 +08001208 btrfs_init_work(&async_cow->work,
1209 btrfs_delalloc_helper,
1210 async_cow_start, async_cow_submit,
1211 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001212
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001213 nr_pages = (cur_end - start + PAGE_SIZE) >>
1214 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001218
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001219 while (atomic_read(&fs_info->async_submit_draining) &&
1220 atomic_read(&fs_info->async_delalloc_pages)) {
1221 wait_event(fs_info->async_submit_wait,
1222 (atomic_read(&fs_info->async_delalloc_pages) ==
1223 0));
Chris Mason771ed682008-11-06 22:02:51 -05001224 }
1225
1226 *nr_written += nr_pages;
1227 start = cur_end + 1;
1228 }
1229 *page_started = 1;
1230 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001231}
1232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001233static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001234 u64 bytenr, u64 num_bytes)
1235{
1236 int ret;
1237 struct btrfs_ordered_sum *sums;
1238 LIST_HEAD(list);
1239
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001240 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001241 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001242 if (ret == 0 && list_empty(&list))
1243 return 0;
1244
1245 while (!list_empty(&list)) {
1246 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1247 list_del(&sums->list);
1248 kfree(sums);
1249 }
1250 return 1;
1251}
1252
Chris Masond352ac62008-09-29 15:18:18 -04001253/*
1254 * when nowcow writeback call back. This checks for snapshots or COW copies
1255 * of the extents that exist in the file, and COWs the file as required.
1256 *
1257 * If no cow copies or snapshots exist, we write directly to the existing
1258 * blocks on disk
1259 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001260static noinline int run_delalloc_nocow(struct inode *inode,
1261 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001262 u64 start, u64 end, int *page_started, int force,
1263 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_root *root = BTRFS_I(inode)->root;
1267 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001270 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001271 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 u64 cow_start;
1273 u64 cur_offset;
1274 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 u64 disk_bytenr;
1277 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001278 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001279 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001281 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001282 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 int nocow;
1284 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001285 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001286 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001287
1288 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001289 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001290 extent_clear_unlock_delalloc(inode, start, end, end,
1291 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001292 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001293 EXTENT_DO_ACCOUNTING |
1294 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001295 PAGE_CLEAR_DIRTY |
1296 PAGE_SET_WRITEBACK |
1297 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001298 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 }
Li Zefan82d59022011-04-20 10:33:24 +08001300
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001301 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 cow_start = (u64)-1;
1304 cur_offset = start;
1305 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001306 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001308 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1311 leaf = path->nodes[0];
1312 btrfs_item_key_to_cpu(leaf, &found_key,
1313 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001314 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 found_key.type == BTRFS_EXTENT_DATA_KEY)
1316 path->slots[0]--;
1317 }
1318 check_prev = 0;
1319next_slot:
1320 leaf = path->nodes[0];
1321 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1322 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001323 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001324 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (ret > 0)
1326 break;
1327 leaf = path->nodes[0];
1328 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001329
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 nocow = 0;
1331 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
Filipe Manana1d512cb2015-11-09 00:33:58 +00001335 if (found_key.objectid > ino)
1336 break;
1337 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1338 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1339 path->slots[0]++;
1340 goto next_slot;
1341 }
1342 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 found_key.offset > end)
1344 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001345
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 if (found_key.offset > cur_offset) {
1347 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001348 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 goto out_check;
1350 }
Chris Masonc31f8832008-01-08 15:46:31 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 fi = btrfs_item_ptr(leaf, path->slots[0],
1353 struct btrfs_file_extent_item);
1354 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Josef Bacikcc95bef2013-04-04 14:31:27 -04001356 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1358 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 extent_end = found_key.offset +
1362 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001363 disk_num_bytes =
1364 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (extent_end <= start) {
1366 path->slots[0]++;
1367 goto next_slot;
1368 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 if (disk_bytenr == 0)
1370 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (btrfs_file_extent_compression(leaf, fi) ||
1372 btrfs_file_extent_encryption(leaf, fi) ||
1373 btrfs_file_extent_other_encoding(leaf, fi))
1374 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1376 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001377 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001379 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001380 found_key.offset -
1381 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001383 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 disk_bytenr += cur_offset - found_key.offset;
1385 num_bytes = min(end + 1, extent_end) - cur_offset;
1386 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001387 * if there are pending snapshots for this root,
1388 * we fall into common COW way.
1389 */
1390 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001391 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 if (!err)
1393 goto out_check;
1394 }
1395 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 * force cow if csum exists in the range.
1397 * this ensure that csum for a given extent are
1398 * either valid or do not exist.
1399 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001400 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001401 num_bytes)) {
1402 if (!nolock)
1403 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001404 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001405 }
1406 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1407 if (!nolock)
1408 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001409 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001410 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 nocow = 1;
1412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1413 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001414 btrfs_file_extent_inline_len(leaf,
1415 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001416 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001417 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 } else {
1419 BUG_ON(1);
1420 }
1421out_check:
1422 if (extent_end <= start) {
1423 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001424 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001425 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 goto next_slot;
1429 }
1430 if (!nocow) {
1431 if (cow_start == (u64)-1)
1432 cow_start = cur_offset;
1433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
1436 path->slots[0]++;
1437 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001439
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001442 ret = cow_file_range(inode, locked_page,
1443 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001444 end, page_started, nr_written, 1,
1445 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001446 if (ret) {
1447 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001448 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001450 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001451 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001452 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001455 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001456
Yan Zhengd899e052008-10-30 14:25:28 -04001457 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001458 u64 orig_start = found_key.offset - extent_offset;
1459
1460 em = create_io_em(inode, cur_offset, num_bytes,
1461 orig_start,
1462 disk_bytenr, /* block_start */
1463 num_bytes, /* block_len */
1464 disk_num_bytes, /* orig_block_len */
1465 ram_bytes, BTRFS_COMPRESS_NONE,
1466 BTRFS_ORDERED_PREALLOC);
1467 if (IS_ERR(em)) {
1468 if (!nolock && nocow)
1469 btrfs_end_write_no_snapshoting(root);
1470 if (nocow)
1471 btrfs_dec_nocow_writers(fs_info,
1472 disk_bytenr);
1473 ret = PTR_ERR(em);
1474 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001475 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001476 free_extent_map(em);
1477 }
1478
1479 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001480 type = BTRFS_ORDERED_PREALLOC;
1481 } else {
1482 type = BTRFS_ORDERED_NOCOW;
1483 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001484
1485 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001486 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001487 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001488 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001490
Yan, Zhengefa56462010-05-16 10:49:59 -04001491 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001492 BTRFS_DATA_RELOC_TREE_OBJECTID)
1493 /*
1494 * Error handled later, as we must prevent
1495 * extent_clear_unlock_delalloc() in error handler
1496 * from freeing metadata of created ordered extent.
1497 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001498 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1499 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001500
Josef Bacikc2790a22013-07-29 11:20:47 -04001501 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001502 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001503 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001504 EXTENT_DELALLOC |
1505 EXTENT_CLEAR_DATA_RESV,
1506 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1507
Wang Shilonge9894fd2014-03-27 11:12:25 +08001508 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001509 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001510 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001511
1512 /*
1513 * btrfs_reloc_clone_csums() error, now we're OK to call error
1514 * handler, as metadata for created ordered extent will only
1515 * be freed by btrfs_finish_ordered_io().
1516 */
1517 if (ret)
1518 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 if (cur_offset > end)
1520 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001522 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001523
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001526 cur_offset = end;
1527 }
1528
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001530 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1531 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001532 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001533 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 }
1535
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001536error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001538 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001540 EXTENT_DELALLOC | EXTENT_DEFRAG |
1541 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1542 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 PAGE_SET_WRITEBACK |
1544 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001545 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001547}
1548
Wang Shilong47059d92014-07-03 18:22:07 +08001549static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1550{
1551
1552 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1553 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1554 return 0;
1555
1556 /*
1557 * @defrag_bytes is a hint value, no spinlock held here,
1558 * if is not zero, it means the file is defragging.
1559 * Force cow if given extent needs to be defragged.
1560 */
1561 if (BTRFS_I(inode)->defrag_bytes &&
1562 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1563 EXTENT_DEFRAG, 0, NULL))
1564 return 1;
1565
1566 return 0;
1567}
1568
Chris Masond352ac62008-09-29 15:18:18 -04001569/*
1570 * extent_io.c call back to do delayed allocation processing
1571 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001572static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001573 u64 start, u64 end, int *page_started,
1574 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001575{
Josef Bacikc6100a42017-05-05 11:57:13 -04001576 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001577 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001578 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001579
Wang Shilong47059d92014-07-03 18:22:07 +08001580 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001581 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001582 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001583 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001584 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001585 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001586 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001587 ret = cow_file_range(inode, locked_page, start, end, end,
1588 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001589 } else {
1590 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1591 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001592 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001594 }
Qu Wenruo524272602017-03-08 10:25:52 +08001595 if (ret)
1596 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001597 return ret;
1598}
1599
Josef Bacikc6100a42017-05-05 11:57:13 -04001600static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001601 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602{
Josef Bacikc6100a42017-05-05 11:57:13 -04001603 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 u64 size;
1605
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001606 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001608 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 size = orig->end - orig->start + 1;
1611 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001612 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001613 u64 new_size;
1614
1615 /*
Josef Bacikba117212015-03-13 15:01:24 -04001616 * See the explanation in btrfs_merge_extent_hook, the same
1617 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 */
1619 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001620 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001621 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001622 num_extents += count_max_extents(new_size);
1623 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001624 return;
1625 }
1626
Josef Bacik9e0baf62011-07-15 15:16:44 +00001627 spin_lock(&BTRFS_I(inode)->lock);
1628 BTRFS_I(inode)->outstanding_extents++;
1629 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001630}
1631
1632/*
1633 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1634 * extents so we can keep track of new extents that are just merged onto old
1635 * extents, such as when we are doing sequential writes, so we can properly
1636 * account for the metadata space we'll need.
1637 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001638static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001639 struct extent_state *new,
1640 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641{
Josef Bacikc6100a42017-05-05 11:57:13 -04001642 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001644 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645
Josef Bacik9ed74f22009-09-11 16:12:44 -04001646 /* not delalloc, ignore it */
1647 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001648 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001649
Josef Bacik8461a3d2015-03-13 15:12:08 -04001650 if (new->start > other->start)
1651 new_size = new->end - other->start + 1;
1652 else
1653 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001654
1655 /* we're not bigger than the max, unreserve the space and go */
1656 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1657 spin_lock(&BTRFS_I(inode)->lock);
1658 BTRFS_I(inode)->outstanding_extents--;
1659 spin_unlock(&BTRFS_I(inode)->lock);
1660 return;
1661 }
1662
1663 /*
Josef Bacikba117212015-03-13 15:01:24 -04001664 * We have to add up either side to figure out how many extents were
1665 * accounted for before we merged into one big extent. If the number of
1666 * extents we accounted for is <= the amount we need for the new range
1667 * then we can return, otherwise drop. Think of it like this
1668 *
1669 * [ 4k][MAX_SIZE]
1670 *
1671 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1672 * need 2 outstanding extents, on one side we have 1 and the other side
1673 * we have 1 so they are == and we can return. But in this case
1674 *
1675 * [MAX_SIZE+4k][MAX_SIZE+4k]
1676 *
1677 * Each range on their own accounts for 2 extents, but merged together
1678 * they are only 3 extents worth of accounting, so we need to drop in
1679 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001680 */
Josef Bacikba117212015-03-13 15:01:24 -04001681 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001682 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001683 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001684 num_extents += count_max_extents(old_size);
1685 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686 return;
1687
Josef Bacik9e0baf62011-07-15 15:16:44 +00001688 spin_lock(&BTRFS_I(inode)->lock);
1689 BTRFS_I(inode)->outstanding_extents--;
1690 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001691}
1692
Miao Xieeb73c1b2013-05-15 07:48:22 +00001693static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1694 struct inode *inode)
1695{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001696 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1697
Miao Xieeb73c1b2013-05-15 07:48:22 +00001698 spin_lock(&root->delalloc_lock);
1699 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1700 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1701 &root->delalloc_inodes);
1702 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1703 &BTRFS_I(inode)->runtime_flags);
1704 root->nr_delalloc_inodes++;
1705 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001706 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001707 BUG_ON(!list_empty(&root->delalloc_root));
1708 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001709 &fs_info->delalloc_roots);
1710 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001711 }
1712 }
1713 spin_unlock(&root->delalloc_lock);
1714}
1715
1716static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001717 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720
Miao Xieeb73c1b2013-05-15 07:48:22 +00001721 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001722 if (!list_empty(&inode->delalloc_inodes)) {
1723 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001725 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001726 root->nr_delalloc_inodes--;
1727 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001728 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001729 BUG_ON(list_empty(&root->delalloc_root));
1730 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001731 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 }
1733 }
1734 spin_unlock(&root->delalloc_lock);
1735}
1736
Chris Masond352ac62008-09-29 15:18:18 -04001737/*
1738 * extent_io.c set_bit_hook, used to track delayed allocation
1739 * bytes in this file, and to maintain the list of inodes that
1740 * have pending delalloc work to be done.
1741 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001742static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001743 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001744{
Josef Bacikc6100a42017-05-05 11:57:13 -04001745 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001746
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1748
Wang Shilong47059d92014-07-03 18:22:07 +08001749 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1750 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001751 /*
1752 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001753 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001754 * bit, which is only set or cleared with irqs on
1755 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001757 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001758 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001759 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001760
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001763 } else {
1764 spin_lock(&BTRFS_I(inode)->lock);
1765 BTRFS_I(inode)->outstanding_extents++;
1766 spin_unlock(&BTRFS_I(inode)->lock);
1767 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001768
Josef Bacik6a3891c2015-03-16 17:38:52 -04001769 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001770 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001771 return;
1772
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001773 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1774 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001775 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001777 if (*bits & EXTENT_DEFRAG)
1778 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001779 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001780 &BTRFS_I(inode)->runtime_flags))
1781 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001782 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001783 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001784
1785 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1786 (*bits & EXTENT_DELALLOC_NEW)) {
1787 spin_lock(&BTRFS_I(inode)->lock);
1788 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1789 state->start;
1790 spin_unlock(&BTRFS_I(inode)->lock);
1791 }
Chris Mason291d6732008-01-29 15:55:23 -05001792}
1793
Chris Masond352ac62008-09-29 15:18:18 -04001794/*
1795 * extent_io.c clear_bit_hook, see set_bit_hook for why
1796 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001797static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001798 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001799 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001800{
Josef Bacikc6100a42017-05-05 11:57:13 -04001801 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001802 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001803 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001804 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001805
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001806 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001807 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001808 inode->defrag_bytes -= len;
1809 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001810
Chris Mason75eff682008-12-15 15:54:40 -05001811 /*
1812 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001813 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001814 * bit, which is only set or cleared with irqs on
1815 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001816 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001817 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001818 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001819
Josef Bacik9e0baf62011-07-15 15:16:44 +00001820 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001821 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001822 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001823 spin_lock(&inode->lock);
1824 inode->outstanding_extents -= num_extents;
1825 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001826 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827
Josef Bacikb6d08f02013-09-27 14:57:43 -04001828 /*
1829 * We don't reserve metadata space for space cache inodes so we
1830 * don't need to call dellalloc_release_metadata if there is an
1831 * error.
1832 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001833 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001834 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835 btrfs_delalloc_release_metadata(inode, len);
1836
Josef Bacik6a3891c2015-03-16 17:38:52 -04001837 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001838 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001839 return;
1840
Filipe Mananaa315e682017-03-06 23:04:20 +00001841 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1842 do_list && !(state->state & EXTENT_NORESERVE) &&
1843 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001844 btrfs_free_reserved_data_space_noquota(
1845 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001846 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001847
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1849 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001850 spin_lock(&inode->lock);
1851 inode->delalloc_bytes -= len;
1852 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001853 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001854 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001855 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001857 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001858
1859 if ((state->state & EXTENT_DELALLOC_NEW) &&
1860 (*bits & EXTENT_DELALLOC_NEW)) {
1861 spin_lock(&inode->lock);
1862 ASSERT(inode->new_delalloc_bytes >= len);
1863 inode->new_delalloc_bytes -= len;
1864 spin_unlock(&inode->lock);
1865 }
Chris Mason291d6732008-01-29 15:55:23 -05001866}
1867
Chris Masond352ac62008-09-29 15:18:18 -04001868/*
1869 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1870 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001871 *
1872 * return 1 if page cannot be merged to bio
1873 * return 0 if page can be merged to bio
1874 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001875 */
Mike Christie81a75f672016-06-05 14:31:54 -05001876int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001877 size_t size, struct bio *bio,
1878 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880 struct inode *inode = page->mapping->host;
1881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001882 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001883 u64 length = 0;
1884 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001885 int ret;
1886
Chris Mason771ed682008-11-06 22:02:51 -05001887 if (bio_flags & EXTENT_BIO_COMPRESSED)
1888 return 0;
1889
Kent Overstreet4f024f32013-10-11 15:44:27 -07001890 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001891 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001892 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1893 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001894 if (ret < 0)
1895 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001896 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001897 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001898 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001899}
1900
Chris Masond352ac62008-09-29 15:18:18 -04001901/*
1902 * in order to insert checksums into the metadata in large chunks,
1903 * we wait until bio submission time. All the pages in the bio are
1904 * checksummed and sums are attached onto the ordered extent record.
1905 *
1906 * At IO completion time the cums attached on the ordered extent record
1907 * are inserted into the btree
1908 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001909static int __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001910 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001911 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001912{
Josef Bacikc6100a42017-05-05 11:57:13 -04001913 struct inode *inode = private_data;
Chris Mason065631f2008-02-20 12:07:25 -05001914 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001915
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001916 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001917 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001918 return 0;
1919}
Chris Masone0156402008-04-16 11:15:20 -04001920
Chris Mason4a69a412008-11-06 22:03:00 -05001921/*
1922 * in order to insert checksums into the metadata in large chunks,
1923 * we wait until bio submission time. All the pages in the bio are
1924 * checksummed and sums are attached onto the ordered extent record.
1925 *
1926 * At IO completion time the cums attached on the ordered extent record
1927 * are inserted into the btree
1928 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001929static int __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001930 int mirror_num, unsigned long bio_flags,
1931 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001932{
Josef Bacikc6100a42017-05-05 11:57:13 -04001933 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001935 int ret;
1936
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001937 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001938 if (ret) {
1939 bio->bi_error = ret;
1940 bio_endio(bio);
1941 }
Stefan Behrens61891922012-11-05 18:51:52 +01001942 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001943}
1944
Chris Masond352ac62008-09-29 15:18:18 -04001945/*
Chris Masoncad321a2008-12-17 14:51:42 -05001946 * extent_io.c submission hook. This does the right thing for csum calculation
1947 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001948 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001949static int btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1950 int mirror_num, unsigned long bio_flags,
1951 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001952{
Josef Bacikc6100a42017-05-05 11:57:13 -04001953 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301956 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001957 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001958 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001959 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001960
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001961 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001962
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001963 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301964 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001965
Mike Christie37226b22016-06-05 14:31:52 -05001966 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001968 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001969 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001970
Chris Masond20f7042008-12-08 16:58:54 -05001971 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001972 ret = btrfs_submit_compressed_read(inode, bio,
1973 mirror_num,
1974 bio_flags);
1975 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001976 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001977 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001978 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001979 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001980 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001981 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001982 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001983 /* csum items have already been cloned */
1984 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1985 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001986 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001987 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1988 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 __btrfs_submit_bio_start,
1990 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001991 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001992 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001993 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001994 if (ret)
1995 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001996 }
1997
Chris Mason0b86a832008-03-24 15:01:56 -04001998mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001999 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002000
2001out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002002 if (ret < 0) {
2003 bio->bi_error = ret;
2004 bio_endio(bio);
2005 }
Stefan Behrens61891922012-11-05 18:51:52 +01002006 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002007}
Chris Mason6885f302008-02-20 16:11:05 -05002008
Chris Masond352ac62008-09-29 15:18:18 -04002009/*
2010 * given a list of ordered sums record them in the inode. This happens
2011 * at IO completion time based on sums calculated at bio submission time.
2012 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002013static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002014 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002016 struct btrfs_ordered_sum *sum;
2017
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002018 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002019 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002020 btrfs_csum_file_blocks(trans,
2021 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002022 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 }
2024 return 0;
2025}
2026
Josef Bacik2ac55d42010-02-03 19:33:23 +00002027int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002028 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002029{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002030 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002031 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002032 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002033}
2034
Chris Masond352ac62008-09-29 15:18:18 -04002035/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002036struct btrfs_writepage_fixup {
2037 struct page *page;
2038 struct btrfs_work work;
2039};
2040
Christoph Hellwigb2950862008-12-02 09:54:17 -05002041static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002042{
2043 struct btrfs_writepage_fixup *fixup;
2044 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002045 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002046 struct page *page;
2047 struct inode *inode;
2048 u64 page_start;
2049 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002050 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002051
2052 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2053 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002054again:
Chris Mason247e7432008-07-17 12:53:51 -04002055 lock_page(page);
2056 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2057 ClearPageChecked(page);
2058 goto out_page;
2059 }
2060
2061 inode = page->mapping->host;
2062 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002063 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002064
David Sterbaff13db42015-12-03 14:30:40 +01002065 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002066 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002067
2068 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002069 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002070 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002071
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002072 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002074 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002075 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2076 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002077 unlock_page(page);
2078 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002079 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002080 goto again;
2081 }
Chris Mason247e7432008-07-17 12:53:51 -04002082
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002083 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002084 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002085 if (ret) {
2086 mapping_set_error(page->mapping, ret);
2087 end_extent_writepage(page, ret, page_start, page_end);
2088 ClearPageChecked(page);
2089 goto out;
2090 }
2091
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002092 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2093 0);
Chris Mason247e7432008-07-17 12:53:51 -04002094 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002096out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2098 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002099out_page:
2100 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002102 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002103}
2104
2105/*
2106 * There are a few paths in the higher layers of the kernel that directly
2107 * set the page dirty bit without asking the filesystem if it is a
2108 * good idea. This causes problems because we want to make sure COW
2109 * properly happens and the data=ordered rules are followed.
2110 *
Chris Masonc8b97812008-10-29 14:49:59 -04002111 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002112 * hasn't been properly setup for IO. We kick off an async process
2113 * to fix it up. The async helper will wait for ordered extents, set
2114 * the delalloc bit and make it safe to write the page.
2115 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002116static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002117{
2118 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002120 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002121
Chris Mason8b62b722009-09-02 16:53:46 -04002122 /* this page is properly in the ordered list */
2123 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002124 return 0;
2125
2126 if (PageChecked(page))
2127 return -EAGAIN;
2128
2129 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2130 if (!fixup)
2131 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002132
Chris Mason247e7432008-07-17 12:53:51 -04002133 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002134 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002135 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2136 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002137 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002138 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002139 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002140}
2141
Yan Zhengd899e052008-10-30 14:25:28 -04002142static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2143 struct inode *inode, u64 file_pos,
2144 u64 disk_bytenr, u64 disk_num_bytes,
2145 u64 num_bytes, u64 ram_bytes,
2146 u8 compression, u8 encryption,
2147 u16 other_encoding, int extent_type)
2148{
2149 struct btrfs_root *root = BTRFS_I(inode)->root;
2150 struct btrfs_file_extent_item *fi;
2151 struct btrfs_path *path;
2152 struct extent_buffer *leaf;
2153 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002154 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002155 int ret;
2156
2157 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002158 if (!path)
2159 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002160
Chris Masona1ed8352009-09-11 12:27:37 -04002161 /*
2162 * we may be replacing one extent in the tree with another.
2163 * The new extent is pinned in the extent map, and we don't want
2164 * to drop it from the cache until it is completely in the btree.
2165 *
2166 * So, tell btrfs_drop_extents to leave this extent in the cache.
2167 * the caller is expected to unpin it and allow it to be merged
2168 * with the others.
2169 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002170 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2171 file_pos + num_bytes, NULL, 0,
2172 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002173 if (ret)
2174 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002175
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002176 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002177 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002178 ins.offset = file_pos;
2179 ins.type = BTRFS_EXTENT_DATA_KEY;
2180
2181 path->leave_spinning = 1;
2182 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2183 sizeof(*fi));
2184 if (ret)
2185 goto out;
2186 }
Yan Zhengd899e052008-10-30 14:25:28 -04002187 leaf = path->nodes[0];
2188 fi = btrfs_item_ptr(leaf, path->slots[0],
2189 struct btrfs_file_extent_item);
2190 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2191 btrfs_set_file_extent_type(leaf, fi, extent_type);
2192 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2193 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2194 btrfs_set_file_extent_offset(leaf, fi, 0);
2195 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2196 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2197 btrfs_set_file_extent_compression(leaf, fi, compression);
2198 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2199 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002200
Yan Zhengd899e052008-10-30 14:25:28 -04002201 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002202 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002203
2204 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002205
2206 ins.objectid = disk_bytenr;
2207 ins.offset = disk_num_bytes;
2208 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002209 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002210 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002211 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002212 * Release the reserved range from inode dirty range map, as it is
2213 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002214 */
2215 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002216out:
Yan Zhengd899e052008-10-30 14:25:28 -04002217 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002218
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002219 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002220}
2221
Liu Bo38c227d2013-01-29 03:18:40 +00002222/* snapshot-aware defrag */
2223struct sa_defrag_extent_backref {
2224 struct rb_node node;
2225 struct old_sa_defrag_extent *old;
2226 u64 root_id;
2227 u64 inum;
2228 u64 file_pos;
2229 u64 extent_offset;
2230 u64 num_bytes;
2231 u64 generation;
2232};
2233
2234struct old_sa_defrag_extent {
2235 struct list_head list;
2236 struct new_sa_defrag_extent *new;
2237
2238 u64 extent_offset;
2239 u64 bytenr;
2240 u64 offset;
2241 u64 len;
2242 int count;
2243};
2244
2245struct new_sa_defrag_extent {
2246 struct rb_root root;
2247 struct list_head head;
2248 struct btrfs_path *path;
2249 struct inode *inode;
2250 u64 file_pos;
2251 u64 len;
2252 u64 bytenr;
2253 u64 disk_len;
2254 u8 compress_type;
2255};
2256
2257static int backref_comp(struct sa_defrag_extent_backref *b1,
2258 struct sa_defrag_extent_backref *b2)
2259{
2260 if (b1->root_id < b2->root_id)
2261 return -1;
2262 else if (b1->root_id > b2->root_id)
2263 return 1;
2264
2265 if (b1->inum < b2->inum)
2266 return -1;
2267 else if (b1->inum > b2->inum)
2268 return 1;
2269
2270 if (b1->file_pos < b2->file_pos)
2271 return -1;
2272 else if (b1->file_pos > b2->file_pos)
2273 return 1;
2274
2275 /*
2276 * [------------------------------] ===> (a range of space)
2277 * |<--->| |<---->| =============> (fs/file tree A)
2278 * |<---------------------------->| ===> (fs/file tree B)
2279 *
2280 * A range of space can refer to two file extents in one tree while
2281 * refer to only one file extent in another tree.
2282 *
2283 * So we may process a disk offset more than one time(two extents in A)
2284 * and locate at the same extent(one extent in B), then insert two same
2285 * backrefs(both refer to the extent in B).
2286 */
2287 return 0;
2288}
2289
2290static void backref_insert(struct rb_root *root,
2291 struct sa_defrag_extent_backref *backref)
2292{
2293 struct rb_node **p = &root->rb_node;
2294 struct rb_node *parent = NULL;
2295 struct sa_defrag_extent_backref *entry;
2296 int ret;
2297
2298 while (*p) {
2299 parent = *p;
2300 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2301
2302 ret = backref_comp(backref, entry);
2303 if (ret < 0)
2304 p = &(*p)->rb_left;
2305 else
2306 p = &(*p)->rb_right;
2307 }
2308
2309 rb_link_node(&backref->node, parent, p);
2310 rb_insert_color(&backref->node, root);
2311}
2312
2313/*
2314 * Note the backref might has changed, and in this case we just return 0.
2315 */
2316static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2317 void *ctx)
2318{
2319 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002320 struct old_sa_defrag_extent *old = ctx;
2321 struct new_sa_defrag_extent *new = old->new;
2322 struct btrfs_path *path = new->path;
2323 struct btrfs_key key;
2324 struct btrfs_root *root;
2325 struct sa_defrag_extent_backref *backref;
2326 struct extent_buffer *leaf;
2327 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002329 int slot;
2330 int ret;
2331 u64 extent_offset;
2332 u64 num_bytes;
2333
2334 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002335 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002336 return 0;
2337
2338 key.objectid = root_id;
2339 key.type = BTRFS_ROOT_ITEM_KEY;
2340 key.offset = (u64)-1;
2341
Liu Bo38c227d2013-01-29 03:18:40 +00002342 root = btrfs_read_fs_root_no_name(fs_info, &key);
2343 if (IS_ERR(root)) {
2344 if (PTR_ERR(root) == -ENOENT)
2345 return 0;
2346 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002347 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002348 inum, offset, root_id);
2349 return PTR_ERR(root);
2350 }
2351
2352 key.objectid = inum;
2353 key.type = BTRFS_EXTENT_DATA_KEY;
2354 if (offset > (u64)-1 << 32)
2355 key.offset = 0;
2356 else
2357 key.offset = offset;
2358
2359 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302360 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002361 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002362 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002363
2364 while (1) {
2365 cond_resched();
2366
2367 leaf = path->nodes[0];
2368 slot = path->slots[0];
2369
2370 if (slot >= btrfs_header_nritems(leaf)) {
2371 ret = btrfs_next_leaf(root, path);
2372 if (ret < 0) {
2373 goto out;
2374 } else if (ret > 0) {
2375 ret = 0;
2376 goto out;
2377 }
2378 continue;
2379 }
2380
2381 path->slots[0]++;
2382
2383 btrfs_item_key_to_cpu(leaf, &key, slot);
2384
2385 if (key.objectid > inum)
2386 goto out;
2387
2388 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2389 continue;
2390
2391 extent = btrfs_item_ptr(leaf, slot,
2392 struct btrfs_file_extent_item);
2393
2394 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2395 continue;
2396
Liu Boe68afa42013-07-01 22:13:26 +08002397 /*
2398 * 'offset' refers to the exact key.offset,
2399 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2400 * (key.offset - extent_offset).
2401 */
2402 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002403 continue;
2404
Liu Boe68afa42013-07-01 22:13:26 +08002405 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002406 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002407
Liu Bo38c227d2013-01-29 03:18:40 +00002408 if (extent_offset >= old->extent_offset + old->offset +
2409 old->len || extent_offset + num_bytes <=
2410 old->extent_offset + old->offset)
2411 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002412 break;
2413 }
2414
2415 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2416 if (!backref) {
2417 ret = -ENOENT;
2418 goto out;
2419 }
2420
2421 backref->root_id = root_id;
2422 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002423 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002424 backref->num_bytes = num_bytes;
2425 backref->extent_offset = extent_offset;
2426 backref->generation = btrfs_file_extent_generation(leaf, extent);
2427 backref->old = old;
2428 backref_insert(&new->root, backref);
2429 old->count++;
2430out:
2431 btrfs_release_path(path);
2432 WARN_ON(ret);
2433 return ret;
2434}
2435
2436static noinline bool record_extent_backrefs(struct btrfs_path *path,
2437 struct new_sa_defrag_extent *new)
2438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002439 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002440 struct old_sa_defrag_extent *old, *tmp;
2441 int ret;
2442
2443 new->path = path;
2444
2445 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002446 ret = iterate_inodes_from_logical(old->bytenr +
2447 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002448 path, record_one_backref,
2449 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002450 if (ret < 0 && ret != -ENOENT)
2451 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002452
2453 /* no backref to be processed for this extent */
2454 if (!old->count) {
2455 list_del(&old->list);
2456 kfree(old);
2457 }
2458 }
2459
2460 if (list_empty(&new->head))
2461 return false;
2462
2463 return true;
2464}
2465
2466static int relink_is_mergable(struct extent_buffer *leaf,
2467 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002468 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002469{
Liu Bo116e0022013-08-02 16:30:40 +08002470 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002471 return 0;
2472
2473 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2474 return 0;
2475
Liu Bo116e0022013-08-02 16:30:40 +08002476 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2477 return 0;
2478
2479 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002480 btrfs_file_extent_other_encoding(leaf, fi))
2481 return 0;
2482
2483 return 1;
2484}
2485
2486/*
2487 * Note the backref might has changed, and in this case we just return 0.
2488 */
2489static noinline int relink_extent_backref(struct btrfs_path *path,
2490 struct sa_defrag_extent_backref *prev,
2491 struct sa_defrag_extent_backref *backref)
2492{
2493 struct btrfs_file_extent_item *extent;
2494 struct btrfs_file_extent_item *item;
2495 struct btrfs_ordered_extent *ordered;
2496 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002497 struct btrfs_root *root;
2498 struct btrfs_key key;
2499 struct extent_buffer *leaf;
2500 struct old_sa_defrag_extent *old = backref->old;
2501 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002503 struct inode *inode;
2504 struct extent_state *cached = NULL;
2505 int ret = 0;
2506 u64 start;
2507 u64 len;
2508 u64 lock_start;
2509 u64 lock_end;
2510 bool merge = false;
2511 int index;
2512
2513 if (prev && prev->root_id == backref->root_id &&
2514 prev->inum == backref->inum &&
2515 prev->file_pos + prev->num_bytes == backref->file_pos)
2516 merge = true;
2517
2518 /* step 1: get root */
2519 key.objectid = backref->root_id;
2520 key.type = BTRFS_ROOT_ITEM_KEY;
2521 key.offset = (u64)-1;
2522
Liu Bo38c227d2013-01-29 03:18:40 +00002523 index = srcu_read_lock(&fs_info->subvol_srcu);
2524
2525 root = btrfs_read_fs_root_no_name(fs_info, &key);
2526 if (IS_ERR(root)) {
2527 srcu_read_unlock(&fs_info->subvol_srcu, index);
2528 if (PTR_ERR(root) == -ENOENT)
2529 return 0;
2530 return PTR_ERR(root);
2531 }
Liu Bo38c227d2013-01-29 03:18:40 +00002532
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002533 if (btrfs_root_readonly(root)) {
2534 srcu_read_unlock(&fs_info->subvol_srcu, index);
2535 return 0;
2536 }
2537
Liu Bo38c227d2013-01-29 03:18:40 +00002538 /* step 2: get inode */
2539 key.objectid = backref->inum;
2540 key.type = BTRFS_INODE_ITEM_KEY;
2541 key.offset = 0;
2542
2543 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2544 if (IS_ERR(inode)) {
2545 srcu_read_unlock(&fs_info->subvol_srcu, index);
2546 return 0;
2547 }
2548
2549 srcu_read_unlock(&fs_info->subvol_srcu, index);
2550
2551 /* step 3: relink backref */
2552 lock_start = backref->file_pos;
2553 lock_end = backref->file_pos + backref->num_bytes - 1;
2554 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002555 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002556
2557 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2558 if (ordered) {
2559 btrfs_put_ordered_extent(ordered);
2560 goto out_unlock;
2561 }
2562
2563 trans = btrfs_join_transaction(root);
2564 if (IS_ERR(trans)) {
2565 ret = PTR_ERR(trans);
2566 goto out_unlock;
2567 }
2568
2569 key.objectid = backref->inum;
2570 key.type = BTRFS_EXTENT_DATA_KEY;
2571 key.offset = backref->file_pos;
2572
2573 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2574 if (ret < 0) {
2575 goto out_free_path;
2576 } else if (ret > 0) {
2577 ret = 0;
2578 goto out_free_path;
2579 }
2580
2581 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2582 struct btrfs_file_extent_item);
2583
2584 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2585 backref->generation)
2586 goto out_free_path;
2587
2588 btrfs_release_path(path);
2589
2590 start = backref->file_pos;
2591 if (backref->extent_offset < old->extent_offset + old->offset)
2592 start += old->extent_offset + old->offset -
2593 backref->extent_offset;
2594
2595 len = min(backref->extent_offset + backref->num_bytes,
2596 old->extent_offset + old->offset + old->len);
2597 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2598
2599 ret = btrfs_drop_extents(trans, root, inode, start,
2600 start + len, 1);
2601 if (ret)
2602 goto out_free_path;
2603again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002604 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002605 key.type = BTRFS_EXTENT_DATA_KEY;
2606 key.offset = start;
2607
Liu Boa09a0a72013-03-11 09:20:58 +00002608 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 if (merge) {
2610 struct btrfs_file_extent_item *fi;
2611 u64 extent_len;
2612 struct btrfs_key found_key;
2613
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002614 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002615 if (ret < 0)
2616 goto out_free_path;
2617
2618 path->slots[0]--;
2619 leaf = path->nodes[0];
2620 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2621
2622 fi = btrfs_item_ptr(leaf, path->slots[0],
2623 struct btrfs_file_extent_item);
2624 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2625
Liu Bo116e0022013-08-02 16:30:40 +08002626 if (extent_len + found_key.offset == start &&
2627 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002628 btrfs_set_file_extent_num_bytes(leaf, fi,
2629 extent_len + len);
2630 btrfs_mark_buffer_dirty(leaf);
2631 inode_add_bytes(inode, len);
2632
2633 ret = 1;
2634 goto out_free_path;
2635 } else {
2636 merge = false;
2637 btrfs_release_path(path);
2638 goto again;
2639 }
2640 }
2641
2642 ret = btrfs_insert_empty_item(trans, root, path, &key,
2643 sizeof(*extent));
2644 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002645 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002646 goto out_free_path;
2647 }
2648
2649 leaf = path->nodes[0];
2650 item = btrfs_item_ptr(leaf, path->slots[0],
2651 struct btrfs_file_extent_item);
2652 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2653 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2654 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2655 btrfs_set_file_extent_num_bytes(leaf, item, len);
2656 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2657 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2658 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2659 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2660 btrfs_set_file_extent_encryption(leaf, item, 0);
2661 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2662
2663 btrfs_mark_buffer_dirty(leaf);
2664 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002665 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002666
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002667 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002668 new->disk_len, 0,
2669 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002670 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002672 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002673 goto out_free_path;
2674 }
2675
2676 ret = 1;
2677out_free_path:
2678 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002679 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002680 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002681out_unlock:
2682 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2683 &cached, GFP_NOFS);
2684 iput(inode);
2685 return ret;
2686}
2687
Liu Bo6f519562013-10-29 10:45:05 +08002688static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2689{
2690 struct old_sa_defrag_extent *old, *tmp;
2691
2692 if (!new)
2693 return;
2694
2695 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002696 kfree(old);
2697 }
2698 kfree(new);
2699}
2700
Liu Bo38c227d2013-01-29 03:18:40 +00002701static void relink_file_extents(struct new_sa_defrag_extent *new)
2702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002703 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002704 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002705 struct sa_defrag_extent_backref *backref;
2706 struct sa_defrag_extent_backref *prev = NULL;
2707 struct inode *inode;
2708 struct btrfs_root *root;
2709 struct rb_node *node;
2710 int ret;
2711
2712 inode = new->inode;
2713 root = BTRFS_I(inode)->root;
2714
2715 path = btrfs_alloc_path();
2716 if (!path)
2717 return;
2718
2719 if (!record_extent_backrefs(path, new)) {
2720 btrfs_free_path(path);
2721 goto out;
2722 }
2723 btrfs_release_path(path);
2724
2725 while (1) {
2726 node = rb_first(&new->root);
2727 if (!node)
2728 break;
2729 rb_erase(node, &new->root);
2730
2731 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2732
2733 ret = relink_extent_backref(path, prev, backref);
2734 WARN_ON(ret < 0);
2735
2736 kfree(prev);
2737
2738 if (ret == 1)
2739 prev = backref;
2740 else
2741 prev = NULL;
2742 cond_resched();
2743 }
2744 kfree(prev);
2745
2746 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002747out:
Liu Bo6f519562013-10-29 10:45:05 +08002748 free_sa_defrag_extent(new);
2749
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002750 atomic_dec(&fs_info->defrag_running);
2751 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002752}
2753
2754static struct new_sa_defrag_extent *
2755record_old_file_extents(struct inode *inode,
2756 struct btrfs_ordered_extent *ordered)
2757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002759 struct btrfs_root *root = BTRFS_I(inode)->root;
2760 struct btrfs_path *path;
2761 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002762 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002763 struct new_sa_defrag_extent *new;
2764 int ret;
2765
2766 new = kmalloc(sizeof(*new), GFP_NOFS);
2767 if (!new)
2768 return NULL;
2769
2770 new->inode = inode;
2771 new->file_pos = ordered->file_offset;
2772 new->len = ordered->len;
2773 new->bytenr = ordered->start;
2774 new->disk_len = ordered->disk_len;
2775 new->compress_type = ordered->compress_type;
2776 new->root = RB_ROOT;
2777 INIT_LIST_HEAD(&new->head);
2778
2779 path = btrfs_alloc_path();
2780 if (!path)
2781 goto out_kfree;
2782
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002783 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002784 key.type = BTRFS_EXTENT_DATA_KEY;
2785 key.offset = new->file_pos;
2786
2787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2788 if (ret < 0)
2789 goto out_free_path;
2790 if (ret > 0 && path->slots[0] > 0)
2791 path->slots[0]--;
2792
2793 /* find out all the old extents for the file range */
2794 while (1) {
2795 struct btrfs_file_extent_item *extent;
2796 struct extent_buffer *l;
2797 int slot;
2798 u64 num_bytes;
2799 u64 offset;
2800 u64 end;
2801 u64 disk_bytenr;
2802 u64 extent_offset;
2803
2804 l = path->nodes[0];
2805 slot = path->slots[0];
2806
2807 if (slot >= btrfs_header_nritems(l)) {
2808 ret = btrfs_next_leaf(root, path);
2809 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002810 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002811 else if (ret > 0)
2812 break;
2813 continue;
2814 }
2815
2816 btrfs_item_key_to_cpu(l, &key, slot);
2817
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002818 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002819 break;
2820 if (key.type != BTRFS_EXTENT_DATA_KEY)
2821 break;
2822 if (key.offset >= new->file_pos + new->len)
2823 break;
2824
2825 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2826
2827 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2828 if (key.offset + num_bytes < new->file_pos)
2829 goto next;
2830
2831 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2832 if (!disk_bytenr)
2833 goto next;
2834
2835 extent_offset = btrfs_file_extent_offset(l, extent);
2836
2837 old = kmalloc(sizeof(*old), GFP_NOFS);
2838 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002839 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002840
2841 offset = max(new->file_pos, key.offset);
2842 end = min(new->file_pos + new->len, key.offset + num_bytes);
2843
2844 old->bytenr = disk_bytenr;
2845 old->extent_offset = extent_offset;
2846 old->offset = offset - key.offset;
2847 old->len = end - offset;
2848 old->new = new;
2849 old->count = 0;
2850 list_add_tail(&old->list, &new->head);
2851next:
2852 path->slots[0]++;
2853 cond_resched();
2854 }
2855
2856 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002857 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002858
2859 return new;
2860
Liu Bo38c227d2013-01-29 03:18:40 +00002861out_free_path:
2862 btrfs_free_path(path);
2863out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002864 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002865 return NULL;
2866}
2867
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002868static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002869 u64 start, u64 len)
2870{
2871 struct btrfs_block_group_cache *cache;
2872
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002873 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002874 ASSERT(cache);
2875
2876 spin_lock(&cache->lock);
2877 cache->delalloc_bytes -= len;
2878 spin_unlock(&cache->lock);
2879
2880 btrfs_put_block_group(cache);
2881}
2882
Chris Masond352ac62008-09-29 15:18:18 -04002883/* as ordered data IO finishes, this gets called so we can finish
2884 * an ordered extent if the range of bytes in the file it covers are
2885 * fully written.
2886 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002887static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002888{
Josef Bacik5fd02042012-05-02 14:00:54 -04002889 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002891 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002892 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002893 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002894 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002895 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002896 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002897 int ret = 0;
2898 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002899 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002900 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002901 bool range_locked = false;
2902 bool clear_new_delalloc_bytes = false;
2903
2904 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2905 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2906 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2907 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002909 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002910
Josef Bacik5fd02042012-05-02 14:00:54 -04002911 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2912 ret = -EIO;
2913 goto out;
2914 }
2915
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002916 btrfs_free_io_failure_record(BTRFS_I(inode),
2917 ordered_extent->file_offset,
2918 ordered_extent->file_offset +
2919 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2922 truncated = true;
2923 logical_len = ordered_extent->truncated_len;
2924 /* Truncated the entire extent, don't bother adding */
2925 if (!logical_len)
2926 goto out;
2927 }
2928
Yan, Zhengc2167752009-11-12 09:34:21 +00002929 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002931
2932 /*
2933 * For mwrite(mmap + memset to write) case, we still reserve
2934 * space for NOCOW range.
2935 * As NOCOW won't cause a new delayed ref, just free the space
2936 */
2937 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2938 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002939 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2940 if (nolock)
2941 trans = btrfs_join_transaction_nolock(root);
2942 else
2943 trans = btrfs_join_transaction(root);
2944 if (IS_ERR(trans)) {
2945 ret = PTR_ERR(trans);
2946 trans = NULL;
2947 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002948 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002949 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002950 ret = btrfs_update_inode_fallback(trans, root, inode);
2951 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002952 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002953 goto out;
2954 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002955
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002956 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002957 lock_extent_bits(io_tree, ordered_extent->file_offset,
2958 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002959 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960
Liu Bo38c227d2013-01-29 03:18:40 +00002961 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2962 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002963 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002964 if (ret) {
2965 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002966 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002967 /* the inode is shared */
2968 new = record_old_file_extents(inode, ordered_extent);
2969
2970 clear_extent_bit(io_tree, ordered_extent->file_offset,
2971 ordered_extent->file_offset + ordered_extent->len - 1,
2972 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2973 }
2974
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002976 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002977 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002978 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 if (IS_ERR(trans)) {
2980 ret = PTR_ERR(trans);
2981 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002982 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002983 }
Chris Masona79b7d42014-05-22 16:18:52 -07002984
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002985 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002986
Chris Masonc8b97812008-10-29 14:49:59 -04002987 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002988 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002989 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002990 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002991 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002992 ordered_extent->file_offset,
2993 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002994 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002995 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002996 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002997 ret = insert_reserved_file_extent(trans, inode,
2998 ordered_extent->file_offset,
2999 ordered_extent->start,
3000 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003001 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003002 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003003 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003004 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003005 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003006 ordered_extent->start,
3007 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003008 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003009 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3010 ordered_extent->file_offset, ordered_extent->len,
3011 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003013 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003014 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003015 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003016
David Sterbadf9f6282017-02-10 19:35:37 +01003017 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003018
Josef Bacik6c760c02012-11-09 10:53:21 -05003019 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3020 ret = btrfs_update_inode_fallback(trans, root, inode);
3021 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003022 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003023 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003024 }
3025 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003026out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003027 if (range_locked || clear_new_delalloc_bytes) {
3028 unsigned int clear_bits = 0;
3029
3030 if (range_locked)
3031 clear_bits |= EXTENT_LOCKED;
3032 if (clear_new_delalloc_bytes)
3033 clear_bits |= EXTENT_DELALLOC_NEW;
3034 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3035 ordered_extent->file_offset,
3036 ordered_extent->file_offset +
3037 ordered_extent->len - 1,
3038 clear_bits,
3039 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3040 0, &cached_state, GFP_NOFS);
3041 }
3042
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003043 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003044 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3045 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003046 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003047 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003048
Josef Bacik77cef2e2013-08-29 13:57:21 -04003049 if (ret || truncated) {
3050 u64 start, end;
3051
3052 if (truncated)
3053 start = ordered_extent->file_offset + logical_len;
3054 else
3055 start = ordered_extent->file_offset;
3056 end = ordered_extent->file_offset + ordered_extent->len - 1;
3057 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3058
3059 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003060 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003061
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003062 /*
3063 * If the ordered extent had an IOERR or something else went
3064 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003065 * back to the allocator. We only free the extent in the
3066 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003067 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003068 if ((ret || !logical_len) &&
3069 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003070 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003071 btrfs_free_reserved_extent(fs_info,
3072 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003073 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003074 }
3075
3076
Josef Bacik5fd02042012-05-02 14:00:54 -04003077 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003078 * This needs to be done to make sure anybody waiting knows we are done
3079 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003080 */
3081 btrfs_remove_ordered_extent(inode, ordered_extent);
3082
Liu Bo38c227d2013-01-29 03:18:40 +00003083 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003084 if (new) {
3085 if (ret) {
3086 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003087 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003088 } else {
3089 relink_file_extents(new);
3090 }
3091 }
Liu Bo38c227d2013-01-29 03:18:40 +00003092
Chris Masone6dcd2d2008-07-17 12:53:50 -04003093 /* once for us */
3094 btrfs_put_ordered_extent(ordered_extent);
3095 /* once for the tree */
3096 btrfs_put_ordered_extent(ordered_extent);
3097
Josef Bacik5fd02042012-05-02 14:00:54 -04003098 return ret;
3099}
3100
3101static void finish_ordered_fn(struct btrfs_work *work)
3102{
3103 struct btrfs_ordered_extent *ordered_extent;
3104 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3105 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003106}
3107
David Sterbac3988d62017-02-17 15:18:32 +01003108static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003109 struct extent_state *state, int uptodate)
3110{
Josef Bacik5fd02042012-05-02 14:00:54 -04003111 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003112 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003113 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003114 struct btrfs_workqueue *wq;
3115 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003116
liubo1abe9b82011-03-24 11:18:59 +00003117 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3118
Chris Mason8b62b722009-09-02 16:53:46 -04003119 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003120 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3121 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003122 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003123
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003124 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003126 func = btrfs_freespace_write_helper;
3127 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003129 func = btrfs_endio_write_helper;
3130 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003131
Liu Bo9e0af232014-08-15 23:36:53 +08003132 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3133 NULL);
3134 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003135}
3136
Miao Xiedc380ae2014-09-12 18:43:55 +08003137static int __readpage_endio_check(struct inode *inode,
3138 struct btrfs_io_bio *io_bio,
3139 int icsum, struct page *page,
3140 int pgoff, u64 start, size_t len)
3141{
3142 char *kaddr;
3143 u32 csum_expected;
3144 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003145
3146 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3147
3148 kaddr = kmap_atomic(page);
3149 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003150 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003151 if (csum != csum_expected)
3152 goto zeroit;
3153
3154 kunmap_atomic(kaddr);
3155 return 0;
3156zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003157 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003158 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003159 memset(kaddr + pgoff, 1, len);
3160 flush_dcache_page(page);
3161 kunmap_atomic(kaddr);
3162 if (csum_expected == 0)
3163 return 0;
3164 return -EIO;
3165}
3166
Chris Masond352ac62008-09-29 15:18:18 -04003167/*
Chris Masond352ac62008-09-29 15:18:18 -04003168 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003169 * if there's a match, we allow the bio to finish. If not, the code in
3170 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003171 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003172static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3173 u64 phy_offset, struct page *page,
3174 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003175{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003176 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003177 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003180
Chris Masond20f7042008-12-08 16:58:54 -05003181 if (PageChecked(page)) {
3182 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003183 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003184 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003185
3186 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003187 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003188
Yan Zheng17d217f2008-12-12 10:03:38 -05003189 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003190 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003191 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003192 return 0;
3193 }
3194
Miao Xiefacc8a222013-07-25 19:22:34 +08003195 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003196 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3197 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003198}
Chris Masonb888db22007-08-27 16:49:44 -04003199
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003200void btrfs_add_delayed_iput(struct inode *inode)
3201{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003203 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003204
3205 if (atomic_add_unless(&inode->i_count, -1, 1))
3206 return;
3207
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003208 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003209 if (binode->delayed_iput_count == 0) {
3210 ASSERT(list_empty(&binode->delayed_iput));
3211 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3212 } else {
3213 binode->delayed_iput_count++;
3214 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003215 spin_unlock(&fs_info->delayed_iput_lock);
3216}
3217
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003218void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003220
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003221 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003222 while (!list_empty(&fs_info->delayed_iputs)) {
3223 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224
David Sterba8089fe62015-11-19 14:15:51 +01003225 inode = list_first_entry(&fs_info->delayed_iputs,
3226 struct btrfs_inode, delayed_iput);
3227 if (inode->delayed_iput_count) {
3228 inode->delayed_iput_count--;
3229 list_move_tail(&inode->delayed_iput,
3230 &fs_info->delayed_iputs);
3231 } else {
3232 list_del_init(&inode->delayed_iput);
3233 }
3234 spin_unlock(&fs_info->delayed_iput_lock);
3235 iput(&inode->vfs_inode);
3236 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003237 }
David Sterba8089fe62015-11-19 14:15:51 +01003238 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003239}
3240
Yan, Zhengd68fc572010-05-16 10:49:58 -04003241/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003242 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 * files in the subvolume, it removes orphan item and frees block_rsv
3244 * structure.
3245 */
3246void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3247 struct btrfs_root *root)
3248{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003249 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003250 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 int ret;
3252
Josef Bacik8a35d952012-05-23 14:26:42 -04003253 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3255 return;
3256
Josef Bacik90290e12011-12-02 15:44:12 -05003257 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003258 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003259 spin_unlock(&root->orphan_lock);
3260 return;
3261 }
3262
3263 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3264 spin_unlock(&root->orphan_lock);
3265 return;
3266 }
3267
3268 block_rsv = root->orphan_block_rsv;
3269 root->orphan_block_rsv = NULL;
3270 spin_unlock(&root->orphan_lock);
3271
Miao Xie27cdeb72014-04-02 19:51:05 +08003272 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003274 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003276 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003277 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003278 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003279 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3280 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 }
3282
Josef Bacik90290e12011-12-02 15:44:12 -05003283 if (block_rsv) {
3284 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003285 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 }
3287}
3288
3289/*
Josef Bacik7b128762008-07-24 12:17:14 -04003290 * This creates an orphan entry for the given inode in case something goes
3291 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 *
3293 * NOTE: caller of this function should reserve 5 units of metadata for
3294 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003295 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003296int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3297 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003298{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003299 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3300 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 struct btrfs_block_rsv *block_rsv = NULL;
3302 int reserve = 0;
3303 int insert = 0;
3304 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003307 block_rsv = btrfs_alloc_block_rsv(fs_info,
3308 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003309 if (!block_rsv)
3310 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003311 }
3312
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 spin_lock(&root->orphan_lock);
3314 if (!root->orphan_block_rsv) {
3315 root->orphan_block_rsv = block_rsv;
3316 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003317 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 block_rsv = NULL;
3319 }
Josef Bacik7b128762008-07-24 12:17:14 -04003320
Josef Bacik8a35d952012-05-23 14:26:42 -04003321 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003322 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003323#if 0
3324 /*
3325 * For proper ENOSPC handling, we should do orphan
3326 * cleanup when mounting. But this introduces backward
3327 * compatibility issue.
3328 */
3329 if (!xchg(&root->orphan_item_inserted, 1))
3330 insert = 2;
3331 else
3332 insert = 1;
3333#endif
3334 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003335 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 }
Josef Bacik7b128762008-07-24 12:17:14 -04003337
Josef Bacik72ac3c02012-05-23 14:13:11 -04003338 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003339 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003342
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 /* grab metadata reservation from transaction handle */
3344 if (reserve) {
3345 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003346 ASSERT(!ret);
3347 if (ret) {
3348 atomic_dec(&root->orphan_inodes);
3349 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003350 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003351 if (insert)
3352 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003353 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003354 return ret;
3355 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 }
3357
3358 /* insert an orphan item to track this unlinked/truncated file */
3359 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003360 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003361 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003362 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003363 if (reserve) {
3364 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003365 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003366 btrfs_orphan_release_metadata(inode);
3367 }
3368 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003369 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003370 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003371 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003372 return ret;
3373 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003374 }
3375 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003376 }
3377
3378 /* insert an orphan item to track subvolume contains orphan files */
3379 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003380 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003381 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003382 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003383 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003384 return ret;
3385 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386 }
3387 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003388}
3389
3390/*
3391 * We have done the truncate/delete so we can go ahead and remove the orphan
3392 * item for this particular inode.
3393 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003394static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003395 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003396{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003397 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 int delete_item = 0;
3399 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003400 int ret = 0;
3401
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003403 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003404 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003405 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003406
Josef Bacik72ac3c02012-05-23 14:13:11 -04003407 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003408 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003410 spin_unlock(&root->orphan_lock);
3411
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003412 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003413 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003414 if (trans)
3415 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003416 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003417 }
Josef Bacik7b128762008-07-24 12:17:14 -04003418
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003419 if (release_rsv)
3420 btrfs_orphan_release_metadata(inode);
3421
Josef Bacik4ef31a42013-08-13 14:10:08 -04003422 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003423}
3424
3425/*
3426 * this cleans up any orphans that may be left on the list from the last use
3427 * of this root.
3428 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003429int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003431 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003432 struct btrfs_path *path;
3433 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003434 struct btrfs_key key, found_key;
3435 struct btrfs_trans_handle *trans;
3436 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003437 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003438 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3439
Yan, Zhengd68fc572010-05-16 10:49:58 -04003440 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003441 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003442
3443 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444 if (!path) {
3445 ret = -ENOMEM;
3446 goto out;
3447 }
David Sterbae4058b52015-11-27 16:31:35 +01003448 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003449
3450 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003451 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003452 key.offset = (u64)-1;
3453
Josef Bacik7b128762008-07-24 12:17:14 -04003454 while (1) {
3455 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003456 if (ret < 0)
3457 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003458
3459 /*
3460 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003461 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003462 * find the key and see if we have stuff that matches
3463 */
3464 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003465 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 if (path->slots[0] == 0)
3467 break;
3468 path->slots[0]--;
3469 }
3470
3471 /* pull out the item */
3472 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003473 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3474
3475 /* make sure the item matches what we want */
3476 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3477 break;
David Sterba962a2982014-06-04 18:41:45 +02003478 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003479 break;
3480
3481 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003482 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003483
3484 /*
3485 * this is where we are basically btrfs_lookup, without the
3486 * crossing root thing. we store the inode number in the
3487 * offset of the orphan item.
3488 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003489
3490 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003491 btrfs_err(fs_info,
3492 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003493 ret = -EINVAL;
3494 goto out;
3495 }
3496
3497 last_objectid = found_key.offset;
3498
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003499 found_key.objectid = found_key.offset;
3500 found_key.type = BTRFS_INODE_ITEM_KEY;
3501 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003502 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303503 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003504 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003505 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003506
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003508 struct btrfs_root *dead_root;
3509 struct btrfs_fs_info *fs_info = root->fs_info;
3510 int is_dead_root = 0;
3511
3512 /*
3513 * this is an orphan in the tree root. Currently these
3514 * could come from 2 sources:
3515 * a) a snapshot deletion in progress
3516 * b) a free space cache inode
3517 * We need to distinguish those two, as the snapshot
3518 * orphan must not get deleted.
3519 * find_dead_roots already ran before us, so if this
3520 * is a snapshot deletion, we should find the root
3521 * in the dead_roots list
3522 */
3523 spin_lock(&fs_info->trans_lock);
3524 list_for_each_entry(dead_root, &fs_info->dead_roots,
3525 root_list) {
3526 if (dead_root->root_key.objectid ==
3527 found_key.objectid) {
3528 is_dead_root = 1;
3529 break;
3530 }
3531 }
3532 spin_unlock(&fs_info->trans_lock);
3533 if (is_dead_root) {
3534 /* prevent this orphan from being found again */
3535 key.offset = found_key.objectid - 1;
3536 continue;
3537 }
3538 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003539 /*
3540 * Inode is already gone but the orphan item is still there,
3541 * kill the orphan item.
3542 */
Filipe Manana67710892016-06-06 11:51:25 +01003543 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003544 trans = btrfs_start_transaction(root, 1);
3545 if (IS_ERR(trans)) {
3546 ret = PTR_ERR(trans);
3547 goto out;
3548 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 btrfs_debug(fs_info, "auto deleting %Lu",
3550 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003551 ret = btrfs_del_orphan_item(trans, root,
3552 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003553 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003554 if (ret)
3555 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003556 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003557 }
Josef Bacik7b128762008-07-24 12:17:14 -04003558
Josef Bacik7b128762008-07-24 12:17:14 -04003559 /*
3560 * add this inode to the orphan list so btrfs_orphan_del does
3561 * the proper thing when we hit it
3562 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003563 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3564 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003565 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003566
Josef Bacik7b128762008-07-24 12:17:14 -04003567 /* if we have links, this was a truncate, lets do that */
3568 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303569 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003570 iput(inode);
3571 continue;
3572 }
Josef Bacik7b128762008-07-24 12:17:14 -04003573 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003574
3575 /* 1 for the orphan item deletion. */
3576 trans = btrfs_start_transaction(root, 1);
3577 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003578 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003579 ret = PTR_ERR(trans);
3580 goto out;
3581 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003582 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003583 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003584 if (ret) {
3585 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003586 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003587 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003588
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003589 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003590 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003591 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003592 } else {
3593 nr_unlink++;
3594 }
3595
3596 /* this will do delete_inode and everything for us */
3597 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003598 if (ret)
3599 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003600 }
Miao Xie3254c872011-11-10 20:45:05 -05003601 /* release the path since we're done with it */
3602 btrfs_release_path(path);
3603
Yan, Zhengd68fc572010-05-16 10:49:58 -04003604 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3605
3606 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003607 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003608 (u64)-1);
3609
Miao Xie27cdeb72014-04-02 19:51:05 +08003610 if (root->orphan_block_rsv ||
3611 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003612 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003614 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003615 }
Josef Bacik7b128762008-07-24 12:17:14 -04003616
3617 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003619 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003620 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003621
3622out:
3623 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003625 btrfs_free_path(path);
3626 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003627}
3628
Chris Masond352ac62008-09-29 15:18:18 -04003629/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003630 * very simple check to peek ahead in the leaf looking for xattrs. If we
3631 * don't find any xattrs, we know there can't be any acls.
3632 *
3633 * slot is the slot the inode is in, objectid is the objectid of the inode
3634 */
3635static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003636 int slot, u64 objectid,
3637 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003638{
3639 u32 nritems = btrfs_header_nritems(leaf);
3640 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003641 static u64 xattr_access = 0;
3642 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003643 int scanned = 0;
3644
Josef Bacikf23b5a52013-06-19 10:16:26 -04003645 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003646 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3647 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3648 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3649 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003650 }
3651
Chris Mason46a53cc2009-04-27 11:47:50 -04003652 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003653 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003654 while (slot < nritems) {
3655 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3656
3657 /* we found a different objectid, there must not be acls */
3658 if (found_key.objectid != objectid)
3659 return 0;
3660
3661 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003662 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003663 if (*first_xattr_slot == -1)
3664 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003665 if (found_key.offset == xattr_access ||
3666 found_key.offset == xattr_default)
3667 return 1;
3668 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003669
3670 /*
3671 * we found a key greater than an xattr key, there can't
3672 * be any acls later on
3673 */
3674 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3675 return 0;
3676
3677 slot++;
3678 scanned++;
3679
3680 /*
3681 * it goes inode, inode backrefs, xattrs, extents,
3682 * so if there are a ton of hard links to an inode there can
3683 * be a lot of backrefs. Don't waste time searching too hard,
3684 * this is just an optimization
3685 */
3686 if (scanned >= 8)
3687 break;
3688 }
3689 /* we hit the end of the leaf before we found an xattr or
3690 * something larger than an xattr. We have to assume the inode
3691 * has acls
3692 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003693 if (*first_xattr_slot == -1)
3694 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003695 return 1;
3696}
3697
3698/*
Chris Masond352ac62008-09-29 15:18:18 -04003699 * read an inode from the btree into the in-memory inode
3700 */
Filipe Manana67710892016-06-06 11:51:25 +01003701static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003703 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003704 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003705 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003706 struct btrfs_inode_item *inode_item;
3707 struct btrfs_root *root = BTRFS_I(inode)->root;
3708 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003709 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003711 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003712 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003713 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003714 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003715
3716 ret = btrfs_fill_inode(inode, &rdev);
3717 if (!ret)
3718 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003719
3720 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003721 if (!path) {
3722 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003723 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003724 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003725
Chris Mason39279cc2007-06-12 06:35:45 -04003726 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003727
Chris Mason39279cc2007-06-12 06:35:45 -04003728 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003729 if (ret) {
3730 if (ret > 0)
3731 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003732 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003733 }
Chris Mason39279cc2007-06-12 06:35:45 -04003734
Chris Mason5f39d392007-10-15 16:14:19 -04003735 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003736
3737 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003738 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003739
Chris Mason5f39d392007-10-15 16:14:19 -04003740 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3741 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003742 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003743 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003744 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3745 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003746 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003747
David Sterbaa937b972014-12-12 17:39:12 +01003748 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3749 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
David Sterbaa937b972014-12-12 17:39:12 +01003751 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3752 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
David Sterbaa937b972014-12-12 17:39:12 +01003754 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3755 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
chandan r9cc97d62012-07-04 12:48:07 +05303757 BTRFS_I(inode)->i_otime.tv_sec =
3758 btrfs_timespec_sec(leaf, &inode_item->otime);
3759 BTRFS_I(inode)->i_otime.tv_nsec =
3760 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003762 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003763 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003764 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3765
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003766 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003767 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003768 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003769 rdev = btrfs_inode_rdev(leaf, inode_item);
3770
Josef Bacikaec74772008-07-24 12:12:38 -04003771 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003772 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003773
3774cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003775 /*
3776 * If we were modified in the current generation and evicted from memory
3777 * and then re-read we need to do a full sync since we don't have any
3778 * idea about which extents were modified before we were evicted from
3779 * cache.
3780 *
3781 * This is required for both inode re-read from disk and delayed inode
3782 * in delayed_nodes_tree.
3783 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003784 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003785 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3786 &BTRFS_I(inode)->runtime_flags);
3787
Filipe Mananabde6c242015-07-24 00:00:19 +01003788 /*
3789 * We don't persist the id of the transaction where an unlink operation
3790 * against the inode was last made. So here we assume the inode might
3791 * have been evicted, and therefore the exact value of last_unlink_trans
3792 * lost, and set it to last_trans to avoid metadata inconsistencies
3793 * between the inode and its parent if the inode is fsync'ed and the log
3794 * replayed. For example, in the scenario:
3795 *
3796 * touch mydir/foo
3797 * ln mydir/foo mydir/bar
3798 * sync
3799 * unlink mydir/bar
3800 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3801 * xfs_io -c fsync mydir/foo
3802 * <power failure>
3803 * mount fs, triggers fsync log replay
3804 *
3805 * We must make sure that when we fsync our inode foo we also log its
3806 * parent inode, otherwise after log replay the parent still has the
3807 * dentry with the "bar" name but our inode foo has a link count of 1
3808 * and doesn't have an inode ref with the name "bar" anymore.
3809 *
3810 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003811 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003812 * transaction commits on fsync if our inode is a directory, or if our
3813 * inode is not a directory, logging its parent unnecessarily.
3814 */
3815 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3816
Miao Xie67de1172013-12-26 13:07:06 +08003817 path->slots[0]++;
3818 if (inode->i_nlink != 1 ||
3819 path->slots[0] >= btrfs_header_nritems(leaf))
3820 goto cache_acl;
3821
3822 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003823 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003824 goto cache_acl;
3825
3826 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3827 if (location.type == BTRFS_INODE_REF_KEY) {
3828 struct btrfs_inode_ref *ref;
3829
3830 ref = (struct btrfs_inode_ref *)ptr;
3831 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3832 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3833 struct btrfs_inode_extref *extref;
3834
3835 extref = (struct btrfs_inode_extref *)ptr;
3836 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3837 extref);
3838 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003839cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003840 /*
3841 * try to precache a NULL acl entry for files that don't have
3842 * any xattrs or acls
3843 */
Li Zefan33345d012011-04-20 10:31:50 +08003844 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003845 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003846 if (first_xattr_slot != -1) {
3847 path->slots[0] = first_xattr_slot;
3848 ret = btrfs_load_inode_props(inode, path);
3849 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003850 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003851 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003852 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003853 root->root_key.objectid, ret);
3854 }
3855 btrfs_free_path(path);
3856
Al Viro72c04902009-06-24 16:58:48 -04003857 if (!maybe_acls)
3858 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003859
Chris Mason39279cc2007-06-12 06:35:45 -04003860 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003861 case S_IFREG:
3862 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003863 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003864 inode->i_fop = &btrfs_file_operations;
3865 inode->i_op = &btrfs_file_inode_operations;
3866 break;
3867 case S_IFDIR:
3868 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003869 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 break;
3871 case S_IFLNK:
3872 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003873 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003874 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3875 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003876 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003877 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003878 init_special_inode(inode, inode->i_mode, rdev);
3879 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003880 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003881
3882 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003883 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003884
3885make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003886 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003887 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003888 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003889}
3890
Chris Masond352ac62008-09-29 15:18:18 -04003891/*
3892 * given a leaf and an inode, copy the inode fields into the leaf
3893 */
Chris Masone02119d2008-09-05 16:13:11 -04003894static void fill_inode_item(struct btrfs_trans_handle *trans,
3895 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003896 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003897 struct inode *inode)
3898{
Liu Bo51fab692012-12-27 09:01:21 +00003899 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003900
Liu Bo51fab692012-12-27 09:01:21 +00003901 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003902
Liu Bo51fab692012-12-27 09:01:21 +00003903 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3904 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3905 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3906 &token);
3907 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3908 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003909
David Sterbaa937b972014-12-12 17:39:12 +01003910 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003911 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003912 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003913 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003914
David Sterbaa937b972014-12-12 17:39:12 +01003915 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003916 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003917 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003918 inode->i_mtime.tv_nsec, &token);
3919
David Sterbaa937b972014-12-12 17:39:12 +01003920 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003921 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003922 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003923 inode->i_ctime.tv_nsec, &token);
3924
chandan r9cc97d62012-07-04 12:48:07 +05303925 btrfs_set_token_timespec_sec(leaf, &item->otime,
3926 BTRFS_I(inode)->i_otime.tv_sec, &token);
3927 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3928 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3929
Liu Bo51fab692012-12-27 09:01:21 +00003930 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3931 &token);
3932 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3933 &token);
3934 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3935 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3936 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3937 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3938 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003939}
3940
Chris Masond352ac62008-09-29 15:18:18 -04003941/*
3942 * copy everything in the in-memory inode into the btree.
3943 */
Chris Mason21151332011-11-10 20:39:08 -05003944static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003945 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003946{
3947 struct btrfs_inode_item *inode_item;
3948 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003949 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003950 int ret;
3951
3952 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003953 if (!path)
3954 return -ENOMEM;
3955
Chris Masonb9473432009-03-13 11:00:37 -04003956 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003957 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3958 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003959 if (ret) {
3960 if (ret > 0)
3961 ret = -ENOENT;
3962 goto failed;
3963 }
3964
Chris Mason5f39d392007-10-15 16:14:19 -04003965 leaf = path->nodes[0];
3966 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003967 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003968
Chris Masone02119d2008-09-05 16:13:11 -04003969 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003970 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003971 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003972 ret = 0;
3973failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003974 btrfs_free_path(path);
3975 return ret;
3976}
3977
Chris Masond352ac62008-09-29 15:18:18 -04003978/*
Chris Mason21151332011-11-10 20:39:08 -05003979 * copy everything in the in-memory inode into the btree.
3980 */
3981noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3982 struct btrfs_root *root, struct inode *inode)
3983{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003984 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003985 int ret;
3986
3987 /*
3988 * If the inode is a free space inode, we can deadlock during commit
3989 * if we put it into the delayed code.
3990 *
3991 * The data relocation inode should also be directly updated
3992 * without delay
3993 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003994 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003995 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003996 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003997 btrfs_update_root_times(trans, root);
3998
Chris Mason21151332011-11-10 20:39:08 -05003999 ret = btrfs_delayed_update_inode(trans, root, inode);
4000 if (!ret)
4001 btrfs_set_inode_last_trans(trans, inode);
4002 return ret;
4003 }
4004
4005 return btrfs_update_inode_item(trans, root, inode);
4006}
4007
Josef Bacikbe6aef62012-10-22 15:43:12 -04004008noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004011{
4012 int ret;
4013
4014 ret = btrfs_update_inode(trans, root, inode);
4015 if (ret == -ENOSPC)
4016 return btrfs_update_inode_item(trans, root, inode);
4017 return ret;
4018}
4019
4020/*
Chris Masond352ac62008-09-29 15:18:18 -04004021 * unlink helper that gets used here in inode.c and in the tree logging
4022 * recovery code. It remove a link in a directory with a given name, and
4023 * also drops the back refs in the inode to the directory
4024 */
Al Viro92986792011-03-04 17:14:37 +00004025static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004027 struct btrfs_inode *dir,
4028 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004029 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004030{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004031 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004032 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004034 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004035 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004036 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004037 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004038 u64 ino = btrfs_ino(inode);
4039 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004040
4041 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004042 if (!path) {
4043 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004044 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004045 }
4046
Chris Masonb9473432009-03-13 11:00:37 -04004047 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004048 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004049 name, name_len, -1);
4050 if (IS_ERR(di)) {
4051 ret = PTR_ERR(di);
4052 goto err;
4053 }
4054 if (!di) {
4055 ret = -ENOENT;
4056 goto err;
4057 }
Chris Mason5f39d392007-10-15 16:14:19 -04004058 leaf = path->nodes[0];
4059 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004060 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004061 if (ret)
4062 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004063 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004064
Miao Xie67de1172013-12-26 13:07:06 +08004065 /*
4066 * If we don't have dir index, we have to get it by looking up
4067 * the inode ref, since we get the inode ref, remove it directly,
4068 * it is unnecessary to do delayed deletion.
4069 *
4070 * But if we have dir index, needn't search inode ref to get it.
4071 * Since the inode ref is close to the inode item, it is better
4072 * that we delay to delete it, and just do this deletion when
4073 * we update the inode item.
4074 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004075 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004076 ret = btrfs_delayed_delete_inode_ref(inode);
4077 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004078 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004079 goto skip_backref;
4080 }
4081 }
4082
Li Zefan33345d012011-04-20 10:31:50 +08004083 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4084 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004085 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004086 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004087 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004088 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004089 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004090 goto err;
4091 }
Miao Xie67de1172013-12-26 13:07:06 +08004092skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004093 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004095 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004096 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 }
Chris Mason39279cc2007-06-12 06:35:45 -04004098
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004099 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4100 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004102 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 goto err;
4104 }
Chris Masone02119d2008-09-05 16:13:11 -04004105
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004106 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4107 index);
Chris Mason6418c962010-10-30 07:34:24 -04004108 if (ret == -ENOENT)
4109 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004110 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004111 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004112err:
4113 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004114 if (ret)
4115 goto out;
4116
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004117 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004118 inode_inc_iversion(&inode->vfs_inode);
4119 inode_inc_iversion(&dir->vfs_inode);
4120 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4121 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4122 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004123out:
Chris Mason39279cc2007-06-12 06:35:45 -04004124 return ret;
4125}
4126
Al Viro92986792011-03-04 17:14:37 +00004127int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4128 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004130 const char *name, int name_len)
4131{
4132 int ret;
4133 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4134 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004135 drop_nlink(&inode->vfs_inode);
4136 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004137 }
4138 return ret;
4139}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004140
4141/*
4142 * helper to start transaction for unlink and rmdir.
4143 *
Josef Bacikd52be812013-05-29 14:54:47 -04004144 * unlink and rmdir are special in btrfs, they do not always free space, so
4145 * if we cannot make our reservations the normal way try and see if there is
4146 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4147 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004148 */
Josef Bacikd52be812013-05-29 14:54:47 -04004149static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152
Josef Bacike70bea52011-10-11 14:18:24 -04004153 /*
4154 * 1 for the possible orphan item
4155 * 1 for the dir item
4156 * 1 for the dir index
4157 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004158 * 1 for the inode
4159 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004160 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161}
4162
Chris Mason39279cc2007-06-12 06:35:45 -04004163static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4164{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004167 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004168 int ret;
4169
Josef Bacikd52be812013-05-29 14:54:47 -04004170 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004171 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004172 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004173
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4175 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004176
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004177 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4178 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4179 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004180 if (ret)
4181 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004182
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004184 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004185 if (ret)
4186 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 }
Josef Bacik7b128762008-07-24 12:17:14 -04004188
Tsutomu Itohb5324022011-07-19 07:27:20 +00004189out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004190 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004191 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004192 return ret;
4193}
4194
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004195int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4196 struct btrfs_root *root,
4197 struct inode *dir, u64 objectid,
4198 const char *name, int name_len)
4199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004200 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201 struct btrfs_path *path;
4202 struct extent_buffer *leaf;
4203 struct btrfs_dir_item *di;
4204 struct btrfs_key key;
4205 u64 index;
4206 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004207 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208
4209 path = btrfs_alloc_path();
4210 if (!path)
4211 return -ENOMEM;
4212
Li Zefan33345d012011-04-20 10:31:50 +08004213 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004215 if (IS_ERR_OR_NULL(di)) {
4216 if (!di)
4217 ret = -ENOENT;
4218 else
4219 ret = PTR_ERR(di);
4220 goto out;
4221 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222
4223 leaf = path->nodes[0];
4224 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4225 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4226 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004228 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 goto out;
4230 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004231 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004233 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4234 root->root_key.objectid, dir_ino,
4235 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004237 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004238 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 goto out;
4240 }
Li Zefan33345d012011-04-20 10:31:50 +08004241 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 if (IS_ERR_OR_NULL(di)) {
4244 if (!di)
4245 ret = -ENOENT;
4246 else
4247 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004248 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 goto out;
4250 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251
4252 leaf = path->nodes[0];
4253 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004254 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255 index = key.offset;
4256 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004257 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004259 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004261 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 goto out;
4263 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004264
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004265 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004266 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004267 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004268 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004269 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004270 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004272 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274}
4275
Chris Mason39279cc2007-06-12 06:35:45 -04004276static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4277{
David Howells2b0143b2015-03-17 22:25:59 +00004278 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004279 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004280 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004281 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004282 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004283
David Sterbab3ae2442012-09-13 16:04:34 -06004284 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004285 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004286 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004287 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004288
Josef Bacikd52be812013-05-29 14:54:47 -04004289 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004290 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004291 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004292
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004293 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 err = btrfs_unlink_subvol(trans, root, dir,
4295 BTRFS_I(inode)->location.objectid,
4296 dentry->d_name.name,
4297 dentry->d_name.len);
4298 goto out;
4299 }
4300
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004301 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004302 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004303 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004304
Filipe Manana44f714d2016-06-06 16:11:13 +01004305 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4306
Chris Mason39279cc2007-06-12 06:35:45 -04004307 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004308 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4309 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4310 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004311 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004312 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004313 /*
4314 * Propagate the last_unlink_trans value of the deleted dir to
4315 * its parent directory. This is to prevent an unrecoverable
4316 * log tree in the case we do something like this:
4317 * 1) create dir foo
4318 * 2) create snapshot under dir foo
4319 * 3) delete the snapshot
4320 * 4) rmdir foo
4321 * 5) mkdir foo
4322 * 6) fsync foo or some file inside foo
4323 */
4324 if (last_unlink_trans >= trans->transid)
4325 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4326 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004327out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004328 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004329 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004330
Chris Mason39279cc2007-06-12 06:35:45 -04004331 return err;
4332}
4333
Chris Mason28f75a02015-02-04 06:59:29 -08004334static int truncate_space_check(struct btrfs_trans_handle *trans,
4335 struct btrfs_root *root,
4336 u64 bytes_deleted)
4337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004338 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004339 int ret;
4340
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004341 /*
4342 * This is only used to apply pressure to the enospc system, we don't
4343 * intend to use this reservation at all.
4344 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004345 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004346 bytes_deleted *= fs_info->nodesize;
4347 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004348 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004349 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004350 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004351 trans->transid,
4352 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004353 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004354 }
Chris Mason28f75a02015-02-04 06:59:29 -08004355 return ret;
4356
4357}
4358
Filipe Manana0305cd52015-10-16 12:34:25 +01004359static int truncate_inline_extent(struct inode *inode,
4360 struct btrfs_path *path,
4361 struct btrfs_key *found_key,
4362 const u64 item_end,
4363 const u64 new_size)
4364{
4365 struct extent_buffer *leaf = path->nodes[0];
4366 int slot = path->slots[0];
4367 struct btrfs_file_extent_item *fi;
4368 u32 size = (u32)(new_size - found_key->offset);
4369 struct btrfs_root *root = BTRFS_I(inode)->root;
4370
4371 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4372
4373 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4374 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004375 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004376
4377 /*
4378 * Zero out the remaining of the last page of our inline extent,
4379 * instead of directly truncating our inline extent here - that
4380 * would be much more complex (decompressing all the data, then
4381 * compressing the truncated data, which might be bigger than
4382 * the size of the inline extent, resize the extent, etc).
4383 * We release the path because to get the page we might need to
4384 * read the extent item from disk (data not in the page cache).
4385 */
4386 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304387 return btrfs_truncate_block(inode, offset, page_end - offset,
4388 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004389 }
4390
4391 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4392 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004393 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004394
4395 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4396 inode_sub_bytes(inode, item_end + 1 - new_size);
4397
4398 return 0;
4399}
4400
Chris Mason323ac952008-10-01 19:05:46 -04004401/*
Chris Mason39279cc2007-06-12 06:35:45 -04004402 * this can truncate away extent items, csum items and directory items.
4403 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004404 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004405 *
4406 * csum items that cross the new i_size are truncated to the new size
4407 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004408 *
4409 * min_type is the minimum key type to truncate down to. If set to 0, this
4410 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004411 */
Yan, Zheng80825102009-11-12 09:35:36 +00004412int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4413 struct btrfs_root *root,
4414 struct inode *inode,
4415 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004418 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004419 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004420 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004421 struct btrfs_key key;
4422 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004424 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004425 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004427 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004428 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 int found_extent;
4430 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004431 int pending_del_nr = 0;
4432 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004433 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004434 int ret;
4435 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004436 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004437 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004438 bool be_nice = 0;
4439 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004440 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004441
4442 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004443
Chris Mason28ed1342014-12-17 09:41:04 -08004444 /*
4445 * for non-free space inodes and ref cows, we want to back off from
4446 * time to time
4447 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004448 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004449 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4450 be_nice = 1;
4451
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004452 path = btrfs_alloc_path();
4453 if (!path)
4454 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004455 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004456
Josef Bacik5dc562c2012-08-17 13:14:17 -04004457 /*
4458 * We want to drop from the next block forward in case this new size is
4459 * not block aligned since we will be keeping the last block of the
4460 * extent just the way it is.
4461 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004462 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004463 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004464 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004465 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004466 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004467
Miao Xie16cdcec2011-04-22 18:12:22 +08004468 /*
4469 * This function is also used to drop the items in the log tree before
4470 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4471 * it is used to drop the loged items. So we shouldn't kill the delayed
4472 * items.
4473 */
4474 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004475 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004476
Li Zefan33345d012011-04-20 10:31:50 +08004477 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004478 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004479 key.type = (u8)-1;
4480
Chris Mason85e21ba2008-01-29 15:11:36 -05004481search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004482 /*
4483 * with a 16K leaf size and 128MB extents, you can actually queue
4484 * up a huge file in a single leaf. Most of the time that
4485 * bytes_deleted is > 0, it will be huge by the time we get here
4486 */
Byongho Leeee221842015-12-15 01:42:10 +09004487 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004488 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004489 err = -EAGAIN;
4490 goto error;
4491 }
4492 }
4493
4494
Chris Masonb9473432009-03-13 11:00:37 -04004495 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004496 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004497 if (ret < 0) {
4498 err = ret;
4499 goto out;
4500 }
Chris Masond3977122009-01-05 21:25:51 -05004501
Chris Mason85e21ba2008-01-29 15:11:36 -05004502 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004503 /* there are no items in the tree for us to truncate, we're
4504 * done
4505 */
Yan, Zheng80825102009-11-12 09:35:36 +00004506 if (path->slots[0] == 0)
4507 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004508 path->slots[0]--;
4509 }
4510
Chris Masond3977122009-01-05 21:25:51 -05004511 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004512 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004513 leaf = path->nodes[0];
4514 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004515 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004516
Li Zefan33345d012011-04-20 10:31:50 +08004517 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004518 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004519
Chris Mason85e21ba2008-01-29 15:11:36 -05004520 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004521 break;
4522
Chris Mason5f39d392007-10-15 16:14:19 -04004523 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004525 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004526 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004527 extent_type = btrfs_file_extent_type(leaf, fi);
4528 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004529 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004530 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004531
4532 trace_btrfs_truncate_show_fi_regular(
4533 BTRFS_I(inode), leaf, fi,
4534 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004535 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004536 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004537 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004538
4539 trace_btrfs_truncate_show_fi_inline(
4540 BTRFS_I(inode), leaf, fi, path->slots[0],
4541 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004542 }
Yan008630c2007-11-07 13:31:09 -05004543 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004544 }
Yan, Zheng80825102009-11-12 09:35:36 +00004545 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004546 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004547 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004548 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004549 break;
4550 if (found_key.offset >= new_size)
4551 del_item = 1;
4552 else
4553 del_item = 0;
4554 }
Chris Mason39279cc2007-06-12 06:35:45 -04004555 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004556 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004557 if (found_type != BTRFS_EXTENT_DATA_KEY)
4558 goto delete;
4559
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004560 if (del_item)
4561 last_size = found_key.offset;
4562 else
4563 last_size = new_size;
4564
Chris Mason179e29e2007-11-01 11:28:41 -04004565 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004566 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004567 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004568 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004569 u64 orig_num_bytes =
4570 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004571 extent_num_bytes = ALIGN(new_size -
4572 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004573 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004574 btrfs_set_file_extent_num_bytes(leaf, fi,
4575 extent_num_bytes);
4576 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004577 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004578 if (test_bit(BTRFS_ROOT_REF_COWS,
4579 &root->state) &&
4580 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004581 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004582 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004583 } else {
Chris Masondb945352007-10-15 16:15:53 -04004584 extent_num_bytes =
4585 btrfs_file_extent_disk_num_bytes(leaf,
4586 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004587 extent_offset = found_key.offset -
4588 btrfs_file_extent_offset(leaf, fi);
4589
Chris Mason39279cc2007-06-12 06:35:45 -04004590 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004591 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004592 if (extent_start != 0) {
4593 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004594 if (test_bit(BTRFS_ROOT_REF_COWS,
4595 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004596 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 }
4598 }
Chris Mason90692182008-02-08 13:49:28 -05004599 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004600 /*
4601 * we can't truncate inline items that have had
4602 * special encodings
4603 */
4604 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004605 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4606 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004607
4608 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004609 * Need to release path in order to truncate a
4610 * compressed extent. So delete any accumulated
4611 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004612 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004613 if (btrfs_file_extent_compression(leaf, fi) !=
4614 BTRFS_COMPRESS_NONE && pending_del_nr) {
4615 err = btrfs_del_items(trans, root, path,
4616 pending_del_slot,
4617 pending_del_nr);
4618 if (err) {
4619 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004620 err);
4621 goto error;
4622 }
4623 pending_del_nr = 0;
4624 }
4625
4626 err = truncate_inline_extent(inode, path,
4627 &found_key,
4628 item_end,
4629 new_size);
4630 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004631 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004632 goto error;
4633 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004634 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4635 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004636 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004637 }
Chris Mason39279cc2007-06-12 06:35:45 -04004638 }
Chris Mason179e29e2007-11-01 11:28:41 -04004639delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004640 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004641 if (!pending_del_nr) {
4642 /* no pending yet, add ourselves */
4643 pending_del_slot = path->slots[0];
4644 pending_del_nr = 1;
4645 } else if (pending_del_nr &&
4646 path->slots[0] + 1 == pending_del_slot) {
4647 /* hop on the pending chunk */
4648 pending_del_nr++;
4649 pending_del_slot = path->slots[0];
4650 } else {
Chris Masond3977122009-01-05 21:25:51 -05004651 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004652 }
Chris Mason39279cc2007-06-12 06:35:45 -04004653 } else {
4654 break;
4655 }
Chris Mason28f75a02015-02-04 06:59:29 -08004656 should_throttle = 0;
4657
Miao Xie27cdeb72014-04-02 19:51:05 +08004658 if (found_extent &&
4659 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004660 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004661 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004662 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004663 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004664 extent_num_bytes, 0,
4665 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004666 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004667 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004668 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4669 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004670 trans->delayed_ref_updates * 2,
4671 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004672 if (be_nice) {
4673 if (truncate_space_check(trans, root,
4674 extent_num_bytes)) {
4675 should_end = 1;
4676 }
4677 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004678 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004679 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004680 }
Chris Mason39279cc2007-06-12 06:35:45 -04004681 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004682
Yan, Zheng80825102009-11-12 09:35:36 +00004683 if (found_type == BTRFS_INODE_ITEM_KEY)
4684 break;
4685
4686 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004687 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004688 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004689 if (pending_del_nr) {
4690 ret = btrfs_del_items(trans, root, path,
4691 pending_del_slot,
4692 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004693 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004694 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004695 goto error;
4696 }
Yan, Zheng80825102009-11-12 09:35:36 +00004697 pending_del_nr = 0;
4698 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004699 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004700 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004701 unsigned long updates = trans->delayed_ref_updates;
4702 if (updates) {
4703 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004704 ret = btrfs_run_delayed_refs(trans,
4705 fs_info,
4706 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004707 if (ret && !err)
4708 err = ret;
4709 }
4710 }
Chris Mason28f75a02015-02-04 06:59:29 -08004711 /*
4712 * if we failed to refill our space rsv, bail out
4713 * and let the transaction restart
4714 */
4715 if (should_end) {
4716 err = -EAGAIN;
4717 goto error;
4718 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004719 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004720 } else {
4721 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004722 }
Chris Mason39279cc2007-06-12 06:35:45 -04004723 }
Yan, Zheng80825102009-11-12 09:35:36 +00004724out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 if (pending_del_nr) {
4726 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4727 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004728 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004729 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004730 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004731error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004732 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4733 ASSERT(last_size >= new_size);
4734 if (!err && last_size > new_size)
4735 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004736 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004737 }
Chris Mason28ed1342014-12-17 09:41:04 -08004738
Chris Mason39279cc2007-06-12 06:35:45 -04004739 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004740
Byongho Leeee221842015-12-15 01:42:10 +09004741 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004742 unsigned long updates = trans->delayed_ref_updates;
4743 if (updates) {
4744 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004745 ret = btrfs_run_delayed_refs(trans, fs_info,
4746 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004747 if (ret && !err)
4748 err = ret;
4749 }
4750 }
Yan, Zheng80825102009-11-12 09:35:36 +00004751 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004752}
4753
Chris Masona52d9a82007-08-27 16:49:44 -04004754/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004756 * @inode - inode that we're zeroing
4757 * @from - the offset to start zeroing
4758 * @len - the length to zero, 0 to zero the entire range respective to the
4759 * offset
4760 * @front - zero up to the offset instead of from the offset on
4761 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004763 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004764 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004766 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004767{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004768 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004769 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4771 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004772 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004773 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004775 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304776 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004777 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004778 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004779 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 u64 block_start;
4781 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004782
Josef Bacik2aaa6652012-08-29 14:27:18 -04004783 if ((offset & (blocksize - 1)) == 0 &&
4784 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004785 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004787 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004789 if (ret)
4790 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004791
Chris Mason211c17f2008-05-15 09:13:45 -04004792again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004793 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004794 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004795 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796 round_down(from, blocksize),
4797 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004798 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004799 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004800 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 block_start = round_down(from, blocksize);
4803 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004804
Chris Masona52d9a82007-08-27 16:49:44 -04004805 if (!PageUptodate(page)) {
4806 ret = btrfs_readpage(NULL, page);
4807 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004808 if (page->mapping != mapping) {
4809 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004810 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004811 goto again;
4812 }
Chris Masona52d9a82007-08-27 16:49:44 -04004813 if (!PageUptodate(page)) {
4814 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004815 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004816 }
4817 }
Chris Mason211c17f2008-05-15 09:13:45 -04004818 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004819
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304820 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821 set_page_extent_mapped(page);
4822
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004826 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004828 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004829 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004830 btrfs_put_ordered_extent(ordered);
4831 goto again;
4832 }
4833
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004835 EXTENT_DIRTY | EXTENT_DELALLOC |
4836 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004837 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004838
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304839 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004840 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004841 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304842 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004843 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004844 goto out_unlock;
4845 }
4846
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304847 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004848 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004851 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 memset(kaddr + (block_start - page_offset(page)),
4853 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004854 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 memset(kaddr + (block_start - page_offset(page)) + offset,
4856 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004857 flush_dcache_page(page);
4858 kunmap(page);
4859 }
Chris Mason247e7432008-07-17 12:53:51 -04004860 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304862 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004863 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004864
Chris Mason89642222008-07-24 09:41:53 -04004865out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004866 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 btrfs_delalloc_release_space(inode, block_start,
4868 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004869 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004870 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004871out:
4872 return ret;
4873}
4874
Josef Bacik16e75492013-10-22 12:18:51 -04004875static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4876 u64 offset, u64 len)
4877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004879 struct btrfs_trans_handle *trans;
4880 int ret;
4881
4882 /*
4883 * Still need to make sure the inode looks like it's been updated so
4884 * that any holes get logged if we fsync.
4885 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4887 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004888 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4889 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4890 return 0;
4891 }
4892
4893 /*
4894 * 1 - for the one we're dropping
4895 * 1 - for the one we're adding
4896 * 1 - for updating the inode.
4897 */
4898 trans = btrfs_start_transaction(root, 3);
4899 if (IS_ERR(trans))
4900 return PTR_ERR(trans);
4901
4902 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4903 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004904 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004905 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004906 return ret;
4907 }
4908
David Sterbaf85b7372017-01-20 14:54:07 +01004909 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4910 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004911 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004912 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004913 else
4914 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004915 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004916 return ret;
4917}
4918
Josef Bacik695a0d02011-03-04 15:46:53 -05004919/*
4920 * This function puts in dummy file extents for the area we're creating a hole
4921 * for. So if we are truncating this file to a larger size we need to insert
4922 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4923 * the range between oldsize and size
4924 */
Josef Bacika41ad392011-01-31 15:30:16 -05004925int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004926{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004927 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004928 struct btrfs_root *root = BTRFS_I(inode)->root;
4929 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004930 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004931 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004932 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4934 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004935 u64 last_byte;
4936 u64 cur_offset;
4937 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004938 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004939
Josef Bacika71754f2013-06-17 17:14:39 -04004940 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304941 * If our size started in the middle of a block we need to zero out the
4942 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004943 * expose stale data.
4944 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304945 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004946 if (err)
4947 return err;
4948
Yan Zheng9036c102008-10-30 14:19:41 -04004949 if (size <= hole_start)
4950 return 0;
4951
Yan Zheng9036c102008-10-30 14:19:41 -04004952 while (1) {
4953 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004954
David Sterbaff13db42015-12-03 14:30:40 +01004955 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004956 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004957 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004958 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004959 if (!ordered)
4960 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004961 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4962 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004963 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004964 btrfs_put_ordered_extent(ordered);
4965 }
4966
Yan Zheng9036c102008-10-30 14:19:41 -04004967 cur_offset = hole_start;
4968 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004969 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004970 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004971 if (IS_ERR(em)) {
4972 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004973 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004974 break;
4975 }
Yan Zheng9036c102008-10-30 14:19:41 -04004976 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004977 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004978 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004979 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004980 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004981
Josef Bacik16e75492013-10-22 12:18:51 -04004982 err = maybe_insert_hole(root, inode, cur_offset,
4983 hole_size);
4984 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004985 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004986 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004987 cur_offset + hole_size - 1, 0);
4988 hole_em = alloc_extent_map();
4989 if (!hole_em) {
4990 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4991 &BTRFS_I(inode)->runtime_flags);
4992 goto next;
4993 }
4994 hole_em->start = cur_offset;
4995 hole_em->len = hole_size;
4996 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004997
Josef Bacik5dc562c2012-08-17 13:14:17 -04004998 hole_em->block_start = EXTENT_MAP_HOLE;
4999 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005000 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005001 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005002 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005003 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005004 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005
5006 while (1) {
5007 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005008 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005009 write_unlock(&em_tree->lock);
5010 if (err != -EEXIST)
5011 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005012 btrfs_drop_extent_cache(BTRFS_I(inode),
5013 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005014 cur_offset +
5015 hole_size - 1, 0);
5016 }
5017 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005018 }
Josef Bacik16e75492013-10-22 12:18:51 -04005019next:
Yan Zheng9036c102008-10-30 14:19:41 -04005020 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005021 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005022 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005023 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005024 break;
5025 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005026 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005027 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5028 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005029 return err;
5030}
5031
Eric Sandeen3972f262013-01-12 02:57:22 +00005032static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005033{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005034 struct btrfs_root *root = BTRFS_I(inode)->root;
5035 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005036 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005037 loff_t newsize = attr->ia_size;
5038 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005039 int ret;
5040
Eric Sandeen3972f262013-01-12 02:57:22 +00005041 /*
5042 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5043 * special case where we need to update the times despite not having
5044 * these flags set. For all other operations the VFS set these flags
5045 * explicitly if it wants a timestamp update.
5046 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005047 if (newsize != oldsize) {
5048 inode_inc_iversion(inode);
5049 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5050 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005051 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005052 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005053
Josef Bacika41ad392011-01-31 15:30:16 -05005054 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005055 /*
5056 * Don't do an expanding truncate while snapshoting is ongoing.
5057 * This is to ensure the snapshot captures a fully consistent
5058 * state of this file - if the snapshot captures this expanding
5059 * truncation, it must capture all writes that happened before
5060 * this truncation.
5061 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005062 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005063 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005064 if (ret) {
5065 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005066 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005067 }
Yan, Zheng80825102009-11-12 09:35:36 +00005068
Miao Xief4a2f4c2011-12-14 20:12:01 -05005069 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005070 if (IS_ERR(trans)) {
5071 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005072 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005074
5075 i_size_write(inode, newsize);
5076 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305077 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005078 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005079 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005080 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005081 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005082
Josef Bacika41ad392011-01-31 15:30:16 -05005083 /*
5084 * We're truncating a file that used to have good data down to
5085 * zero. Make sure it gets into the ordered flush list so that
5086 * any new writes get down to disk quickly.
5087 */
5088 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005089 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5090 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005091
Josef Bacikf3fe8202013-01-07 17:03:21 -05005092 /*
5093 * 1 for the orphan item we're going to add
5094 * 1 for the orphan item deletion.
5095 */
5096 trans = btrfs_start_transaction(root, 2);
5097 if (IS_ERR(trans))
5098 return PTR_ERR(trans);
5099
5100 /*
5101 * We need to do this in case we fail at _any_ point during the
5102 * actual truncate. Once we do the truncate_setsize we could
5103 * invalidate pages which forces any outstanding ordered io to
5104 * be instantly completed which will give us extents that need
5105 * to be truncated. If we fail to get an orphan inode down we
5106 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005107 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005108 * will be consistent.
5109 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005110 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005111 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005112 if (ret)
5113 return ret;
5114
Josef Bacika41ad392011-01-31 15:30:16 -05005115 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5116 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005117
5118 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005119 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005120 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005121 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005122
Josef Bacika41ad392011-01-31 15:30:16 -05005123 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005124 if (ret && inode->i_nlink) {
5125 int err;
5126
Liu Bo19fd2df2016-12-01 13:01:02 -08005127 /* To get a stable disk_i_size */
5128 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5129 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005130 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005131 return err;
5132 }
5133
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005134 /*
5135 * failed to truncate, disk_i_size is only adjusted down
5136 * as we remove extents, so it should represent the true
5137 * size of the inode, so reset the in memory size and
5138 * delete our orphan entry.
5139 */
5140 trans = btrfs_join_transaction(root);
5141 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005142 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005143 return ret;
5144 }
5145 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005146 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005147 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005148 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005149 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005150 }
Yan, Zheng80825102009-11-12 09:35:36 +00005151 }
5152
Josef Bacika41ad392011-01-31 15:30:16 -05005153 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005154}
5155
Chris Mason39279cc2007-06-12 06:35:45 -04005156static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5157{
David Howells2b0143b2015-03-17 22:25:59 +00005158 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005159 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005160 int err;
5161
Li Zefanb83cc962010-12-20 16:04:08 +08005162 if (btrfs_root_readonly(root))
5163 return -EROFS;
5164
Jan Kara31051c82016-05-26 16:55:18 +02005165 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005166 if (err)
5167 return err;
5168
Chris Mason5a3f23d2009-03-31 13:27:11 -04005169 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005170 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005171 if (err)
5172 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005173 }
Yan Zheng9036c102008-10-30 14:19:41 -04005174
Christoph Hellwig10257742010-06-04 11:30:02 +02005175 if (attr->ia_valid) {
5176 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005177 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005178 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005179
Josef Bacik22c44fe2011-11-30 10:45:38 -05005180 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005181 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005182 }
5183
Chris Mason39279cc2007-06-12 06:35:45 -04005184 return err;
5185}
Chris Mason61295eb2008-01-14 16:24:38 -05005186
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005187/*
5188 * While truncating the inode pages during eviction, we get the VFS calling
5189 * btrfs_invalidatepage() against each page of the inode. This is slow because
5190 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5191 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5192 * extent_state structures over and over, wasting lots of time.
5193 *
5194 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5195 * those expensive operations on a per page basis and do only the ordered io
5196 * finishing, while we release here the extent_map and extent_state structures,
5197 * without the excessive merging and splitting.
5198 */
5199static void evict_inode_truncate_pages(struct inode *inode)
5200{
5201 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5202 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5203 struct rb_node *node;
5204
5205 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005206 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207
5208 write_lock(&map_tree->lock);
5209 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5210 struct extent_map *em;
5211
5212 node = rb_first(&map_tree->map);
5213 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005214 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5215 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005216 remove_extent_mapping(map_tree, em);
5217 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005218 if (need_resched()) {
5219 write_unlock(&map_tree->lock);
5220 cond_resched();
5221 write_lock(&map_tree->lock);
5222 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005223 }
5224 write_unlock(&map_tree->lock);
5225
Filipe Manana6ca07092015-05-26 00:55:42 +01005226 /*
5227 * Keep looping until we have no more ranges in the io tree.
5228 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005229 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5230 * still in progress (unlocked the pages in the bio but did not yet
5231 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005232 * ranges can still be locked and eviction started because before
5233 * submitting those bios, which are executed by a separate task (work
5234 * queue kthread), inode references (inode->i_count) were not taken
5235 * (which would be dropped in the end io callback of each bio).
5236 * Therefore here we effectively end up waiting for those bios and
5237 * anyone else holding locked ranges without having bumped the inode's
5238 * reference count - if we don't do it, when they access the inode's
5239 * io_tree to unlock a range it may be too late, leading to an
5240 * use-after-free issue.
5241 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242 spin_lock(&io_tree->lock);
5243 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5244 struct extent_state *state;
5245 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005246 u64 start;
5247 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248
5249 node = rb_first(&io_tree->state);
5250 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005251 start = state->start;
5252 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253 spin_unlock(&io_tree->lock);
5254
David Sterbaff13db42015-12-03 14:30:40 +01005255 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005256
5257 /*
5258 * If still has DELALLOC flag, the extent didn't reach disk,
5259 * and its reserved space won't be freed by delayed_ref.
5260 * So we need to free its reserved space here.
5261 * (Refer to comment in btrfs_invalidatepage, case 2)
5262 *
5263 * Note, end is the bytenr of last byte, so we need + 1 here.
5264 */
5265 if (state->state & EXTENT_DELALLOC)
5266 btrfs_qgroup_free_data(inode, start, end - start + 1);
5267
Filipe Manana6ca07092015-05-26 00:55:42 +01005268 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269 EXTENT_LOCKED | EXTENT_DIRTY |
5270 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5271 EXTENT_DEFRAG, 1, 1,
5272 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005273
Filipe Manana7064dd52014-08-08 02:47:05 +01005274 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 spin_lock(&io_tree->lock);
5276 }
5277 spin_unlock(&io_tree->lock);
5278}
5279
Al Virobd555972010-06-07 11:35:40 -04005280void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005283 struct btrfs_trans_handle *trans;
5284 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005285 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005286 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005287 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005288 int ret;
5289
liubo1abe9b82011-03-24 11:18:59 +00005290 trace_btrfs_inode_evict(inode);
5291
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005292 if (!root) {
5293 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5294 return;
5295 }
5296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005297 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005298
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005299 evict_inode_truncate_pages(inode);
5300
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005301 if (inode->i_nlink &&
5302 ((btrfs_root_refs(&root->root_item) != 0 &&
5303 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005304 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005305 goto no_delete;
5306
Chris Mason39279cc2007-06-12 06:35:45 -04005307 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005308 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005309 goto no_delete;
5310 }
Al Virobd555972010-06-07 11:35:40 -04005311 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005312 if (!special_file(inode->i_mode))
5313 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005314
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005315 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005316
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005317 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005318 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005319 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005320 goto no_delete;
5321 }
5322
Yan, Zheng76dda932009-09-21 16:00:26 -04005323 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005324 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5325 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005326 goto no_delete;
5327 }
5328
Nikolay Borisovaa790212017-01-10 20:35:40 +02005329 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005330 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005331 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005332 goto no_delete;
5333 }
5334
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005335 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005336 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005337 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005338 goto no_delete;
5339 }
Josef Bacik4a338542011-08-29 11:01:31 -04005340 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005341 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005343
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005344 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005345
Josef Bacik4289a662011-08-05 13:22:24 -04005346 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005347 * This is a bit simpler than btrfs_truncate since we've already
5348 * reserved our space for our orphan item in the unlink, so we just
5349 * need to reserve some slack space in case we add bytes and update
5350 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005351 */
Yan, Zheng80825102009-11-12 09:35:36 +00005352 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005353 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5354 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005355
Josef Bacik726c35f2011-09-26 15:46:06 -04005356 /*
5357 * Try and steal from the global reserve since we will
5358 * likely not use this space anyway, we want to try as
5359 * hard as possible to get this to work.
5360 */
5361 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005362 steal_from_global++;
5363 else
5364 steal_from_global = 0;
5365 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005366
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 /*
5368 * steal_from_global == 0: we reserved stuff, hooray!
5369 * steal_from_global == 1: we didn't reserve stuff, boo!
5370 * steal_from_global == 2: we've committed, still not a lot of
5371 * room but maybe we'll have room in the global reserve this
5372 * time.
5373 * steal_from_global == 3: abandon all hope!
5374 */
5375 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005376 btrfs_warn(fs_info,
5377 "Could not get space for a delete, will truncate on mount %d",
5378 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005379 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005380 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005381 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005382 }
5383
Miao Xie0e8c36a2012-12-19 06:59:51 +00005384 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005385 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005386 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005387 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005388 goto no_delete;
5389 }
5390
Josef Bacik3bce8762015-02-24 12:35:51 -08005391 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005392 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005393 * sure there is room to do it, if not we need to commit and try
5394 * again.
5395 */
5396 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005397 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005399 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005400 else
5401 ret = -ENOSPC;
5402 }
5403
5404 /*
5405 * Couldn't steal from the global reserve, we have too much
5406 * pending stuff built up, commit the transaction and try it
5407 * again.
5408 */
5409 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005410 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005411 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005412 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005413 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005414 goto no_delete;
5415 }
5416 continue;
5417 } else {
5418 steal_from_global = 0;
5419 }
5420
Josef Bacik4289a662011-08-05 13:22:24 -04005421 trans->block_rsv = rsv;
5422
Yan, Zhengd68fc572010-05-16 10:49:58 -04005423 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005424 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005425 break;
5426
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005427 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005428 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005429 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005430 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005431 }
5432
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005433 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005434
Josef Bacik4ef31a42013-08-13 14:10:08 -04005435 /*
5436 * Errors here aren't a big deal, it just means we leave orphan items
5437 * in the tree. They will be cleaned up on the next mount.
5438 */
Yan, Zheng80825102009-11-12 09:35:36 +00005439 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005440 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005441 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005442 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005443 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005444 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005445
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005446 trans->block_rsv = &fs_info->trans_block_rsv;
5447 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005448 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005449 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005450
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005451 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005452 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005453no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005454 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005455 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005456}
5457
5458/*
5459 * this returns the key found in the dir entry in the location pointer.
5460 * If no dir entries were found, location->objectid is 0.
5461 */
5462static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5463 struct btrfs_key *location)
5464{
5465 const char *name = dentry->d_name.name;
5466 int namelen = dentry->d_name.len;
5467 struct btrfs_dir_item *di;
5468 struct btrfs_path *path;
5469 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005470 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005471
5472 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005473 if (!path)
5474 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005475
David Sterbaf85b7372017-01-20 14:54:07 +01005476 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5477 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005478 if (IS_ERR(di))
5479 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005480
David Sterbac7040052011-04-19 18:00:01 +02005481 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005482 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005483
Chris Mason5f39d392007-10-15 16:14:19 -04005484 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005485out:
Chris Mason39279cc2007-06-12 06:35:45 -04005486 btrfs_free_path(path);
5487 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005488out_err:
5489 location->objectid = 0;
5490 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005491}
5492
5493/*
5494 * when we hit a tree root in a directory, the btrfs part of the inode
5495 * needs to be changed to reflect the root directory of the tree root. This
5496 * is kind of like crossing a mount point.
5497 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005498static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 struct inode *dir,
5500 struct dentry *dentry,
5501 struct btrfs_key *location,
5502 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005503{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 struct btrfs_path *path;
5505 struct btrfs_root *new_root;
5506 struct btrfs_root_ref *ref;
5507 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005508 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 int ret;
5510 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005511
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 path = btrfs_alloc_path();
5513 if (!path) {
5514 err = -ENOMEM;
5515 goto out;
5516 }
Chris Mason39279cc2007-06-12 06:35:45 -04005517
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005519 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5520 key.type = BTRFS_ROOT_REF_KEY;
5521 key.offset = location->objectid;
5522
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005523 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 if (ret) {
5525 if (ret < 0)
5526 err = ret;
5527 goto out;
5528 }
Chris Mason39279cc2007-06-12 06:35:45 -04005529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 leaf = path->nodes[0];
5531 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005532 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005533 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5534 goto out;
5535
5536 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5537 (unsigned long)(ref + 1),
5538 dentry->d_name.len);
5539 if (ret)
5540 goto out;
5541
David Sterbab3b4aa72011-04-21 01:20:15 +02005542 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005544 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 if (IS_ERR(new_root)) {
5546 err = PTR_ERR(new_root);
5547 goto out;
5548 }
5549
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 *sub_root = new_root;
5551 location->objectid = btrfs_root_dirid(&new_root->root_item);
5552 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005553 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005554 err = 0;
5555out:
5556 btrfs_free_path(path);
5557 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005558}
5559
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560static void inode_tree_add(struct inode *inode)
5561{
5562 struct btrfs_root *root = BTRFS_I(inode)->root;
5563 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005564 struct rb_node **p;
5565 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005566 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005567 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568
Al Viro1d3382cb2010-10-23 15:19:20 -04005569 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005570 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005571 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005573 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574 while (*p) {
5575 parent = *p;
5576 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5577
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005578 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005580 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005581 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582 else {
5583 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005584 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005585 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005586 RB_CLEAR_NODE(parent);
5587 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005588 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 }
5590 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005591 rb_link_node(new, parent, p);
5592 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593 spin_unlock(&root->inode_lock);
5594}
5595
5596static void inode_tree_del(struct inode *inode)
5597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005602 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005606 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005608 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005609
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005610 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005611 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 spin_lock(&root->inode_lock);
5613 empty = RB_EMPTY_ROOT(&root->inode_tree);
5614 spin_unlock(&root->inode_lock);
5615 if (empty)
5616 btrfs_add_dead_root(root);
5617 }
5618}
5619
Jeff Mahoney143bede2012-03-01 14:56:26 +01005620void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005621{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005622 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005623 struct rb_node *node;
5624 struct rb_node *prev;
5625 struct btrfs_inode *entry;
5626 struct inode *inode;
5627 u64 objectid = 0;
5628
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005629 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005630 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005631
5632 spin_lock(&root->inode_lock);
5633again:
5634 node = root->inode_tree.rb_node;
5635 prev = NULL;
5636 while (node) {
5637 prev = node;
5638 entry = rb_entry(node, struct btrfs_inode, rb_node);
5639
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005640 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005642 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005643 node = node->rb_right;
5644 else
5645 break;
5646 }
5647 if (!node) {
5648 while (prev) {
5649 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005650 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005651 node = prev;
5652 break;
5653 }
5654 prev = rb_next(prev);
5655 }
5656 }
5657 while (node) {
5658 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005659 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005660 inode = igrab(&entry->vfs_inode);
5661 if (inode) {
5662 spin_unlock(&root->inode_lock);
5663 if (atomic_read(&inode->i_count) > 1)
5664 d_prune_aliases(inode);
5665 /*
Al Viro45321ac2010-06-07 13:43:19 -04005666 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005667 * the inode cache when its usage count
5668 * hits zero.
5669 */
5670 iput(inode);
5671 cond_resched();
5672 spin_lock(&root->inode_lock);
5673 goto again;
5674 }
5675
5676 if (cond_resched_lock(&root->inode_lock))
5677 goto again;
5678
5679 node = rb_next(node);
5680 }
5681 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005682}
5683
Chris Masone02119d2008-09-05 16:13:11 -04005684static int btrfs_init_locked_inode(struct inode *inode, void *p)
5685{
5686 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005687 inode->i_ino = args->location->objectid;
5688 memcpy(&BTRFS_I(inode)->location, args->location,
5689 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005690 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005691 return 0;
5692}
5693
5694static int btrfs_find_actor(struct inode *inode, void *opaque)
5695{
5696 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005697 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005698 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005699}
5700
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005702 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005703 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005704{
5705 struct inode *inode;
5706 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005707 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005708
Chris Mason90d3e592014-01-09 17:28:00 -08005709 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005710 args.root = root;
5711
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005712 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005713 btrfs_init_locked_inode,
5714 (void *)&args);
5715 return inode;
5716}
5717
Balaji Rao1a54ef82008-07-21 02:01:04 +05305718/* Get an inode object given its location and corresponding root.
5719 * Returns in *is_new if the inode was read from disk
5720 */
5721struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005722 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723{
5724 struct inode *inode;
5725
Chris Mason90d3e592014-01-09 17:28:00 -08005726 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305727 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005728 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305729
5730 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005731 int ret;
5732
5733 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005734 if (!is_bad_inode(inode)) {
5735 inode_tree_add(inode);
5736 unlock_new_inode(inode);
5737 if (new)
5738 *new = 1;
5739 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005740 unlock_new_inode(inode);
5741 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005742 ASSERT(ret < 0);
5743 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005744 }
5745 }
5746
Balaji Rao1a54ef82008-07-21 02:01:04 +05305747 return inode;
5748}
5749
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750static struct inode *new_simple_dir(struct super_block *s,
5751 struct btrfs_key *key,
5752 struct btrfs_root *root)
5753{
5754 struct inode *inode = new_inode(s);
5755
5756 if (!inode)
5757 return ERR_PTR(-ENOMEM);
5758
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005759 BTRFS_I(inode)->root = root;
5760 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005761 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762
5763 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005764 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005765 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766 inode->i_fop = &simple_dir_operations;
5767 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005768 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305769 inode->i_atime = inode->i_mtime;
5770 inode->i_ctime = inode->i_mtime;
5771 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772
5773 return inode;
5774}
5775
Chris Mason3de45862008-11-17 21:02:50 -05005776struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005777{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005778 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005779 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005781 struct btrfs_root *sub_root = root;
5782 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005783 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005784 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005785
5786 if (dentry->d_name.len > BTRFS_NAME_LEN)
5787 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005788
Jeff Layton39e3c952012-11-28 11:30:53 -05005789 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005790 if (ret < 0)
5791 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005794 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005795
5796 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005797 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005799 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005800
5801 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5802
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005803 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005804 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005805 &location, &sub_root);
5806 if (ret < 0) {
5807 if (ret != -ENOENT)
5808 inode = ERR_PTR(ret);
5809 else
5810 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5811 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005812 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005815
Julia Lawall34d19ba2011-01-24 19:55:19 +00005816 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005817 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005818 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005819 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005821 if (ret) {
5822 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005823 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005824 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005825 }
5826
Chris Mason3de45862008-11-17 21:02:50 -05005827 return inode;
5828}
5829
Nick Pigginfe15ce42011-01-07 17:49:23 +11005830static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005831{
5832 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005833 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005834
Li Zefan848cce02012-02-21 17:04:28 +08005835 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005836 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005837
Li Zefan848cce02012-02-21 17:04:28 +08005838 if (inode) {
5839 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005840 if (btrfs_root_refs(&root->root_item) == 0)
5841 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005842
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005843 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005844 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005845 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005846 return 0;
5847}
5848
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005849static void btrfs_dentry_release(struct dentry *dentry)
5850{
Daeseok Youn944a4512014-04-14 15:37:02 +09005851 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005852}
5853
Chris Mason3de45862008-11-17 21:02:50 -05005854static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005855 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005856{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005857 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005858
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005859 inode = btrfs_lookup_dentry(dir, dentry);
5860 if (IS_ERR(inode)) {
5861 if (PTR_ERR(inode) == -ENOENT)
5862 inode = NULL;
5863 else
5864 return ERR_CAST(inode);
5865 }
5866
Al Viro41d28bc2014-10-12 22:24:21 -04005867 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005868}
5869
Miao Xie16cdcec2011-04-22 18:12:22 +08005870unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005871 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5872};
5873
Al Viro9cdda8d2013-05-22 16:48:09 -04005874static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005875{
Al Viro9cdda8d2013-05-22 16:48:09 -04005876 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005879 struct btrfs_dir_item *di;
5880 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005881 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005882 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005883 struct list_head ins_list;
5884 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005886 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005887 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 unsigned char d_type;
5889 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005890 char tmp_name[32];
5891 char *name_ptr;
5892 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005893 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005894 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005895
Al Viro9cdda8d2013-05-22 16:48:09 -04005896 if (!dir_emit_dots(file, ctx))
5897 return 0;
5898
David Woodhouse49593bf2008-08-17 17:08:36 +01005899 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005900 if (!path)
5901 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005902
David Sterbae4058b52015-11-27 16:31:35 +01005903 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005904
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005905 INIT_LIST_HEAD(&ins_list);
5906 INIT_LIST_HEAD(&del_list);
5907 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005908
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005909 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005910 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005911 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005912
Chris Mason39279cc2007-06-12 06:35:45 -04005913 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5914 if (ret < 0)
5915 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005916
5917 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005918 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005919 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005920 if (slot >= btrfs_header_nritems(leaf)) {
5921 ret = btrfs_next_leaf(root, path);
5922 if (ret < 0)
5923 goto err;
5924 else if (ret > 0)
5925 break;
5926 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 }
Chris Mason3de45862008-11-17 21:02:50 -05005928
Chris Mason5f39d392007-10-15 16:14:19 -04005929 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5930
5931 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005932 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005933 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005934 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005935 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005936 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005937 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005938 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Al Viro9cdda8d2013-05-22 16:48:09 -04005940 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005941
Chris Mason39279cc2007-06-12 06:35:45 -04005942 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005943 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005944 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005945
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005946 name_len = btrfs_dir_name_len(leaf, di);
5947 if (name_len <= sizeof(tmp_name)) {
5948 name_ptr = tmp_name;
5949 } else {
5950 name_ptr = kmalloc(name_len, GFP_KERNEL);
5951 if (!name_ptr) {
5952 ret = -ENOMEM;
5953 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005954 }
Chris Mason39279cc2007-06-12 06:35:45 -04005955 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005956 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5957 name_len);
5958
5959 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5960 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5961
5962 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5963 d_type);
5964
5965 if (name_ptr != tmp_name)
5966 kfree(name_ptr);
5967
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005968 if (over)
5969 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005970 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005971next:
5972 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005974
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005975 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005976 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005977 goto nopos;
5978
Zach Browndb62efb2013-07-11 16:19:42 -07005979 /*
5980 * Stop new entries from being returned after we return the last
5981 * entry.
5982 *
5983 * New directory entries are assigned a strictly increasing
5984 * offset. This means that new entries created during readdir
5985 * are *guaranteed* to be seen in the future by that readdir.
5986 * This has broken buggy programs which operate on names as
5987 * they're returned by readdir. Until we re-use freed offsets
5988 * we have this hack to stop new entries from being returned
5989 * under the assumption that they'll never reach this huge
5990 * offset.
5991 *
5992 * This is being careful not to overflow 32bit loff_t unless the
5993 * last entry requires it because doing so has broken 32bit apps
5994 * in the past.
5995 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 if (ctx->pos >= INT_MAX)
5997 ctx->pos = LLONG_MAX;
5998 else
5999 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006000nopos:
6001 ret = 0;
6002err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006003 if (put)
6004 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006005 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006006 return ret;
6007}
6008
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006009int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006010{
6011 struct btrfs_root *root = BTRFS_I(inode)->root;
6012 struct btrfs_trans_handle *trans;
6013 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006014 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006015
Josef Bacik72ac3c02012-05-23 14:13:11 -04006016 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006017 return 0;
6018
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006019 if (btrfs_fs_closing(root->fs_info) &&
6020 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006021 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006022
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006023 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006024 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006025 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006026 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006027 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006028 if (IS_ERR(trans))
6029 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006030 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006031 }
6032 return ret;
6033}
6034
6035/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006036 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006037 * inode changes. But, it is most likely to find the inode in cache.
6038 * FIXME, needs more benchmarking...there are no reasons other than performance
6039 * to keep or drop this code.
6040 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006041static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006042{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006044 struct btrfs_root *root = BTRFS_I(inode)->root;
6045 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006046 int ret;
6047
Josef Bacik72ac3c02012-05-23 14:13:11 -04006048 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006049 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006050
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006051 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006052 if (IS_ERR(trans))
6053 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006054
6055 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006056 if (ret && ret == -ENOSPC) {
6057 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006058 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006059 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006060 if (IS_ERR(trans))
6061 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006062
Chris Mason94b60442010-05-26 11:02:00 -04006063 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006064 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006065 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006066 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006067 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006068
6069 return ret;
6070}
6071
6072/*
6073 * This is a copy of file_update_time. We need this so we can return error on
6074 * ENOSPC for updating the inode in the case of file write and mmap writes.
6075 */
Josef Bacike41f9412012-03-26 09:46:47 -04006076static int btrfs_update_time(struct inode *inode, struct timespec *now,
6077 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006078{
Alexander Block2bc5565282012-06-15 09:49:33 +02006079 struct btrfs_root *root = BTRFS_I(inode)->root;
6080
6081 if (btrfs_root_readonly(root))
6082 return -EROFS;
6083
Josef Bacike41f9412012-03-26 09:46:47 -04006084 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006085 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006086 if (flags & S_CTIME)
6087 inode->i_ctime = *now;
6088 if (flags & S_MTIME)
6089 inode->i_mtime = *now;
6090 if (flags & S_ATIME)
6091 inode->i_atime = *now;
6092 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006093}
6094
Chris Masond352ac62008-09-29 15:18:18 -04006095/*
6096 * find the highest existing sequence number in a directory
6097 * and then set the in-memory index_cnt variable to reflect
6098 * free sequence numbers
6099 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006100static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006101{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006102 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006103 struct btrfs_key key, found_key;
6104 struct btrfs_path *path;
6105 struct extent_buffer *leaf;
6106 int ret;
6107
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006108 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006109 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006110 key.offset = (u64)-1;
6111
6112 path = btrfs_alloc_path();
6113 if (!path)
6114 return -ENOMEM;
6115
6116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6117 if (ret < 0)
6118 goto out;
6119 /* FIXME: we should be able to handle this */
6120 if (ret == 0)
6121 goto out;
6122 ret = 0;
6123
6124 /*
6125 * MAGIC NUMBER EXPLANATION:
6126 * since we search a directory based on f_pos we have to start at 2
6127 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6128 * else has to start at 2
6129 */
6130 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006131 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006132 goto out;
6133 }
6134
6135 path->slots[0]--;
6136
6137 leaf = path->nodes[0];
6138 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6139
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006140 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006141 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006142 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006143 goto out;
6144 }
6145
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006146 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006147out:
6148 btrfs_free_path(path);
6149 return ret;
6150}
6151
Chris Masond352ac62008-09-29 15:18:18 -04006152/*
6153 * helper to find a free sequence number in a given directory. This current
6154 * code is very simple, later versions will do smarter things in the btree
6155 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006156int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006157{
6158 int ret = 0;
6159
Nikolay Borisov877574e2017-02-20 13:50:33 +02006160 if (dir->index_cnt == (u64)-1) {
6161 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006162 if (ret) {
6163 ret = btrfs_set_inode_index_count(dir);
6164 if (ret)
6165 return ret;
6166 }
Josef Bacikaec74772008-07-24 12:12:38 -04006167 }
6168
Nikolay Borisov877574e2017-02-20 13:50:33 +02006169 *index = dir->index_cnt;
6170 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006171
6172 return ret;
6173}
6174
Chris Masonb0d5d102014-09-08 13:08:51 -07006175static int btrfs_insert_inode_locked(struct inode *inode)
6176{
6177 struct btrfs_iget_args args;
6178 args.location = &BTRFS_I(inode)->location;
6179 args.root = BTRFS_I(inode)->root;
6180
6181 return insert_inode_locked4(inode,
6182 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6183 btrfs_find_actor, &args);
6184}
6185
Chris Mason39279cc2007-06-12 06:35:45 -04006186static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6187 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006188 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006189 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006190 u64 ref_objectid, u64 objectid,
6191 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006193 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006194 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006195 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006196 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006197 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006198 struct btrfs_inode_ref *ref;
6199 struct btrfs_key key[2];
6200 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006201 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006202 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006203 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006204
Chris Mason5f39d392007-10-15 16:14:19 -04006205 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006206 if (!path)
6207 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006209 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006210 if (!inode) {
6211 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006212 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006213 }
Chris Mason39279cc2007-06-12 06:35:45 -04006214
Li Zefan581bb052011-04-20 10:06:11 +08006215 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006216 * O_TMPFILE, set link count to 0, so that after this point,
6217 * we fill in an inode item with the correct link count.
6218 */
6219 if (!name)
6220 set_nlink(inode, 0);
6221
6222 /*
Li Zefan581bb052011-04-20 10:06:11 +08006223 * we have to initialize this early, so we can reclaim the inode
6224 * number if we fail afterwards in this function.
6225 */
6226 inode->i_ino = objectid;
6227
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006228 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006229 trace_btrfs_inode_request(dir);
6230
Nikolay Borisov877574e2017-02-20 13:50:33 +02006231 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006232 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006233 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006234 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006235 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006236 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006237 } else if (dir) {
6238 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006239 }
6240 /*
6241 * index_cnt is ignored for everything but a dir,
6242 * btrfs_get_inode_index_count has an explanation for the magic
6243 * number
6244 */
6245 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006246 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006247 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006248 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006249 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006250
Josef Bacik5dc562c2012-08-17 13:14:17 -04006251 /*
6252 * We could have gotten an inode number from somebody who was fsynced
6253 * and then removed in this same transaction, so let's just set full
6254 * sync since it will be a full sync anyway and this will blow away the
6255 * old info in the log.
6256 */
6257 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6258
Chris Mason9c583092008-01-29 15:15:18 -05006259 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006260 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006261 key[0].offset = 0;
6262
Chris Mason9c583092008-01-29 15:15:18 -05006263 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006264
6265 if (name) {
6266 /*
6267 * Start new inodes with an inode_ref. This is slightly more
6268 * efficient for small numbers of hard links since they will
6269 * be packed into one item. Extended refs will kick in if we
6270 * add more hard links than can fit in the ref item.
6271 */
6272 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006273 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006274 key[1].offset = ref_objectid;
6275
6276 sizes[1] = name_len + sizeof(*ref);
6277 }
Chris Mason9c583092008-01-29 15:15:18 -05006278
Chris Masonb0d5d102014-09-08 13:08:51 -07006279 location = &BTRFS_I(inode)->location;
6280 location->objectid = objectid;
6281 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006282 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006283
6284 ret = btrfs_insert_inode_locked(inode);
6285 if (ret < 0)
6286 goto fail;
6287
Chris Masonb9473432009-03-13 11:00:37 -04006288 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006289 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006290 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006291 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006292
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006293 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006294 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306295
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006296 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306297 inode->i_atime = inode->i_mtime;
6298 inode->i_ctime = inode->i_mtime;
6299 BTRFS_I(inode)->i_otime = inode->i_mtime;
6300
Chris Mason5f39d392007-10-15 16:14:19 -04006301 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6302 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006303 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006304 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006305 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006306
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006307 if (name) {
6308 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6309 struct btrfs_inode_ref);
6310 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6311 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6312 ptr = (unsigned long)(ref + 1);
6313 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6314 }
Chris Mason9c583092008-01-29 15:15:18 -05006315
Chris Mason5f39d392007-10-15 16:14:19 -04006316 btrfs_mark_buffer_dirty(path->nodes[0]);
6317 btrfs_free_path(path);
6318
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006319 btrfs_inherit_iflags(inode, dir);
6320
Al Viro569254b2011-07-24 17:08:40 -04006321 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006322 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006323 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006324 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006325 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6326 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006327 }
6328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006329 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006330
6331 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006332 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006333
Alexander Block8ea05e32012-07-25 17:35:53 +02006334 btrfs_update_root_times(trans, root);
6335
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006336 ret = btrfs_inode_inherit_props(trans, inode, dir);
6337 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006338 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006339 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006340 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006341
Chris Mason39279cc2007-06-12 06:35:45 -04006342 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006343
6344fail_unlock:
6345 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006346fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006347 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006348 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006349 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006350 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006351 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006352}
6353
6354static inline u8 btrfs_inode_type(struct inode *inode)
6355{
6356 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6357}
6358
Chris Masond352ac62008-09-29 15:18:18 -04006359/*
6360 * utility function to add 'inode' into 'parent_inode' with
6361 * a give name and a given sequence number.
6362 * if 'add_backref' is true, also insert a backref from the
6363 * inode to the parent directory.
6364 */
Chris Masone02119d2008-09-05 16:13:11 -04006365int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006366 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006367 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006368{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006369 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006370 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006371 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006372 struct btrfs_root *root = parent_inode->root;
6373 u64 ino = btrfs_ino(inode);
6374 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006375
Li Zefan33345d012011-04-20 10:31:50 +08006376 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006377 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006378 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006379 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006380 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006381 key.offset = 0;
6382 }
Chris Mason39279cc2007-06-12 06:35:45 -04006383
Li Zefan33345d012011-04-20 10:31:50 +08006384 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006385 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6386 root->root_key.objectid, parent_ino,
6387 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006388 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006389 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6390 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006391 }
6392
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006393 /* Nothing to clean up yet */
6394 if (ret)
6395 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006396
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006397 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6398 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006399 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006400 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006401 goto fail_dir_item;
6402 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006403 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006404 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006405 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006406
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006407 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006408 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006409 inode_inc_iversion(&parent_inode->vfs_inode);
6410 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6411 current_time(&parent_inode->vfs_inode);
6412 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006413 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006414 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006415 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006416
6417fail_dir_item:
6418 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6419 u64 local_index;
6420 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006421 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6422 root->root_key.objectid, parent_ino,
6423 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006424
6425 } else if (add_backref) {
6426 u64 local_index;
6427 int err;
6428
6429 err = btrfs_del_inode_ref(trans, root, name, name_len,
6430 ino, parent_ino, &local_index);
6431 }
6432 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006433}
6434
6435static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006436 struct btrfs_inode *dir, struct dentry *dentry,
6437 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006438{
Josef Bacika1b075d2010-11-19 20:36:11 +00006439 int err = btrfs_add_link(trans, dir, inode,
6440 dentry->d_name.name, dentry->d_name.len,
6441 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006442 if (err > 0)
6443 err = -EEXIST;
6444 return err;
6445}
6446
Josef Bacik618e21d2007-07-11 10:18:17 -04006447static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006448 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006449{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006450 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006451 struct btrfs_trans_handle *trans;
6452 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006453 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006454 int err;
6455 int drop_inode = 0;
6456 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006457 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006458
Josef Bacik9ed74f22009-09-11 16:12:44 -04006459 /*
6460 * 2 for inode item and ref
6461 * 2 for dir items
6462 * 1 for xattr if selinux is on
6463 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006464 trans = btrfs_start_transaction(root, 5);
6465 if (IS_ERR(trans))
6466 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006467
Li Zefan581bb052011-04-20 10:06:11 +08006468 err = btrfs_find_free_ino(root, &objectid);
6469 if (err)
6470 goto out_unlock;
6471
Josef Bacikaec74772008-07-24 12:12:38 -04006472 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006473 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6474 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006475 if (IS_ERR(inode)) {
6476 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006477 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006478 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006479
Casey Schauflerad19db72011-12-15 10:09:07 -05006480 /*
6481 * If the active LSM wants to access the inode during
6482 * d_instantiate it needs these. Smack checks to see
6483 * if the filesystem supports xattrs by looking at the
6484 * ops vector.
6485 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006486 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006487 init_special_inode(inode, inode->i_mode, rdev);
6488
6489 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006490 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006491 goto out_unlock_inode;
6492
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006493 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6494 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006495 if (err) {
6496 goto out_unlock_inode;
6497 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006498 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006499 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006500 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006501 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006502
Josef Bacik618e21d2007-07-11 10:18:17 -04006503out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006504 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006505 btrfs_balance_delayed_items(fs_info);
6506 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006507 if (drop_inode) {
6508 inode_dec_link_count(inode);
6509 iput(inode);
6510 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006511 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006512
6513out_unlock_inode:
6514 drop_inode = 1;
6515 unlock_new_inode(inode);
6516 goto out_unlock;
6517
Josef Bacik618e21d2007-07-11 10:18:17 -04006518}
6519
Chris Mason39279cc2007-06-12 06:35:45 -04006520static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006521 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006522{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006523 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006524 struct btrfs_trans_handle *trans;
6525 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006526 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006527 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006528 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006529 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006530 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006531
Josef Bacik9ed74f22009-09-11 16:12:44 -04006532 /*
6533 * 2 for inode item and ref
6534 * 2 for dir items
6535 * 1 for xattr if selinux is on
6536 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006537 trans = btrfs_start_transaction(root, 5);
6538 if (IS_ERR(trans))
6539 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006540
Li Zefan581bb052011-04-20 10:06:11 +08006541 err = btrfs_find_free_ino(root, &objectid);
6542 if (err)
6543 goto out_unlock;
6544
Josef Bacikaec74772008-07-24 12:12:38 -04006545 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006546 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6547 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006548 if (IS_ERR(inode)) {
6549 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006550 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006551 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006552 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006553 /*
6554 * If the active LSM wants to access the inode during
6555 * d_instantiate it needs these. Smack checks to see
6556 * if the filesystem supports xattrs by looking at the
6557 * ops vector.
6558 */
6559 inode->i_fop = &btrfs_file_operations;
6560 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006561 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006562
6563 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6564 if (err)
6565 goto out_unlock_inode;
6566
6567 err = btrfs_update_inode(trans, root, inode);
6568 if (err)
6569 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006570
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006571 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6572 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006574 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006575
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006576 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006577 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006578 d_instantiate(dentry, inode);
6579
Chris Mason39279cc2007-06-12 06:35:45 -04006580out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006581 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006582 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006583 inode_dec_link_count(inode);
6584 iput(inode);
6585 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006586 btrfs_balance_delayed_items(fs_info);
6587 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006588 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006589
6590out_unlock_inode:
6591 unlock_new_inode(inode);
6592 goto out_unlock;
6593
Chris Mason39279cc2007-06-12 06:35:45 -04006594}
6595
6596static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6597 struct dentry *dentry)
6598{
Filipe Manana271dba452016-01-05 16:24:05 +00006599 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006600 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006601 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006603 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006604 int err;
6605 int drop_inode = 0;
6606
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006607 /* do not allow sys_link's with other subvols of the same device */
6608 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006609 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006610
Mark Fashehf1863732012-08-08 11:32:27 -07006611 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006612 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006613
Nikolay Borisov877574e2017-02-20 13:50:33 +02006614 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006615 if (err)
6616 goto fail;
6617
Yan, Zhenga22285a2010-05-16 10:48:46 -04006618 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006619 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006620 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006621 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006622 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006623 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006624 if (IS_ERR(trans)) {
6625 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006626 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006627 goto fail;
6628 }
Chris Mason5f39d392007-10-15 16:14:19 -04006629
Miao Xie67de1172013-12-26 13:07:06 +08006630 /* There are several dir indexes for this inode, clear the cache. */
6631 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006632 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006633 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006634 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006635 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006636 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006637
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006638 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6639 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006640
Yan, Zhenga5719522009-09-24 09:17:31 -04006641 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006642 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006643 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006644 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006645 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006646 if (err)
6647 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006648 if (inode->i_nlink == 1) {
6649 /*
6650 * If new hard link count is 1, it's a file created
6651 * with open(2) O_TMPFILE flag.
6652 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006653 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006654 if (err)
6655 goto fail;
6656 }
Al Viro08c422c2011-12-23 07:58:13 -05006657 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006658 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006659 }
Chris Mason39279cc2007-06-12 06:35:45 -04006660
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006661 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006662fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006663 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006664 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 if (drop_inode) {
6666 inode_dec_link_count(inode);
6667 iput(inode);
6668 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006669 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006670 return err;
6671}
6672
Al Viro18bb1db2011-07-26 01:41:39 -04006673static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006674{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006675 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006676 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006677 struct btrfs_trans_handle *trans;
6678 struct btrfs_root *root = BTRFS_I(dir)->root;
6679 int err = 0;
6680 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006681 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006682 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006683
Josef Bacik9ed74f22009-09-11 16:12:44 -04006684 /*
6685 * 2 items for inode and ref
6686 * 2 items for dir items
6687 * 1 for xattr if selinux is on
6688 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006689 trans = btrfs_start_transaction(root, 5);
6690 if (IS_ERR(trans))
6691 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006692
Li Zefan581bb052011-04-20 10:06:11 +08006693 err = btrfs_find_free_ino(root, &objectid);
6694 if (err)
6695 goto out_fail;
6696
Josef Bacikaec74772008-07-24 12:12:38 -04006697 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006698 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6699 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006700 if (IS_ERR(inode)) {
6701 err = PTR_ERR(inode);
6702 goto out_fail;
6703 }
Chris Mason5f39d392007-10-15 16:14:19 -04006704
Chris Mason39279cc2007-06-12 06:35:45 -04006705 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006706 /* these must be set before we unlock the inode */
6707 inode->i_op = &btrfs_dir_inode_operations;
6708 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006709
Eric Paris2a7dba32011-02-01 11:05:39 -05006710 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006711 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006712 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006713
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006714 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006715 err = btrfs_update_inode(trans, root, inode);
6716 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006717 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006718
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006719 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6720 dentry->d_name.name,
6721 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006722 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006723 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006724
Chris Mason39279cc2007-06-12 06:35:45 -04006725 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006726 /*
6727 * mkdir is special. We're unlocking after we call d_instantiate
6728 * to avoid a race with nfsd calling d_instantiate.
6729 */
6730 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006731 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006732
6733out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006734 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006735 if (drop_on_err) {
6736 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006737 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006738 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006739 btrfs_balance_delayed_items(fs_info);
6740 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006741 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006742
6743out_fail_inode:
6744 unlock_new_inode(inode);
6745 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006746}
6747
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006748/* Find next extent map of a given extent map, caller needs to ensure locks */
6749static struct extent_map *next_extent_map(struct extent_map *em)
6750{
6751 struct rb_node *next;
6752
6753 next = rb_next(&em->rb_node);
6754 if (!next)
6755 return NULL;
6756 return container_of(next, struct extent_map, rb_node);
6757}
6758
6759static struct extent_map *prev_extent_map(struct extent_map *em)
6760{
6761 struct rb_node *prev;
6762
6763 prev = rb_prev(&em->rb_node);
6764 if (!prev)
6765 return NULL;
6766 return container_of(prev, struct extent_map, rb_node);
6767}
6768
Chris Masond352ac62008-09-29 15:18:18 -04006769/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006770 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006771 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006772 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006773 */
Chris Mason3b951512008-04-17 11:29:12 -04006774static int merge_extent_mapping(struct extent_map_tree *em_tree,
6775 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006776 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006777 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006778{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006779 struct extent_map *prev;
6780 struct extent_map *next;
6781 u64 start;
6782 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006783 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006784
Chris Masone6dcd2d2008-07-17 12:53:50 -04006785 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006786
6787 if (existing->start > map_start) {
6788 next = existing;
6789 prev = prev_extent_map(next);
6790 } else {
6791 prev = existing;
6792 next = next_extent_map(prev);
6793 }
6794
6795 start = prev ? extent_map_end(prev) : em->start;
6796 start = max_t(u64, start, em->start);
6797 end = next ? next->start : extent_map_end(em);
6798 end = min_t(u64, end, extent_map_end(em));
6799 start_diff = start - em->start;
6800 em->start = start;
6801 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006802 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6803 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006804 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006805 em->block_len -= start_diff;
6806 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006807 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006808}
6809
Chris Masonc8b97812008-10-29 14:49:59 -04006810static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006811 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006812 size_t pg_offset, u64 extent_offset,
6813 struct btrfs_file_extent_item *item)
6814{
6815 int ret;
6816 struct extent_buffer *leaf = path->nodes[0];
6817 char *tmp;
6818 size_t max_size;
6819 unsigned long inline_size;
6820 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006821 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006822
6823 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006824 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006825 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6826 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006827 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006828 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006829 if (!tmp)
6830 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006831 ptr = btrfs_file_extent_inline_start(item);
6832
6833 read_extent_buffer(leaf, tmp, ptr, inline_size);
6834
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006835 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006836 ret = btrfs_decompress(compress_type, tmp, page,
6837 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006838
6839 /*
6840 * decompression code contains a memset to fill in any space between the end
6841 * of the uncompressed data and the end of max_size in case the decompressed
6842 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6843 * the end of an inline extent and the beginning of the next block, so we
6844 * cover that region here.
6845 */
6846
6847 if (max_size + pg_offset < PAGE_SIZE) {
6848 char *map = kmap(page);
6849 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6850 kunmap(page);
6851 }
Chris Masonc8b97812008-10-29 14:49:59 -04006852 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006853 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006854}
6855
Chris Masond352ac62008-09-29 15:18:18 -04006856/*
6857 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006858 * the ugly parts come from merging extents from the disk with the in-ram
6859 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006860 * where the in-ram extents might be locked pending data=ordered completion.
6861 *
6862 * This also copies inline extents directly into the page.
6863 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006864struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6865 struct page *page,
6866 size_t pg_offset, u64 start, u64 len,
6867 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006868{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006869 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006870 int ret;
6871 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006872 u64 extent_start = 0;
6873 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006874 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006875 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006876 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006877 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006878 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006879 struct extent_buffer *leaf;
6880 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006881 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006882 struct extent_map_tree *em_tree = &inode->extent_tree;
6883 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006885 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006886
Chris Masona52d9a82007-08-27 16:49:44 -04006887again:
Chris Mason890871b2009-09-02 16:24:52 -04006888 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006889 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006890 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006891 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006892 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006893
Chris Masona52d9a82007-08-27 16:49:44 -04006894 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006895 if (em->start > start || em->start + em->len <= start)
6896 free_extent_map(em);
6897 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006898 free_extent_map(em);
6899 else
6900 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006901 }
David Sterba172ddd62011-04-21 00:48:27 +02006902 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006903 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006904 err = -ENOMEM;
6905 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006907 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006908 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006909 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006910 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006911 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006912
6913 if (!path) {
6914 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006915 if (!path) {
6916 err = -ENOMEM;
6917 goto out;
6918 }
6919 /*
6920 * Chances are we'll be called again, so go ahead and do
6921 * readahead
6922 */
David Sterbae4058b52015-11-27 16:31:35 +01006923 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006924 }
6925
Chris Mason179e29e2007-11-01 11:28:41 -04006926 ret = btrfs_lookup_file_extent(trans, root, path,
6927 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006928 if (ret < 0) {
6929 err = ret;
6930 goto out;
6931 }
6932
6933 if (ret != 0) {
6934 if (path->slots[0] == 0)
6935 goto not_found;
6936 path->slots[0]--;
6937 }
6938
Chris Mason5f39d392007-10-15 16:14:19 -04006939 leaf = path->nodes[0];
6940 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006941 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006942 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006943 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006944 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006945 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006946 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006947 /*
6948 * If we backup past the first extent we want to move forward
6949 * and see if there is an extent in front of us, otherwise we'll
6950 * say there is a hole for our whole search range which can
6951 * cause problems.
6952 */
6953 extent_end = start;
6954 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006955 }
6956
Chris Mason5f39d392007-10-15 16:14:19 -04006957 found_type = btrfs_file_extent_type(leaf, item);
6958 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006959 if (found_type == BTRFS_FILE_EXTENT_REG ||
6960 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006961 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006962 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006963
6964 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6965 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006966 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6967 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006968 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006969 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006970 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006971
6972 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6973 path->slots[0],
6974 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006975 }
Josef Bacik25a50342013-10-14 12:08:38 -04006976next:
Yan Zheng9036c102008-10-30 14:19:41 -04006977 if (start >= extent_end) {
6978 path->slots[0]++;
6979 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6980 ret = btrfs_next_leaf(root, path);
6981 if (ret < 0) {
6982 err = ret;
6983 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006984 }
Yan Zheng9036c102008-10-30 14:19:41 -04006985 if (ret > 0)
6986 goto not_found;
6987 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006988 }
Yan Zheng9036c102008-10-30 14:19:41 -04006989 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6990 if (found_key.objectid != objectid ||
6991 found_key.type != BTRFS_EXTENT_DATA_KEY)
6992 goto not_found;
6993 if (start + len <= found_key.offset)
6994 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006995 if (start > found_key.offset)
6996 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006997 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006998 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006999 em->len = found_key.offset - start;
7000 goto not_found_em;
7001 }
7002
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007003 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007004 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007005
Yan Zhengd899e052008-10-30 14:25:28 -04007006 if (found_type == BTRFS_FILE_EXTENT_REG ||
7007 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007008 goto insert;
7009 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007010 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007011 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007012 size_t size;
7013 size_t extent_offset;
7014 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007015
Filipe Manana7ffbb592014-06-09 03:48:05 +01007016 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007017 goto out;
Yan689f9342007-10-29 11:41:07 -04007018
Chris Mason514ac8a2014-01-03 21:07:00 -08007019 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007020 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007021 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7022 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007023 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007024 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007025 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007026 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007027 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007028 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007029 if (btrfs_file_extent_compression(leaf, item) !=
7030 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007031 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007032 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007033 if (ret) {
7034 err = ret;
7035 goto out;
7036 }
Chris Masonc8b97812008-10-29 14:49:59 -04007037 } else {
7038 map = kmap(page);
7039 read_extent_buffer(leaf, map + pg_offset, ptr,
7040 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007041 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007042 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007043 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007044 copy_size);
7045 }
Chris Masonc8b97812008-10-29 14:49:59 -04007046 kunmap(page);
7047 }
Chris Mason179e29e2007-11-01 11:28:41 -04007048 flush_dcache_page(page);
7049 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007050 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007051 if (!trans) {
7052 kunmap(page);
7053 free_extent_map(em);
7054 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007055
David Sterbab3b4aa72011-04-21 01:20:15 +02007056 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007057 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007058
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007059 if (IS_ERR(trans))
7060 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007061 goto again;
7062 }
Chris Masonc8b97812008-10-29 14:49:59 -04007063 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007064 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007065 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007066 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007067 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007068 }
Chris Masond1310b22008-01-24 16:13:08 -05007069 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007070 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007071 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007072 }
7073not_found:
7074 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007075 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007076 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007077not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007078 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007079 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007080insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007081 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007082 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007083 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007084 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7085 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007086 err = -EIO;
7087 goto out;
7088 }
Chris Masond1310b22008-01-24 16:13:08 -05007089
7090 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007091 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007092 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007093 /* it is possible that someone inserted the extent into the tree
7094 * while we had the lock dropped. It is also possible that
7095 * an overlapping map exists in the tree
7096 */
Chris Masona52d9a82007-08-27 16:49:44 -04007097 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007098 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007099
7100 ret = 0;
7101
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007102 existing = search_extent_mapping(em_tree, start, len);
7103 /*
7104 * existing will always be non-NULL, since there must be
7105 * extent causing the -EEXIST.
7106 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007107 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007108 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007109 em->block_start == existing->block_start) {
7110 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007111 * The existing extent map already encompasses the
7112 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007113 */
7114 free_extent_map(em);
7115 em = existing;
7116 err = 0;
7117
7118 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007119 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007120 /*
7121 * The existing extent map is the one nearest to
7122 * the [start, start + len) range which overlaps
7123 */
7124 err = merge_extent_mapping(em_tree, existing,
7125 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007126 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007127 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007128 free_extent_map(em);
7129 em = NULL;
7130 }
7131 } else {
7132 free_extent_map(em);
7133 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007134 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007135 }
Chris Masona52d9a82007-08-27 16:49:44 -04007136 }
Chris Mason890871b2009-09-02 16:24:52 -04007137 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007138out:
liubo1abe9b82011-03-24 11:18:59 +00007139
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007140 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007141
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007142 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007143 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007144 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007145 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007146 err = ret;
7147 }
Chris Masona52d9a82007-08-27 16:49:44 -04007148 if (err) {
7149 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007150 return ERR_PTR(err);
7151 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007152 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007153 return em;
7154}
7155
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007156struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7157 struct page *page,
7158 size_t pg_offset, u64 start, u64 len,
7159 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007160{
7161 struct extent_map *em;
7162 struct extent_map *hole_em = NULL;
7163 u64 range_start = start;
7164 u64 end;
7165 u64 found;
7166 u64 found_end;
7167 int err = 0;
7168
7169 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7170 if (IS_ERR(em))
7171 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007172 /*
7173 * If our em maps to:
7174 * - a hole or
7175 * - a pre-alloc extent,
7176 * there might actually be delalloc bytes behind it.
7177 */
7178 if (em->block_start != EXTENT_MAP_HOLE &&
7179 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7180 return em;
7181 else
7182 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007183
7184 /* check to see if we've wrapped (len == -1 or similar) */
7185 end = start + len;
7186 if (end < start)
7187 end = (u64)-1;
7188 else
7189 end -= 1;
7190
7191 em = NULL;
7192
7193 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007194 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007195 end, len, EXTENT_DELALLOC, 1);
7196 found_end = range_start + found;
7197 if (found_end < range_start)
7198 found_end = (u64)-1;
7199
7200 /*
7201 * we didn't find anything useful, return
7202 * the original results from get_extent()
7203 */
7204 if (range_start > end || found_end <= start) {
7205 em = hole_em;
7206 hole_em = NULL;
7207 goto out;
7208 }
7209
7210 /* adjust the range_start to make sure it doesn't
7211 * go backwards from the start they passed in
7212 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307213 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007214 found = found_end - range_start;
7215
7216 if (found > 0) {
7217 u64 hole_start = start;
7218 u64 hole_len = len;
7219
David Sterba172ddd62011-04-21 00:48:27 +02007220 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007221 if (!em) {
7222 err = -ENOMEM;
7223 goto out;
7224 }
7225 /*
7226 * when btrfs_get_extent can't find anything it
7227 * returns one huge hole
7228 *
7229 * make sure what it found really fits our range, and
7230 * adjust to make sure it is based on the start from
7231 * the caller
7232 */
7233 if (hole_em) {
7234 u64 calc_end = extent_map_end(hole_em);
7235
7236 if (calc_end <= start || (hole_em->start > end)) {
7237 free_extent_map(hole_em);
7238 hole_em = NULL;
7239 } else {
7240 hole_start = max(hole_em->start, start);
7241 hole_len = calc_end - hole_start;
7242 }
7243 }
7244 em->bdev = NULL;
7245 if (hole_em && range_start > hole_start) {
7246 /* our hole starts before our delalloc, so we
7247 * have to return just the parts of the hole
7248 * that go until the delalloc starts
7249 */
7250 em->len = min(hole_len,
7251 range_start - hole_start);
7252 em->start = hole_start;
7253 em->orig_start = hole_start;
7254 /*
7255 * don't adjust block start at all,
7256 * it is fixed at EXTENT_MAP_HOLE
7257 */
7258 em->block_start = hole_em->block_start;
7259 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007260 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7261 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007262 } else {
7263 em->start = range_start;
7264 em->len = found;
7265 em->orig_start = range_start;
7266 em->block_start = EXTENT_MAP_DELALLOC;
7267 em->block_len = found;
7268 }
7269 } else if (hole_em) {
7270 return hole_em;
7271 }
7272out:
7273
7274 free_extent_map(hole_em);
7275 if (err) {
7276 free_extent_map(em);
7277 return ERR_PTR(err);
7278 }
7279 return em;
7280}
7281
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007282static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7283 const u64 start,
7284 const u64 len,
7285 const u64 orig_start,
7286 const u64 block_start,
7287 const u64 block_len,
7288 const u64 orig_block_len,
7289 const u64 ram_bytes,
7290 const int type)
7291{
7292 struct extent_map *em = NULL;
7293 int ret;
7294
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007296 em = create_io_em(inode, start, len, orig_start,
7297 block_start, block_len, orig_block_len,
7298 ram_bytes,
7299 BTRFS_COMPRESS_NONE, /* compress_type */
7300 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007301 if (IS_ERR(em))
7302 goto out;
7303 }
7304 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7305 len, block_len, type);
7306 if (ret) {
7307 if (em) {
7308 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007309 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007310 start + len - 1, 0);
7311 }
7312 em = ERR_PTR(ret);
7313 }
7314 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007315
7316 return em;
7317}
7318
Josef Bacik4b46fce2010-05-23 11:00:55 -04007319static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7320 u64 start, u64 len)
7321{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007324 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325 struct btrfs_key ins;
7326 u64 alloc_hint;
7327 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328
Josef Bacik4b46fce2010-05-23 11:00:55 -04007329 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007330 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007331 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007332 if (ret)
7333 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007334
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007335 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7336 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007337 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007338 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007339 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007340 btrfs_free_reserved_extent(fs_info, ins.objectid,
7341 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007342
Josef Bacik4b46fce2010-05-23 11:00:55 -04007343 return em;
7344}
7345
Chris Mason46bfbb52010-05-26 11:04:10 -04007346/*
7347 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7348 * block must be cow'd
7349 */
Josef Bacik00361582013-08-14 14:02:47 -04007350noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007351 u64 *orig_start, u64 *orig_block_len,
7352 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007353{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007354 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007355 struct btrfs_path *path;
7356 int ret;
7357 struct extent_buffer *leaf;
7358 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007359 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007360 struct btrfs_file_extent_item *fi;
7361 struct btrfs_key key;
7362 u64 disk_bytenr;
7363 u64 backref_offset;
7364 u64 extent_end;
7365 u64 num_bytes;
7366 int slot;
7367 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007368 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007369
Chris Mason46bfbb52010-05-26 11:04:10 -04007370 path = btrfs_alloc_path();
7371 if (!path)
7372 return -ENOMEM;
7373
David Sterbaf85b7372017-01-20 14:54:07 +01007374 ret = btrfs_lookup_file_extent(NULL, root, path,
7375 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007376 if (ret < 0)
7377 goto out;
7378
7379 slot = path->slots[0];
7380 if (ret == 1) {
7381 if (slot == 0) {
7382 /* can't find the item, must cow */
7383 ret = 0;
7384 goto out;
7385 }
7386 slot--;
7387 }
7388 ret = 0;
7389 leaf = path->nodes[0];
7390 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007391 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 key.type != BTRFS_EXTENT_DATA_KEY) {
7393 /* not our file or wrong item type, must cow */
7394 goto out;
7395 }
7396
7397 if (key.offset > offset) {
7398 /* Wrong offset, must cow */
7399 goto out;
7400 }
7401
7402 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7403 found_type = btrfs_file_extent_type(leaf, fi);
7404 if (found_type != BTRFS_FILE_EXTENT_REG &&
7405 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7406 /* not a regular extent, must cow */
7407 goto out;
7408 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007409
7410 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7411 goto out;
7412
Miao Xiee77751a2013-12-27 21:11:50 +08007413 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7414 if (extent_end <= offset)
7415 goto out;
7416
Chris Mason46bfbb52010-05-26 11:04:10 -04007417 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007418 if (disk_bytenr == 0)
7419 goto out;
7420
7421 if (btrfs_file_extent_compression(leaf, fi) ||
7422 btrfs_file_extent_encryption(leaf, fi) ||
7423 btrfs_file_extent_other_encoding(leaf, fi))
7424 goto out;
7425
Chris Mason46bfbb52010-05-26 11:04:10 -04007426 backref_offset = btrfs_file_extent_offset(leaf, fi);
7427
Josef Bacik7ee9e442013-06-21 16:37:03 -04007428 if (orig_start) {
7429 *orig_start = key.offset - backref_offset;
7430 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7431 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7432 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007433
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007434 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007435 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007436
7437 num_bytes = min(offset + *len, extent_end) - offset;
7438 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7439 u64 range_end;
7440
Jeff Mahoneyda170662016-06-15 09:22:56 -04007441 range_end = round_up(offset + num_bytes,
7442 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007443 ret = test_range_bit(io_tree, offset, range_end,
7444 EXTENT_DELALLOC, 0, NULL);
7445 if (ret) {
7446 ret = -EAGAIN;
7447 goto out;
7448 }
7449 }
7450
Josef Bacik1bda19e2013-10-18 12:10:36 -04007451 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007452
7453 /*
7454 * look for other files referencing this extent, if we
7455 * find any we must cow
7456 */
Josef Bacik00361582013-08-14 14:02:47 -04007457
Liu Boe4c3b2d2017-01-30 12:25:28 -08007458 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007459 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007460 if (ret) {
7461 ret = 0;
7462 goto out;
7463 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007464
7465 /*
7466 * adjust disk_bytenr and num_bytes to cover just the bytes
7467 * in this extent we are about to write. If there
7468 * are any csums in that range we have to cow in order
7469 * to keep the csums correct
7470 */
7471 disk_bytenr += backref_offset;
7472 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007473 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7474 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007475 /*
7476 * all of the above have passed, it is safe to overwrite this extent
7477 * without cow
7478 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007479 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007480 ret = 1;
7481out:
7482 btrfs_free_path(path);
7483 return ret;
7484}
7485
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007486bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7487{
7488 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007489 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007490 void **pagep = NULL;
7491 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007492 unsigned long start_idx;
7493 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007494
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007495 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007496
7497 /*
7498 * end is the last byte in the last page. end == start is legal
7499 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007500 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007501
7502 rcu_read_lock();
7503
7504 /* Most of the code in this while loop is lifted from
7505 * find_get_page. It's been modified to begin searching from a
7506 * page and return just the first page found in that range. If the
7507 * found idx is less than or equal to the end idx then we know that
7508 * a page exists. If no pages are found or if those pages are
7509 * outside of the range then we're fine (yay!) */
7510 while (page == NULL &&
7511 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7512 page = radix_tree_deref_slot(pagep);
7513 if (unlikely(!page))
7514 break;
7515
7516 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007517 if (radix_tree_deref_retry(page)) {
7518 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007519 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007520 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007521 /*
7522 * Otherwise, shmem/tmpfs must be storing a swap entry
7523 * here as an exceptional entry: so return it without
7524 * attempting to raise page count.
7525 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007526 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007527 break; /* TODO: Is this relevant for this use case? */
7528 }
7529
Filipe Manana91405152014-06-05 13:22:24 +01007530 if (!page_cache_get_speculative(page)) {
7531 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007532 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007533 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007534
7535 /*
7536 * Has the page moved?
7537 * This is part of the lockless pagecache protocol. See
7538 * include/linux/pagemap.h for details.
7539 */
7540 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007541 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007542 page = NULL;
7543 }
7544 }
7545
7546 if (page) {
7547 if (page->index <= end_idx)
7548 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007549 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007550 }
7551
7552 rcu_read_unlock();
7553 return found;
7554}
7555
Josef Bacikeb838e72012-07-31 16:28:48 -04007556static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7557 struct extent_state **cached_state, int writing)
7558{
7559 struct btrfs_ordered_extent *ordered;
7560 int ret = 0;
7561
7562 while (1) {
7563 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007564 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 /*
7566 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007567 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 * extents in this range.
7569 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007570 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007571 lockend - lockstart + 1);
7572
7573 /*
7574 * We need to make sure there are no buffered pages in this
7575 * range either, we could have raced between the invalidate in
7576 * generic_file_direct_write and locking the extent. The
7577 * invalidate needs to happen so that reads after a write do not
7578 * get stale data.
7579 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007580 if (!ordered &&
7581 (!writing ||
7582 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 break;
7584
7585 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7586 cached_state, GFP_NOFS);
7587
7588 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007589 /*
7590 * If we are doing a DIO read and the ordered extent we
7591 * found is for a buffered write, we can not wait for it
7592 * to complete and retry, because if we do so we can
7593 * deadlock with concurrent buffered writes on page
7594 * locks. This happens only if our DIO read covers more
7595 * than one extent map, if at this point has already
7596 * created an ordered extent for a previous extent map
7597 * and locked its range in the inode's io tree, and a
7598 * concurrent write against that previous extent map's
7599 * range and this range started (we unlock the ranges
7600 * in the io tree only when the bios complete and
7601 * buffered writes always lock pages before attempting
7602 * to lock range in the io tree).
7603 */
7604 if (writing ||
7605 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7606 btrfs_start_ordered_extent(inode, ordered, 1);
7607 else
7608 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 btrfs_put_ordered_extent(ordered);
7610 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007612 * We could trigger writeback for this range (and wait
7613 * for it to complete) and then invalidate the pages for
7614 * this range (through invalidate_inode_pages2_range()),
7615 * but that can lead us to a deadlock with a concurrent
7616 * call to readpages() (a buffered read or a defrag call
7617 * triggered a readahead) on a page lock due to an
7618 * ordered dio extent we created before but did not have
7619 * yet a corresponding bio submitted (whence it can not
7620 * complete), which makes readpages() wait for that
7621 * ordered extent to complete while holding a lock on
7622 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007624 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 }
7626
Filipe Mananaade77022016-02-18 14:28:55 +00007627 if (ret)
7628 break;
7629
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 cond_resched();
7631 }
7632
7633 return ret;
7634}
7635
Liu Bo6f9994d2017-01-31 07:50:22 -08007636/* The callers of this must take lock_extent() */
7637static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7638 u64 orig_start, u64 block_start,
7639 u64 block_len, u64 orig_block_len,
7640 u64 ram_bytes, int compress_type,
7641 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007642{
7643 struct extent_map_tree *em_tree;
7644 struct extent_map *em;
7645 struct btrfs_root *root = BTRFS_I(inode)->root;
7646 int ret;
7647
Liu Bo6f9994d2017-01-31 07:50:22 -08007648 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7649 type == BTRFS_ORDERED_COMPRESSED ||
7650 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007651 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007652
Josef Bacik69ffb542012-09-11 15:40:07 -04007653 em_tree = &BTRFS_I(inode)->extent_tree;
7654 em = alloc_extent_map();
7655 if (!em)
7656 return ERR_PTR(-ENOMEM);
7657
7658 em->start = start;
7659 em->orig_start = orig_start;
7660 em->len = len;
7661 em->block_len = block_len;
7662 em->block_start = block_start;
7663 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007664 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007665 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007666 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007667 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007668 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007669 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007670 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007671 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7672 em->compress_type = compress_type;
7673 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007674
7675 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007676 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007677 em->start + em->len - 1, 0);
7678 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007679 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007680 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007681 /*
7682 * The caller has taken lock_extent(), who could race with us
7683 * to add em?
7684 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007685 } while (ret == -EEXIST);
7686
7687 if (ret) {
7688 free_extent_map(em);
7689 return ERR_PTR(ret);
7690 }
7691
Liu Bo6f9994d2017-01-31 07:50:22 -08007692 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007693 return em;
7694}
7695
Filipe Manana9c9464c2015-11-04 09:52:04 +00007696static void adjust_dio_outstanding_extents(struct inode *inode,
7697 struct btrfs_dio_data *dio_data,
7698 const u64 len)
7699{
David Sterba823bb202017-01-04 11:09:51 +01007700 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007701
Filipe Manana9c9464c2015-11-04 09:52:04 +00007702 /*
7703 * If we have an outstanding_extents count still set then we're
7704 * within our reservation, otherwise we need to adjust our inode
7705 * counter appropriately.
7706 */
Liu Boc2931662016-12-22 17:13:54 -08007707 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007708 dio_data->outstanding_extents -= num_extents;
7709 } else {
Liu Boc2931662016-12-22 17:13:54 -08007710 /*
7711 * If dio write length has been split due to no large enough
7712 * contiguous space, we need to compensate our inode counter
7713 * appropriately.
7714 */
7715 u64 num_needed = num_extents - dio_data->outstanding_extents;
7716
Filipe Manana9c9464c2015-11-04 09:52:04 +00007717 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007718 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007719 spin_unlock(&BTRFS_I(inode)->lock);
7720 }
7721}
7722
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7724 struct buffer_head *bh_result, int create)
7725{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007726 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007728 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307729 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007733 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007734 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007735
Miao Xie172a5042013-02-21 02:48:22 -07007736 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007737 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007738 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007739 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007740
Josef Bacikc3298612012-08-03 16:49:19 -04007741 lockstart = start;
7742 lockend = start + len - 1;
7743
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007744 if (current->journal_info) {
7745 /*
7746 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007747 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007748 * confused.
7749 */
chandan50745b02015-08-28 21:10:13 +05307750 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007751 current->journal_info = NULL;
7752 }
7753
Josef Bacikeb838e72012-07-31 16:28:48 -04007754 /*
7755 * If this errors out it's because we couldn't invalidate pagecache for
7756 * this range and we need to fallback to buffered.
7757 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007758 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7759 create)) {
7760 ret = -ENOTBLK;
7761 goto err;
7762 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007764 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 if (IS_ERR(em)) {
7766 ret = PTR_ERR(em);
7767 goto unlock_err;
7768 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769
7770 /*
7771 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7772 * io. INLINE is special, and we could probably kludge it in here, but
7773 * it's still buffered so for safety lets just fall back to the generic
7774 * buffered path.
7775 *
7776 * For COMPRESSED we _have_ to read the entire extent in so we can
7777 * decompress it, so there will be buffering required no matter what we
7778 * do, so go ahead and fallback to buffered.
7779 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007780 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 * to buffered IO. Don't blame me, this is the price we pay for using
7782 * the generic code.
7783 */
7784 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7785 em->block_start == EXTENT_MAP_INLINE) {
7786 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007787 ret = -ENOTBLK;
7788 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007789 }
7790
7791 /* Just a good old fashioned hole, return */
7792 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7793 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7794 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007795 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796 }
7797
7798 /*
7799 * We don't allocate a new extent in the following cases
7800 *
7801 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7802 * existing extent.
7803 * 2) The extent is marked as PREALLOC. We're good to go here and can
7804 * just use the extent.
7805 *
7806 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007807 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007808 len = min(len, em->len - (start - em->start));
7809 lockstart = start + len;
7810 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007811 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812
7813 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7814 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7815 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007817 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007818
7819 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7820 type = BTRFS_ORDERED_PREALLOC;
7821 else
7822 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007823 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007825
Josef Bacik00361582013-08-14 14:02:47 -04007826 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007827 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007828 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007829 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007830
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007831 em2 = btrfs_create_dio_extent(inode, start, len,
7832 orig_start, block_start,
7833 len, orig_block_len,
7834 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007835 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007836 if (type == BTRFS_ORDERED_PREALLOC) {
7837 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007838 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007839 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007840 if (em2 && IS_ERR(em2)) {
7841 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007842 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007843 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007844 /*
7845 * For inode marked NODATACOW or extent marked PREALLOC,
7846 * use the existing or preallocated extent, so does not
7847 * need to adjust btrfs_space_info's bytes_may_use.
7848 */
7849 btrfs_free_reserved_data_space_noquota(inode,
7850 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007851 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007853 }
Josef Bacik00361582013-08-14 14:02:47 -04007854
Chris Mason46bfbb52010-05-26 11:04:10 -04007855 /*
7856 * this will cow the extent, reset the len in case we changed
7857 * it above
7858 */
7859 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007860 free_extent_map(em);
7861 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007862 if (IS_ERR(em)) {
7863 ret = PTR_ERR(em);
7864 goto unlock_err;
7865 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007866 len = min(len, em->len - (start - em->start));
7867unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007868 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7869 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007870 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007871 bh_result->b_bdev = em->bdev;
7872 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007873 if (create) {
7874 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7875 set_buffer_new(bh_result);
7876
7877 /*
7878 * Need to update the i_size under the extent lock so buffered
7879 * readers will get the updated i_size when we unlock.
7880 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007881 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007882 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007883
Filipe Manana9c9464c2015-11-04 09:52:04 +00007884 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307885 WARN_ON(dio_data->reserve < len);
7886 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007887 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307888 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007889 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890
Josef Bacikeb838e72012-07-31 16:28:48 -04007891 /*
7892 * In the case of write we need to clear and unlock the entire range,
7893 * in the case of read we need to unlock only the end area that we
7894 * aren't using if there is any left over space.
7895 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007896 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007897 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7898 lockend, unlock_bits, 1, 0,
7899 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007900 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007901 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007902 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007903
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904 free_extent_map(em);
7905
7906 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007907
7908unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007909 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7910 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007911err:
chandan50745b02015-08-28 21:10:13 +05307912 if (dio_data)
7913 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007914 /*
7915 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7916 * write less data then expected, so that we don't underflow our inode's
7917 * outstanding extents counter.
7918 */
7919 if (create && dio_data)
7920 adjust_dio_outstanding_extents(inode, dio_data, len);
7921
Josef Bacikeb838e72012-07-31 16:28:48 -04007922 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007923}
7924
Miao Xie8b110e32014-09-12 18:44:03 +08007925static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007926 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007927{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007928 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007929 int ret;
7930
Mike Christie37226b22016-06-05 14:31:52 -05007931 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007932
7933 bio_get(bio);
7934
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007935 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007936 if (ret)
7937 goto err;
7938
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007939 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007940err:
7941 bio_put(bio);
7942 return ret;
7943}
7944
7945static int btrfs_check_dio_repairable(struct inode *inode,
7946 struct bio *failed_bio,
7947 struct io_failure_record *failrec,
7948 int failed_mirror)
7949{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007950 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 int num_copies;
7952
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007953 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007954 if (num_copies == 1) {
7955 /*
7956 * we only have a single copy of the data, so don't bother with
7957 * all the retry and error correction code that follows. no
7958 * matter what the error is, it is very likely to persist.
7959 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007960 btrfs_debug(fs_info,
7961 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7962 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007963 return 0;
7964 }
7965
7966 failrec->failed_mirror = failed_mirror;
7967 failrec->this_mirror++;
7968 if (failrec->this_mirror == failed_mirror)
7969 failrec->this_mirror++;
7970
7971 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007972 btrfs_debug(fs_info,
7973 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7974 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007975 return 0;
7976 }
7977
7978 return 1;
7979}
7980
7981static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 struct page *page, unsigned int pgoff,
7983 u64 start, u64 end, int failed_mirror,
7984 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007985{
7986 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007987 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7988 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007989 struct bio *bio;
7990 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007991 int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007992 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 int ret;
7994
Mike Christie37226b22016-06-05 14:31:52 -05007995 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007996
7997 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7998 if (ret)
7999 return ret;
8000
8001 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8002 failed_mirror);
8003 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008004 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008005 return -EIO;
8006 }
8007
Liu Bo17347ce2017-05-15 15:33:27 -07008008 segs = bio_segments(failed_bio);
8009 if (segs > 1 ||
8010 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008011 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008012
8013 isector = start - btrfs_io_bio(failed_bio)->logical;
8014 isector >>= inode->i_sb->s_blocksize_bits;
8015 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308016 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 if (!bio) {
Josef Bacik7870d082017-05-05 11:57:15 -04008018 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008019 return -EIO;
8020 }
Mike Christie37226b22016-06-05 14:31:52 -05008021 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008022
8023 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8024 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8025 read_mode, failrec->this_mirror, failrec->in_validation);
8026
Mike Christie81a75f672016-06-05 14:31:54 -05008027 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008028 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008029 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008030 bio_put(bio);
8031 }
8032
8033 return ret;
8034}
8035
8036struct btrfs_retry_complete {
8037 struct completion done;
8038 struct inode *inode;
8039 u64 start;
8040 int uptodate;
8041};
8042
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008043static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008044{
8045 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008046 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008047 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008048 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008049 int i;
8050
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008051 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008052 goto end;
8053
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308054 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008055 io_tree = &BTRFS_I(inode)->io_tree;
8056 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8057 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058
Miao Xie8b110e32014-09-12 18:44:03 +08008059 done->uptodate = 1;
8060 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008061 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8062 io_tree, done->start, bvec->bv_page,
8063 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008064end:
8065 complete(&done->done);
8066 bio_put(bio);
8067}
8068
8069static int __btrfs_correct_data_nocsum(struct inode *inode,
8070 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008071{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308072 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008073 struct bio_vec bvec;
8074 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008075 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008076 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 unsigned int pgoff;
8078 u32 sectorsize;
8079 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008080 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008081 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008082
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008084 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308085
Miao Xiec1dc0892014-09-12 18:43:56 +08008086 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008087 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008088 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008089
Liu Bo17347ce2017-05-15 15:33:27 -07008090 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8091 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8092 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
8094next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008095 done.uptodate = 0;
8096 done.start = start;
8097 init_completion(&done.done);
8098
Liu Bo17347ce2017-05-15 15:33:27 -07008099 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308100 pgoff, start, start + sectorsize - 1,
8101 io_bio->mirror_num,
8102 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008103 if (ret) {
8104 err = ret;
8105 goto next;
8106 }
Miao Xie8b110e32014-09-12 18:44:03 +08008107
8108 wait_for_completion(&done.done);
8109
8110 if (!done.uptodate) {
8111 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308112 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008113 }
8114
Liu Bo629ebf42017-05-15 17:20:07 -07008115next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116 start += sectorsize;
8117
Liu Bo97bf5a52017-04-07 13:11:10 -07008118 nr_sectors--;
8119 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308120 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008121 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 goto next_block_or_try_again;
8123 }
Miao Xie8b110e32014-09-12 18:44:03 +08008124 }
8125
Liu Bo629ebf42017-05-15 17:20:07 -07008126 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008127}
8128
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008129static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008130{
8131 struct btrfs_retry_complete *done = bio->bi_private;
8132 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008133 struct extent_io_tree *io_tree, *failure_tree;
8134 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008135 struct bio_vec *bvec;
8136 int uptodate;
8137 int ret;
8138 int i;
8139
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008140 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008141 goto end;
8142
8143 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308144
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008146 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147
Josef Bacik7870d082017-05-05 11:57:15 -04008148 io_tree = &BTRFS_I(inode)->io_tree;
8149 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8150
Miao Xie8b110e32014-09-12 18:44:03 +08008151 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008152 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8153 bvec->bv_offset, done->start,
8154 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008155 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008156 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8157 failure_tree, io_tree, done->start,
8158 bvec->bv_page,
8159 btrfs_ino(BTRFS_I(inode)),
8160 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008161 else
8162 uptodate = 0;
8163 }
8164
8165 done->uptodate = uptodate;
8166end:
8167 complete(&done->done);
8168 bio_put(bio);
8169}
8170
8171static int __btrfs_subio_endio_read(struct inode *inode,
8172 struct btrfs_io_bio *io_bio, int err)
8173{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308174 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008175 struct bio_vec bvec;
8176 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008177 struct btrfs_retry_complete done;
8178 u64 start;
8179 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180 u32 sectorsize;
8181 int nr_sectors;
8182 unsigned int pgoff;
8183 int csum_pos;
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:
8199 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8200 ret = __readpage_endio_check(inode, io_bio, csum_pos,
Liu Bo17347ce2017-05-15 15:33:27 -07008201 bvec.bv_page, pgoff, start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308202 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008203 if (likely(!ret))
8204 goto next;
8205try_again:
8206 done.uptodate = 0;
8207 done.start = start;
8208 init_completion(&done.done);
8209
Liu Bo17347ce2017-05-15 15:33:27 -07008210 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308211 pgoff, start, start + sectorsize - 1,
8212 io_bio->mirror_num,
8213 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008214 if (ret) {
8215 err = ret;
8216 goto next;
8217 }
8218
8219 wait_for_completion(&done.done);
8220
8221 if (!done.uptodate) {
8222 /* We might have another mirror, so try again */
8223 goto try_again;
8224 }
8225next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226 offset += sectorsize;
8227 start += sectorsize;
8228
8229 ASSERT(nr_sectors);
8230
Liu Bo97bf5a52017-04-07 13:11:10 -07008231 nr_sectors--;
8232 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308233 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008234 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308235 goto next_block;
8236 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008237 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008238
8239 return err;
8240}
8241
Miao Xie8b110e32014-09-12 18:44:03 +08008242static int btrfs_subio_endio_read(struct inode *inode,
8243 struct btrfs_io_bio *io_bio, int err)
8244{
8245 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8246
8247 if (skip_csum) {
8248 if (unlikely(err))
8249 return __btrfs_correct_data_nocsum(inode, io_bio);
8250 else
8251 return 0;
8252 } else {
8253 return __btrfs_subio_endio_read(inode, io_bio, err);
8254 }
8255}
8256
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008257static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008258{
8259 struct btrfs_dio_private *dip = bio->bi_private;
8260 struct inode *inode = dip->inode;
8261 struct bio *dio_bio;
8262 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008263 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008264
Miao Xie8b110e32014-09-12 18:44:03 +08008265 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8266 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008267
Josef Bacik4b46fce2010-05-23 11:00:55 -04008268 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008269 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008270 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271
Josef Bacik4b46fce2010-05-23 11:00:55 -04008272 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008273
Filipe Manana1636d1d2016-02-15 16:20:26 +00008274 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008275 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008276
8277 if (io_bio->end_io)
8278 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008279 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008280}
8281
Qu Wenruo524272602017-03-08 10:25:52 +08008282static void __endio_write_update_ordered(struct inode *inode,
8283 const u64 offset, const u64 bytes,
8284 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008286 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008287 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008288 struct btrfs_workqueue *wq;
8289 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008290 u64 ordered_offset = offset;
8291 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008292 int ret;
8293
Qu Wenruo524272602017-03-08 10:25:52 +08008294 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8295 wq = fs_info->endio_freespace_worker;
8296 func = btrfs_freespace_write_helper;
8297 } else {
8298 wq = fs_info->endio_write_workers;
8299 func = btrfs_endio_write_helper;
8300 }
8301
Chris Mason163cf092010-11-28 19:56:33 -05008302again:
8303 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8304 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008305 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008306 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008307 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008308 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309
Qu Wenruo524272602017-03-08 10:25:52 +08008310 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8311 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008312out_test:
8313 /*
8314 * our bio might span multiple ordered extents. If we haven't
8315 * completed the accounting for the whole dio, go back and try again
8316 */
Filipe Manana14543772015-11-24 16:23:54 +00008317 if (ordered_offset < offset + bytes) {
8318 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008319 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008320 goto again;
8321 }
Filipe Manana14543772015-11-24 16:23:54 +00008322}
8323
8324static void btrfs_endio_direct_write(struct bio *bio)
8325{
8326 struct btrfs_dio_private *dip = bio->bi_private;
8327 struct bio *dio_bio = dip->dio_bio;
8328
Qu Wenruo524272602017-03-08 10:25:52 +08008329 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8330 dip->bytes, !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008331
Josef Bacik4b46fce2010-05-23 11:00:55 -04008332 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008333
Filipe Manana1636d1d2016-02-15 16:20:26 +00008334 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008335 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008336 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008337}
8338
Josef Bacikc6100a42017-05-05 11:57:13 -04008339static int __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008340 struct bio *bio, int mirror_num,
8341 unsigned long bio_flags, u64 offset)
8342{
Josef Bacikc6100a42017-05-05 11:57:13 -04008343 struct inode *inode = private_data;
Chris Masoneaf25d92010-05-25 09:48:28 -04008344 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008345 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008346 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008347 return 0;
8348}
8349
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008350static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008351{
8352 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008353 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008354
Miao Xie8b110e32014-09-12 18:44:03 +08008355 if (err)
8356 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008357 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008358 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8359 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008360 (unsigned long long)bio->bi_iter.bi_sector,
8361 bio->bi_iter.bi_size, err);
8362
8363 if (dip->subio_endio)
8364 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008365
8366 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008367 dip->errors = 1;
8368
8369 /*
8370 * before atomic variable goto zero, we must make sure
8371 * dip->errors is perceived to be set.
8372 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008373 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008374 }
8375
8376 /* if there are more bios still pending for this dio, just exit */
8377 if (!atomic_dec_and_test(&dip->pending_bios))
8378 goto out;
8379
Chris Mason9be33952013-05-17 18:30:14 -04008380 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008381 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008382 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008383 dip->dio_bio->bi_error = 0;
8384 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008385 }
8386out:
8387 bio_put(bio);
8388}
8389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008390static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008391 struct btrfs_dio_private *dip,
8392 struct bio *bio,
8393 u64 file_offset)
8394{
8395 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8396 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8397 int ret;
8398
8399 /*
8400 * We load all the csum data we need when we submit
8401 * the first bio to reduce the csum tree search and
8402 * contention.
8403 */
8404 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008405 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008406 file_offset);
8407 if (ret)
8408 return ret;
8409 }
8410
8411 if (bio == dip->orig_bio)
8412 return 0;
8413
8414 file_offset -= dip->logical_offset;
8415 file_offset >>= inode->i_sb->s_blocksize_bits;
8416 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8417
8418 return 0;
8419}
8420
Miao Xiee65e1532010-11-22 03:04:43 +00008421static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008422 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008423 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008424{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008425 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008426 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008427 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008428 int ret;
8429
Josef Bacikb812ce22012-11-16 13:56:32 -05008430 if (async_submit)
8431 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8432
Miao Xiee65e1532010-11-22 03:04:43 +00008433 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008434
8435 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008436 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008437 if (ret)
8438 goto err;
8439 }
Miao Xiee65e1532010-11-22 03:04:43 +00008440
Josef Bacik1ae39932011-04-06 14:41:34 -04008441 if (skip_sum)
8442 goto map;
8443
8444 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008445 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8446 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008447 __btrfs_submit_bio_start_direct_io,
8448 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008449 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008450 } else if (write) {
8451 /*
8452 * If we aren't doing async submit, calculate the csum of the
8453 * bio now.
8454 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008455 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008456 if (ret)
8457 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008458 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008459 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008460 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008461 if (ret)
8462 goto err;
8463 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008464map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008465 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008466err:
8467 bio_put(bio);
8468 return ret;
8469}
8470
Mike Christie81a75f672016-06-05 14:31:54 -05008471static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008472 int skip_sum)
8473{
8474 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008475 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008476 struct bio *bio;
8477 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008478 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008479 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008480 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008481 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008482 u64 submit_len;
8483 int clone_offset = 0;
8484 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308485 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008486
Kent Overstreet4f024f32013-10-11 15:44:27 -07008487 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008488 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008489 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8490 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008491 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008492 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008493
Liu Bo725130b2017-05-16 09:51:39 -07008494 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008495 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008496 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008497 goto submit;
8498 }
8499
David Woodhouse53b381b2013-01-29 18:40:14 -05008500 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008501 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008502 async_submit = 0;
8503 else
8504 async_submit = 1;
8505
Liu Bo725130b2017-05-16 09:51:39 -07008506 /* bio split */
8507 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008508 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008509 do {
Liu Bo725130b2017-05-16 09:51:39 -07008510 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008511
Liu Bo725130b2017-05-16 09:51:39 -07008512 /*
8513 * This will never fail as it's passing GPF_NOFS and
8514 * the allocation is backed by btrfs_bioset.
8515 */
Liu Boe4770942017-05-16 10:57:14 -07008516 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008517 clone_len);
8518 bio->bi_private = dip;
8519 bio->bi_end_io = btrfs_end_dio_bio;
8520 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008521
Liu Bo725130b2017-05-16 09:51:39 -07008522 ASSERT(submit_len >= clone_len);
8523 submit_len -= clone_len;
8524 if (submit_len == 0)
8525 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008526
Liu Bo725130b2017-05-16 09:51:39 -07008527 /*
8528 * Increase the count before we submit the bio so we know
8529 * the end IO handler won't happen before we increase the
8530 * count. Otherwise, the dip might get freed before we're
8531 * done setting it up.
8532 */
8533 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008534
Liu Bo725130b2017-05-16 09:51:39 -07008535 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8536 async_submit);
8537 if (ret) {
8538 bio_put(bio);
8539 atomic_dec(&dip->pending_bios);
8540 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008541 }
Liu Bo725130b2017-05-16 09:51:39 -07008542
8543 clone_offset += clone_len;
8544 start_sector += clone_len >> 9;
8545 file_offset += clone_len;
8546
8547 map_length = submit_len;
8548 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8549 start_sector << 9, &map_length, NULL, 0);
8550 if (ret)
8551 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008552 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008553
Josef Bacik02f57c72011-04-06 14:25:44 -04008554submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008555 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008556 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008557 if (!ret)
8558 return 0;
8559
8560 bio_put(bio);
8561out_err:
8562 dip->errors = 1;
8563 /*
8564 * before atomic variable goto zero, we must
8565 * make sure dip->errors is perceived to be set.
8566 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008567 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008568 if (atomic_dec_and_test(&dip->pending_bios))
8569 bio_io_error(dip->orig_bio);
8570
8571 /* bio_end_io() will handle error, so we needn't return it */
8572 return 0;
8573}
8574
Mike Christie8a4c1e42016-06-05 14:31:50 -05008575static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8576 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008577{
Filipe Manana61de7182015-07-01 12:13:10 +01008578 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008579 struct bio *bio = NULL;
8580 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008581 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008582 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008583 int ret = 0;
8584
8585 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8586
Liu Bo3892ac92017-04-17 15:00:28 -07008587 bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
8588 if (!bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008589 ret = -ENOMEM;
8590 goto free_ordered;
8591 }
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) {
8651 bio->bi_error = -EIO;
8652 bio_endio(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008653 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008654 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008655 * and all the cleanup and final put for dio_bio (through
8656 * dio_end_io()).
8657 */
8658 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008659 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008660 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008661 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008662 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008663 file_offset,
8664 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008665 false);
Filipe Manana14543772015-11-24 16:23:54 +00008666 else
Filipe Manana61de7182015-07-01 12:13:10 +01008667 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8668 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008669
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008670 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008671 /*
8672 * Releases and cleans up our dio_bio, no need to bio_put()
8673 * nor bio_endio()/bio_io_error() against dio_bio.
8674 */
8675 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008676 }
Liu Bo3892ac92017-04-17 15:00:28 -07008677 if (bio)
8678 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008679 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008680}
8681
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008682static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8683 struct kiocb *iocb,
8684 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008685{
8686 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008687 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008688 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008689 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008690
8691 if (offset & blocksize_mask)
8692 goto out;
8693
Al Viro28060d52014-03-22 05:15:17 -04008694 if (iov_iter_alignment(iter) & blocksize_mask)
8695 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008696
Al Viro28060d52014-03-22 05:15:17 -04008697 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008698 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008699 return 0;
8700 /*
8701 * Check to make sure we don't have duplicate iov_base's in this
8702 * iovec, if so return EINVAL, otherwise we'll get csum errors
8703 * when reading back.
8704 */
8705 for (seg = 0; seg < iter->nr_segs; seg++) {
8706 for (i = seg + 1; i < iter->nr_segs; i++) {
8707 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008708 goto out;
8709 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008710 }
8711 retval = 0;
8712out:
8713 return retval;
8714}
Josef Bacikeb838e72012-07-31 16:28:48 -04008715
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008716static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008717{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008718 struct file *file = iocb->ki_filp;
8719 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008720 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308721 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008722 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008723 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008724 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008725 bool wakeup = true;
8726 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008727 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008728
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008729 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008730 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008731
Jens Axboefe0f07d2015-04-15 17:05:48 -06008732 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008733 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008734
Josef Bacik0e267c42013-07-02 10:38:02 -04008735 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008736 * The generic stuff only does filemap_write_and_wait_range, which
8737 * isn't enough if we've written compressed pages to this area, so
8738 * we need to flush the dirty pages again to make absolutely sure
8739 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008740 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008741 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008742 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8743 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008744 filemap_fdatawrite_range(inode->i_mapping, offset,
8745 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008746
Omar Sandoval6f673762015-03-16 04:33:52 -07008747 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008748 /*
8749 * If the write DIO is beyond the EOF, we need update
8750 * the isize, but it is protected by i_mutex. So we can
8751 * not unlock the i_mutex at this case.
8752 */
8753 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008754 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008755 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008756 relock = true;
8757 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008758 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008759 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008760 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008761 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008762
8763 /*
8764 * We need to know how many extents we reserved so that we can
8765 * do the accounting properly if we go over the number we
8766 * originally calculated. Abuse current->journal_info for this.
8767 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008768 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008769 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008770 dio_data.unsubmitted_oe_range_start = (u64)offset;
8771 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308772 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308773 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008774 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8775 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008776 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008777 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8778 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008779 }
8780
Omar Sandoval17f8c842015-03-16 04:33:50 -07008781 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008782 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008783 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008784 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008785 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308786 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008787 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008788 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308789 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008790 btrfs_delalloc_release_space(inode, offset,
8791 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008792 /*
8793 * On error we might have left some ordered extents
8794 * without submitting corresponding bios for them, so
8795 * cleanup them up to avoid other tasks getting them
8796 * and waiting for them to complete forever.
8797 */
8798 if (dio_data.unsubmitted_oe_range_start <
8799 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008800 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008801 dio_data.unsubmitted_oe_range_start,
8802 dio_data.unsubmitted_oe_range_end -
8803 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008804 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008805 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008806 btrfs_delalloc_release_space(inode, offset,
8807 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008808 }
Miao Xie38851cc2013-02-08 07:04:11 +00008809out:
Miao Xie2e60a512013-02-08 07:01:08 +00008810 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008811 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008812 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008813 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008814
8815 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008816}
8817
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008818#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8819
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008820static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8821 __u64 start, __u64 len)
8822{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008823 int ret;
8824
8825 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8826 if (ret)
8827 return ret;
8828
Chris Masonec29ed52011-02-23 16:23:20 -05008829 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008830}
8831
Chris Mason9ebefb182007-06-15 13:50:00 -04008832int btrfs_readpage(struct file *file, struct page *page)
8833{
Chris Masond1310b22008-01-24 16:13:08 -05008834 struct extent_io_tree *tree;
8835 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008836 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008837}
Chris Mason1832a6d2007-12-21 16:27:21 -05008838
Chris Mason39279cc2007-06-12 06:35:45 -04008839static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8840{
Chris Masond1310b22008-01-24 16:13:08 -05008841 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008842 struct inode *inode = page->mapping->host;
8843 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008844
8845 if (current->flags & PF_MEMALLOC) {
8846 redirty_page_for_writepage(wbc, page);
8847 unlock_page(page);
8848 return 0;
8849 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008850
8851 /*
8852 * If we are under memory pressure we will call this directly from the
8853 * VM, we need to make sure we have the inode referenced for the ordered
8854 * extent. If not just return like we didn't do anything.
8855 */
8856 if (!igrab(inode)) {
8857 redirty_page_for_writepage(wbc, page);
8858 return AOP_WRITEPAGE_ACTIVATE;
8859 }
Chris Masond1310b22008-01-24 16:13:08 -05008860 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008861 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8862 btrfs_add_delayed_iput(inode);
8863 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008864}
Chris Mason39279cc2007-06-12 06:35:45 -04008865
Eric Sandeen48a3b632013-04-25 20:41:01 +00008866static int btrfs_writepages(struct address_space *mapping,
8867 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008868{
Chris Masond1310b22008-01-24 16:13:08 -05008869 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008870
Chris Masond1310b22008-01-24 16:13:08 -05008871 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008872 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8873}
8874
Chris Mason3ab2fb52007-11-08 10:59:22 -05008875static int
8876btrfs_readpages(struct file *file, struct address_space *mapping,
8877 struct list_head *pages, unsigned nr_pages)
8878{
Chris Masond1310b22008-01-24 16:13:08 -05008879 struct extent_io_tree *tree;
8880 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008881 return extent_readpages(tree, mapping, pages, nr_pages,
8882 btrfs_get_extent);
8883}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008885{
Chris Masond1310b22008-01-24 16:13:08 -05008886 struct extent_io_tree *tree;
8887 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008888 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008889
Chris Masond1310b22008-01-24 16:13:08 -05008890 tree = &BTRFS_I(page->mapping->host)->io_tree;
8891 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008892 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008893 if (ret == 1) {
8894 ClearPagePrivate(page);
8895 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008896 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008897 }
8898 return ret;
8899}
Chris Mason39279cc2007-06-12 06:35:45 -04008900
Chris Masone6dcd2d2008-07-17 12:53:50 -04008901static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8902{
Chris Mason98509cf2008-09-11 15:51:43 -04008903 if (PageWriteback(page) || PageDirty(page))
8904 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008905 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906}
8907
Lukas Czernerd47992f2013-05-21 23:17:23 -04008908static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8909 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008910{
Josef Bacik5fd02042012-05-02 14:00:54 -04008911 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008912 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008914 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008916 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308917 u64 start;
8918 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008919 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008920
Chris Mason8b62b722009-09-02 16:53:46 -04008921 /*
8922 * we have the page locked, so new writeback can't start,
8923 * and the dirty bit won't be cleared while we are here.
8924 *
8925 * Wait for IO on this page so that we can safely clear
8926 * the PagePrivate2 bit and do ordered accounting
8927 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008929
Josef Bacik5fd02042012-05-02 14:00:54 -04008930 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008931 if (offset) {
8932 btrfs_releasepage(page, GFP_NOFS);
8933 return;
8934 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008935
8936 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008937 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308938again:
8939 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008940 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308941 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308943 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008944 /*
8945 * IO on this page will never be started, so we need
8946 * to account for any ordered extents now
8947 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008948 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308949 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008950 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008951 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008952 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8953 EXTENT_DEFRAG, 1, 0, &cached_state,
8954 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008955 /*
8956 * whoever cleared the private bit is responsible
8957 * for the finish_ordered_io
8958 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008959 if (TestClearPagePrivate2(page)) {
8960 struct btrfs_ordered_inode_tree *tree;
8961 u64 new_len;
8962
8963 tree = &BTRFS_I(inode)->ordered_tree;
8964
8965 spin_lock_irq(&tree->lock);
8966 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308967 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008968 if (new_len < ordered->truncated_len)
8969 ordered->truncated_len = new_len;
8970 spin_unlock_irq(&tree->lock);
8971
8972 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308973 start,
8974 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008975 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008976 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008977 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008978 if (!inode_evicting) {
8979 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308980 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008981 &cached_state);
8982 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308983
8984 start = end + 1;
8985 if (start < page_end)
8986 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008987 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008988
Qu Wenruob9d0b382015-09-29 10:35:16 +08008989 /*
8990 * Qgroup reserved space handler
8991 * Page here will be either
8992 * 1) Already written to disk
8993 * In this case, its reserved space is released from data rsv map
8994 * and will be freed by delayed_ref handler finally.
8995 * So even we call qgroup_free_data(), it won't decrease reserved
8996 * space.
8997 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008998 * This means the reserved space should be freed here. However,
8999 * if a truncate invalidates the page (by clearing PageDirty)
9000 * and the page is accounted for while allocating extent
9001 * in btrfs_check_data_free_space() we let delayed_ref to
9002 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009003 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009004 if (PageDirty(page))
9005 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009006 if (!inode_evicting) {
9007 clear_extent_bit(tree, page_start, page_end,
9008 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009009 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9010 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009011 &cached_state, GFP_NOFS);
9012
9013 __btrfs_releasepage(page, GFP_NOFS);
9014 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015
Chris Mason4a096752008-07-21 10:29:44 -04009016 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009017 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009018 ClearPagePrivate(page);
9019 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009020 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009021 }
Chris Mason39279cc2007-06-12 06:35:45 -04009022}
9023
Chris Mason9ebefb182007-06-15 13:50:00 -04009024/*
9025 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9026 * called from a page fault handler when a page is first dirtied. Hence we must
9027 * be careful to check for EOF conditions here. We set the page up correctly
9028 * for a written page which means we get ENOSPC checking when writing into
9029 * holes and correct delalloc and unwritten extent mapping on filesystems that
9030 * support these features.
9031 *
9032 * We are not allowed to take the i_mutex here so we have to play games to
9033 * protect against truncate races as the page could now be beyond EOF. Because
9034 * vmtruncate() writes the inode size before removing pages, once we have the
9035 * page lock we can determine safely if the page is beyond EOF. If it is not
9036 * beyond EOF, then the page is guaranteed safe against truncation until we
9037 * unlock the page.
9038 */
Dave Jiang11bac802017-02-24 14:56:41 -08009039int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009040{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009041 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009042 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009044 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9045 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009046 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009047 char *kaddr;
9048 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009049 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009050 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009051 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309052 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009053 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009054 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 u64 end;
9056
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009058
Jan Karab2b5ef52012-06-12 16:20:45 +02009059 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009060 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009061 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309062 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009063
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309064 /*
9065 * Reserving delalloc space after obtaining the page lock can lead to
9066 * deadlock. For example, if a dirty page is locked by this function
9067 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9068 * dirty page write out, then the btrfs_writepage() function could
9069 * end up waiting indefinitely to get a lock on the page currently
9070 * being processed by btrfs_page_mkwrite() function.
9071 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009072 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309073 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009074 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009075 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009076 reserved = 1;
9077 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009078 if (ret) {
9079 if (ret == -ENOMEM)
9080 ret = VM_FAULT_OOM;
9081 else /* -ENOSPC, -EIO, etc */
9082 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009083 if (reserved)
9084 goto out;
9085 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009086 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009087
Nick Piggin56a76f82009-03-31 15:23:23 -07009088 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009089again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009090 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009091 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009092
Chris Mason9ebefb182007-06-15 13:50:00 -04009093 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009094 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009095 /* page got truncated out from underneath us */
9096 goto out_unlock;
9097 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009098 wait_on_page_writeback(page);
9099
David Sterbaff13db42015-12-03 14:30:40 +01009100 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009101 set_page_extent_mapped(page);
9102
Chris Masoneb84ae02008-07-17 13:53:27 -04009103 /*
9104 * we can't set the delalloc bits if there are pending ordered
9105 * extents. Drop our locks and wait for them to finish
9106 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009107 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9108 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009109 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009110 unlock_extent_cached(io_tree, page_start, page_end,
9111 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009113 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114 btrfs_put_ordered_extent(ordered);
9115 goto again;
9116 }
9117
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009118 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009119 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009120 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009121 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309122 end = page_start + reserved_space - 1;
9123 spin_lock(&BTRFS_I(inode)->lock);
9124 BTRFS_I(inode)->outstanding_extents++;
9125 spin_unlock(&BTRFS_I(inode)->lock);
9126 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009127 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309128 }
9129 }
9130
Josef Bacikfbf19082009-10-01 17:10:23 -04009131 /*
Liu Bo54160342016-12-13 12:15:19 -08009132 * page_mkwrite gets called when the page is firstly dirtied after it's
9133 * faulted in, but write(2) could also dirty a page and set delalloc
9134 * bits, thus in this case for space account reason, we still need to
9135 * clear any delalloc bits within this page range since we have to
9136 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009137 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309138 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009139 EXTENT_DIRTY | EXTENT_DELALLOC |
9140 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009141 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009142
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309143 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009144 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009145 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009146 unlock_extent_cached(io_tree, page_start, page_end,
9147 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009148 ret = VM_FAULT_SIGBUS;
9149 goto out_unlock;
9150 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009151 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009152
9153 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009154 if (page_start + PAGE_SIZE > size)
9155 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009156 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009157 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009158
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009159 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009160 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009161 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009162 flush_dcache_page(page);
9163 kunmap(page);
9164 }
Chris Mason247e7432008-07-17 12:53:51 -04009165 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009166 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009167 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009169 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009170 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009171 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009172
Josef Bacik2ac55d42010-02-03 19:33:23 +00009173 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009174
9175out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009176 if (!ret) {
9177 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009178 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009179 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009180 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009181out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309182 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009183out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009184 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009185 return ret;
9186}
9187
Josef Bacika41ad392011-01-31 15:30:16 -05009188static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009191 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009192 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009193 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009194 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009195 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009196 u64 mask = fs_info->sectorsize - 1;
9197 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009198
Josef Bacik0ef8b722013-10-25 16:13:35 -04009199 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9200 (u64)-1);
9201 if (ret)
9202 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009203
Josef Bacikfcb80c22011-05-03 10:40:22 -04009204 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009205 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009206 * 3 things going on here
9207 *
9208 * 1) We need to reserve space for our orphan item and the space to
9209 * delete our orphan item. Lord knows we don't want to have a dangling
9210 * orphan item because we didn't reserve space to remove it.
9211 *
9212 * 2) We need to reserve space to update our inode.
9213 *
9214 * 3) We need to have something to cache all the space that is going to
9215 * be free'd up by the truncate operation, but also have some slack
9216 * space reserved in case it uses space during the truncate (thank you
9217 * very much snapshotting).
9218 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009219 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009221 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009222 * doesn't end up using space reserved for updating the inode or
9223 * removing the orphan item. We also need to be able to stop the
9224 * transaction and start a new one, which means we need to be able to
9225 * update the inode several times, and we have no idea of knowing how
9226 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009227 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009228 * Then there is the orphan item, which does indeed need to be held on
9229 * to for the whole operation, and we need nobody to touch this reserved
9230 * space except the orphan code.
9231 *
9232 * So that leaves us with
9233 *
9234 * 1) root->orphan_block_rsv - for the orphan deletion.
9235 * 2) rsv - for the truncate reservation, which we will steal from the
9236 * transaction reservation.
9237 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9238 * updating the inode.
9239 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009240 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 if (!rsv)
9242 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009243 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009244 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009245
Josef Bacik907cbce2011-08-08 13:46:15 -04009246 /*
Josef Bacik07127182011-08-19 10:29:59 -04009247 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009248 * 1 for updating the inode.
9249 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009250 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009251 if (IS_ERR(trans)) {
9252 err = PTR_ERR(trans);
9253 goto out;
9254 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009255
Josef Bacik907cbce2011-08-08 13:46:15 -04009256 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009257 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009258 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009259 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009260
Chris Mason5a3f23d2009-03-31 13:27:11 -04009261 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009262 * So if we truncate and then write and fsync we normally would just
9263 * write the extents that changed, which is a problem if we need to
9264 * first truncate that entire inode. So set this flag so we write out
9265 * all of the extents in the inode to the sync log so we're completely
9266 * safe.
9267 */
9268 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009269 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009270
Yan, Zheng80825102009-11-12 09:35:36 +00009271 while (1) {
9272 ret = btrfs_truncate_inode_items(trans, root, inode,
9273 inode->i_size,
9274 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009275 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009276 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009277 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009278 }
Chris Mason39279cc2007-06-12 06:35:45 -04009279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009280 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009281 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009282 if (ret) {
9283 err = ret;
9284 break;
9285 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009286
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009287 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009288 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009289
9290 trans = btrfs_start_transaction(root, 2);
9291 if (IS_ERR(trans)) {
9292 ret = err = PTR_ERR(trans);
9293 trans = NULL;
9294 break;
9295 }
9296
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009297 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009298 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009299 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009300 BUG_ON(ret); /* shouldn't happen */
9301 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009302 }
9303
9304 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009305 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009306 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009307 if (ret)
9308 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009309 }
9310
Chris Mason917c16b2011-11-08 14:49:59 -05009311 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009312 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009313 ret = btrfs_update_inode(trans, root, inode);
9314 if (ret && !err)
9315 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009316
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009317 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009318 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009319 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009320out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009321 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009322
Josef Bacik3893e332011-01-31 16:03:11 -05009323 if (ret && !err)
9324 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009325
Josef Bacik3893e332011-01-31 16:03:11 -05009326 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009327}
9328
Sven Wegener3b963622008-06-09 21:57:42 -04009329/*
Chris Masond352ac62008-09-29 15:18:18 -04009330 * create a new subvolume directory/inode (helper for the ioctl).
9331 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009332int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009333 struct btrfs_root *new_root,
9334 struct btrfs_root *parent_root,
9335 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009336{
Chris Mason39279cc2007-06-12 06:35:45 -04009337 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009338 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009339 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009340
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009341 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9342 new_dirid, new_dirid,
9343 S_IFDIR | (~current_umask() & S_IRWXUGO),
9344 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009345 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009346 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009347 inode->i_op = &btrfs_dir_inode_operations;
9348 inode->i_fop = &btrfs_dir_file_operations;
9349
Miklos Szeredibfe86842011-10-28 14:13:29 +02009350 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009351 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009352 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009353
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009354 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9355 if (err)
9356 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009357 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009358 new_root->root_key.objectid, err);
9359
Yan, Zheng76dda932009-09-21 16:00:26 -04009360 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009361
Yan, Zheng76dda932009-09-21 16:00:26 -04009362 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009363 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009364}
9365
Chris Mason39279cc2007-06-12 06:35:45 -04009366struct inode *btrfs_alloc_inode(struct super_block *sb)
9367{
9368 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009369 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009370
9371 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9372 if (!ei)
9373 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009374
9375 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009376 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009377 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009378 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009379 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009380 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009381 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009382 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009383 ei->disk_i_size = 0;
9384 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009385 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009386 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009387 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009388 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009389 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009390 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009391
Josef Bacik9e0baf62011-07-15 15:16:44 +00009392 spin_lock_init(&ei->lock);
9393 ei->outstanding_extents = 0;
9394 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009395
Josef Bacik72ac3c02012-05-23 14:13:11 -04009396 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009397 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009398
Miao Xie16cdcec2011-04-22 18:12:22 +08009399 ei->delayed_node = NULL;
9400
chandan r9cc97d62012-07-04 12:48:07 +05309401 ei->i_otime.tv_sec = 0;
9402 ei->i_otime.tv_nsec = 0;
9403
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009404 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009405 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009406 extent_io_tree_init(&ei->io_tree, inode);
9407 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009408 ei->io_tree.track_uptodate = 1;
9409 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009410 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009411 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009412 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009413 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009414 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009415 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009417 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418
9419 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009420}
9421
Josef Bacikaaedb552013-10-11 14:44:09 -04009422#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9423void btrfs_test_destroy_inode(struct inode *inode)
9424{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009425 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009426 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9427}
9428#endif
9429
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009430static void btrfs_i_callback(struct rcu_head *head)
9431{
9432 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009433 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9434}
9435
Chris Mason39279cc2007-06-12 06:35:45 -04009436void btrfs_destroy_inode(struct inode *inode)
9437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009438 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009439 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009440 struct btrfs_root *root = BTRFS_I(inode)->root;
9441
Al Virob3d9b7a2012-06-09 13:51:19 -04009442 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009443 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009444 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9445 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009446 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009447 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009448 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009449 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009450
Chris Mason5a3f23d2009-03-31 13:27:11 -04009451 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009452 * This can happen where we create an inode, but somebody else also
9453 * created the same inode and we need to destroy the one we already
9454 * created.
9455 */
9456 if (!root)
9457 goto free;
9458
Josef Bacik8a35d952012-05-23 14:26:42 -04009459 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9460 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009461 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009462 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009463 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009464 }
Josef Bacik7b128762008-07-24 12:17:14 -04009465
Chris Masond3977122009-01-05 21:25:51 -05009466 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009467 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9468 if (!ordered)
9469 break;
9470 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009471 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009472 "found ordered extent %llu %llu on inode cleanup",
9473 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009474 btrfs_remove_ordered_extent(inode, ordered);
9475 btrfs_put_ordered_extent(ordered);
9476 btrfs_put_ordered_extent(ordered);
9477 }
9478 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009479 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009480 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009481 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009482free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009483 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009484}
9485
Al Viro45321ac2010-06-07 13:43:19 -04009486int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009487{
9488 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009489
Naohiro Aota6379ef92013-06-06 09:56:34 +00009490 if (root == NULL)
9491 return 1;
9492
Liu Bofa6ac872013-02-20 14:10:23 +00009493 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009494 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009495 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009496 else
Al Viro45321ac2010-06-07 13:43:19 -04009497 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009498}
9499
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009500static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009501{
9502 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9503
9504 inode_init_once(&ei->vfs_inode);
9505}
9506
9507void btrfs_destroy_cachep(void)
9508{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009509 /*
9510 * Make sure all delayed rcu free inodes are flushed before we
9511 * destroy cache.
9512 */
9513 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009514 kmem_cache_destroy(btrfs_inode_cachep);
9515 kmem_cache_destroy(btrfs_trans_handle_cachep);
9516 kmem_cache_destroy(btrfs_transaction_cachep);
9517 kmem_cache_destroy(btrfs_path_cachep);
9518 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009519}
9520
9521int btrfs_init_cachep(void)
9522{
David Sterba837e1972012-09-07 03:00:48 -06009523 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009524 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009525 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9526 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009527 if (!btrfs_inode_cachep)
9528 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009529
David Sterba837e1972012-09-07 03:00:48 -06009530 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009531 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009532 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009533 if (!btrfs_trans_handle_cachep)
9534 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009535
David Sterba837e1972012-09-07 03:00:48 -06009536 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009537 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009538 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009539 if (!btrfs_transaction_cachep)
9540 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009541
David Sterba837e1972012-09-07 03:00:48 -06009542 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009543 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009544 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009545 if (!btrfs_path_cachep)
9546 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009547
David Sterba837e1972012-09-07 03:00:48 -06009548 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009549 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009550 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009551 if (!btrfs_free_space_cachep)
9552 goto fail;
9553
Chris Mason39279cc2007-06-12 06:35:45 -04009554 return 0;
9555fail:
9556 btrfs_destroy_cachep();
9557 return -ENOMEM;
9558}
9559
David Howellsa528d352017-01-31 16:46:22 +00009560static int btrfs_getattr(const struct path *path, struct kstat *stat,
9561 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009562{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009563 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009564 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009565 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009566 u32 bi_flags = BTRFS_I(inode)->flags;
9567
9568 stat->result_mask |= STATX_BTIME;
9569 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9570 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9571 if (bi_flags & BTRFS_INODE_APPEND)
9572 stat->attributes |= STATX_ATTR_APPEND;
9573 if (bi_flags & BTRFS_INODE_COMPRESS)
9574 stat->attributes |= STATX_ATTR_COMPRESSED;
9575 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9576 stat->attributes |= STATX_ATTR_IMMUTABLE;
9577 if (bi_flags & BTRFS_INODE_NODUMP)
9578 stat->attributes |= STATX_ATTR_NODUMP;
9579
9580 stat->attributes_mask |= (STATX_ATTR_APPEND |
9581 STATX_ATTR_COMPRESSED |
9582 STATX_ATTR_IMMUTABLE |
9583 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009584
Chris Mason39279cc2007-06-12 06:35:45 -04009585 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009586 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009587
9588 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009589 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009590 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009591 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009592 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009593 return 0;
9594}
9595
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596static int btrfs_rename_exchange(struct inode *old_dir,
9597 struct dentry *old_dentry,
9598 struct inode *new_dir,
9599 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009600{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009601 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009602 struct btrfs_trans_handle *trans;
9603 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009604 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 struct inode *new_inode = new_dentry->d_inode;
9606 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009607 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009609 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9610 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 u64 old_idx = 0;
9612 u64 new_idx = 0;
9613 u64 root_objectid;
9614 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009615 bool root_log_pinned = false;
9616 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617
9618 /* we only allow rename subvolume link between subvolumes */
9619 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9620 return -EXDEV;
9621
9622 /* close the race window with snapshot create/destroy ioctl */
9623 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009624 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009626 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627
9628 /*
9629 * We want to reserve the absolute worst case amount of items. So if
9630 * both inodes are subvols and we need to unlink them then that would
9631 * require 4 item modifications, but if they are both normal inodes it
9632 * would require 5 item modifications, so we'll assume their normal
9633 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9634 * should cover the worst case number of items we'll modify.
9635 */
9636 trans = btrfs_start_transaction(root, 12);
9637 if (IS_ERR(trans)) {
9638 ret = PTR_ERR(trans);
9639 goto out_notrans;
9640 }
9641
9642 /*
9643 * We need to find a free sequence number both in the source and
9644 * in the destination directory for the exchange.
9645 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009646 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 if (ret)
9648 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009649 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 if (ret)
9651 goto out_fail;
9652
9653 BTRFS_I(old_inode)->dir_index = 0ULL;
9654 BTRFS_I(new_inode)->dir_index = 0ULL;
9655
9656 /* Reference for the source. */
9657 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9658 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009659 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009660 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009661 btrfs_pin_log_trans(root);
9662 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 ret = btrfs_insert_inode_ref(trans, dest,
9664 new_dentry->d_name.name,
9665 new_dentry->d_name.len,
9666 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009667 btrfs_ino(BTRFS_I(new_dir)),
9668 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 if (ret)
9670 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 }
9672
9673 /* And now for the dest. */
9674 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9675 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009676 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009677 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009678 btrfs_pin_log_trans(dest);
9679 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 ret = btrfs_insert_inode_ref(trans, root,
9681 old_dentry->d_name.name,
9682 old_dentry->d_name.len,
9683 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009684 btrfs_ino(BTRFS_I(old_dir)),
9685 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 if (ret)
9687 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 }
9689
9690 /* Update inode version and ctime/mtime. */
9691 inode_inc_iversion(old_dir);
9692 inode_inc_iversion(new_dir);
9693 inode_inc_iversion(old_inode);
9694 inode_inc_iversion(new_inode);
9695 old_dir->i_ctime = old_dir->i_mtime = ctime;
9696 new_dir->i_ctime = new_dir->i_mtime = ctime;
9697 old_inode->i_ctime = ctime;
9698 new_inode->i_ctime = ctime;
9699
9700 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009701 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9702 BTRFS_I(old_inode), 1);
9703 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9704 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705 }
9706
9707 /* src is a subvolume */
9708 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9709 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9710 ret = btrfs_unlink_subvol(trans, root, old_dir,
9711 root_objectid,
9712 old_dentry->d_name.name,
9713 old_dentry->d_name.len);
9714 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009715 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9716 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 old_dentry->d_name.name,
9718 old_dentry->d_name.len);
9719 if (!ret)
9720 ret = btrfs_update_inode(trans, root, old_inode);
9721 }
9722 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009723 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 goto out_fail;
9725 }
9726
9727 /* dest is a subvolume */
9728 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9729 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9730 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9731 root_objectid,
9732 new_dentry->d_name.name,
9733 new_dentry->d_name.len);
9734 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009735 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9736 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009737 new_dentry->d_name.name,
9738 new_dentry->d_name.len);
9739 if (!ret)
9740 ret = btrfs_update_inode(trans, dest, new_inode);
9741 }
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(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 new_dentry->d_name.name,
9749 new_dentry->d_name.len, 0, old_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
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009755 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 old_dentry->d_name.name,
9757 old_dentry->d_name.len, 0, new_idx);
9758 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009759 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009760 goto out_fail;
9761 }
9762
9763 if (old_inode->i_nlink == 1)
9764 BTRFS_I(old_inode)->dir_index = old_idx;
9765 if (new_inode->i_nlink == 1)
9766 BTRFS_I(new_inode)->dir_index = new_idx;
9767
Filipe Manana86e8aa02016-05-05 02:02:27 +01009768 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009770 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9771 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009772 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009773 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009775 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009777 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9778 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009780 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 }
9782out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009783 /*
9784 * If we have pinned a log and an error happened, we unpin tasks
9785 * trying to sync the log and force them to fallback to a transaction
9786 * commit if the log currently contains any of the inodes involved in
9787 * this rename operation (to ensure we do not persist a log with an
9788 * inconsistent state for any of these inodes or leading to any
9789 * inconsistencies when replayed). If the transaction was aborted, the
9790 * abortion reason is propagated to userspace when attempting to commit
9791 * the transaction. If the log does not contain any of these inodes, we
9792 * allow the tasks to sync it.
9793 */
9794 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009795 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9796 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9797 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009798 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009799 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009800 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009801
9802 if (root_log_pinned) {
9803 btrfs_end_log_trans(root);
9804 root_log_pinned = false;
9805 }
9806 if (dest_log_pinned) {
9807 btrfs_end_log_trans(dest);
9808 dest_log_pinned = false;
9809 }
9810 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009811 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812out_notrans:
9813 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009814 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009815 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009816 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817
9818 return ret;
9819}
9820
9821static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9822 struct btrfs_root *root,
9823 struct inode *dir,
9824 struct dentry *dentry)
9825{
9826 int ret;
9827 struct inode *inode;
9828 u64 objectid;
9829 u64 index;
9830
9831 ret = btrfs_find_free_ino(root, &objectid);
9832 if (ret)
9833 return ret;
9834
9835 inode = btrfs_new_inode(trans, root, dir,
9836 dentry->d_name.name,
9837 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009838 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 objectid,
9840 S_IFCHR | WHITEOUT_MODE,
9841 &index);
9842
9843 if (IS_ERR(inode)) {
9844 ret = PTR_ERR(inode);
9845 return ret;
9846 }
9847
9848 inode->i_op = &btrfs_special_inode_operations;
9849 init_special_inode(inode, inode->i_mode,
9850 WHITEOUT_DEV);
9851
9852 ret = btrfs_init_inode_security(trans, inode, dir,
9853 &dentry->d_name);
9854 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009855 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009856
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009857 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9858 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009859 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009860 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009861
9862 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009863out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009864 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009865 if (ret)
9866 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009867 iput(inode);
9868
Filipe Mananac9901612016-05-05 01:41:57 +01009869 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870}
9871
Chris Mason39279cc2007-06-12 06:35:45 -04009872static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009873 struct inode *new_dir, struct dentry *new_dentry,
9874 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009876 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009877 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009878 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009879 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9880 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009881 struct inode *new_inode = d_inode(new_dentry);
9882 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009883 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009884 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009885 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009886 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009887 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009888
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009889 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009890 return -EPERM;
9891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009893 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009894 return -EXDEV;
9895
Li Zefan33345d012011-04-20 10:31:50 +08009896 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009897 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009898 return -ENOTEMPTY;
9899
Chris Mason39279cc2007-06-12 06:35:45 -04009900 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009901 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009902 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009903
9904
9905 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009906 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009907 new_dentry->d_name.name,
9908 new_dentry->d_name.len);
9909
9910 if (ret) {
9911 if (ret == -EEXIST) {
9912 /* we shouldn't get
9913 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309914 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009915 return ret;
9916 }
9917 } else {
9918 /* maybe -EOVERFLOW */
9919 return ret;
9920 }
9921 }
9922 ret = 0;
9923
Chris Mason5a3f23d2009-03-31 13:27:11 -04009924 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009925 * we're using rename to replace one file with another. Start IO on it
9926 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009927 */
Chris Mason8d875f92014-08-12 10:47:42 -07009928 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009929 filemap_flush(old_inode->i_mapping);
9930
Yan, Zheng76dda932009-09-21 16:00:26 -04009931 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009932 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009933 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009934 /*
9935 * We want to reserve the absolute worst case amount of items. So if
9936 * both inodes are subvols and we need to unlink them then that would
9937 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009939 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9940 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009941 * If our rename has the whiteout flag, we need more 5 units for the
9942 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9943 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009944 */
Filipe Manana5062af32016-05-05 10:26:26 +01009945 trans_num_items = 11;
9946 if (flags & RENAME_WHITEOUT)
9947 trans_num_items += 5;
9948 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009949 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009950 ret = PTR_ERR(trans);
9951 goto out_notrans;
9952 }
Chris Mason5f39d392007-10-15 16:14:19 -04009953
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 if (dest != root)
9955 btrfs_record_root_in_trans(trans, dest);
9956
Nikolay Borisov877574e2017-02-20 13:50:33 +02009957 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009958 if (ret)
9959 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009960
Miao Xie67de1172013-12-26 13:07:06 +08009961 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009962 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009963 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009964 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009965 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009966 btrfs_pin_log_trans(root);
9967 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009968 ret = btrfs_insert_inode_ref(trans, dest,
9969 new_dentry->d_name.name,
9970 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009971 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009972 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009973 if (ret)
9974 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009975 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009976
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009977 inode_inc_iversion(old_dir);
9978 inode_inc_iversion(new_dir);
9979 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009980 old_dir->i_ctime = old_dir->i_mtime =
9981 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009982 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009983
Chris Mason12fcfd22009-03-24 10:24:20 -04009984 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009985 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9986 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009987
Li Zefan33345d012011-04-20 10:31:50 +08009988 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009989 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9990 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9991 old_dentry->d_name.name,
9992 old_dentry->d_name.len);
9993 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009994 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9995 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009996 old_dentry->d_name.name,
9997 old_dentry->d_name.len);
9998 if (!ret)
9999 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010000 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010001 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010002 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010003 goto out_fail;
10004 }
Chris Mason39279cc2007-06-12 06:35:45 -040010005
10006 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010007 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010008 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010009 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010010 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10011 root_objectid = BTRFS_I(new_inode)->location.objectid;
10012 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10013 root_objectid,
10014 new_dentry->d_name.name,
10015 new_dentry->d_name.len);
10016 BUG_ON(new_inode->i_nlink == 0);
10017 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010018 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10019 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010020 new_dentry->d_name.name,
10021 new_dentry->d_name.len);
10022 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010023 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010024 ret = btrfs_orphan_add(trans,
10025 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010026 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010027 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010028 goto out_fail;
10029 }
Chris Mason39279cc2007-06-12 06:35:45 -040010030 }
Josef Bacikaec74772008-07-24 12:12:38 -040010031
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010032 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010033 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010034 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010035 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010036 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010037 goto out_fail;
10038 }
Chris Mason39279cc2007-06-12 06:35:45 -040010039
Miao Xie67de1172013-12-26 13:07:06 +080010040 if (old_inode->i_nlink == 1)
10041 BTRFS_I(old_inode)->dir_index = index;
10042
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010043 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010044 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010045
David Sterbaf85b7372017-01-20 14:54:07 +010010046 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10047 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010048 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010049 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010050 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010051
10052 if (flags & RENAME_WHITEOUT) {
10053 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10054 old_dentry);
10055
10056 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010057 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010058 goto out_fail;
10059 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010060 }
Chris Mason39279cc2007-06-12 06:35:45 -040010061out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010062 /*
10063 * If we have pinned the log and an error happened, we unpin tasks
10064 * trying to sync the log and force them to fallback to a transaction
10065 * commit if the log currently contains any of the inodes involved in
10066 * this rename operation (to ensure we do not persist a log with an
10067 * inconsistent state for any of these inodes or leading to any
10068 * inconsistencies when replayed). If the transaction was aborted, the
10069 * abortion reason is propagated to userspace when attempting to commit
10070 * the transaction. If the log does not contain any of these inodes, we
10071 * allow the tasks to sync it.
10072 */
10073 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010074 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10075 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10076 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010077 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010078 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010079 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010080
10081 btrfs_end_log_trans(root);
10082 log_pinned = false;
10083 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010084 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010085out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010086 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010087 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010088
Chris Mason39279cc2007-06-12 06:35:45 -040010089 return ret;
10090}
10091
Miklos Szeredi80ace852014-07-23 15:15:32 +020010092static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10093 struct inode *new_dir, struct dentry *new_dentry,
10094 unsigned int flags)
10095{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010096 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010097 return -EINVAL;
10098
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010099 if (flags & RENAME_EXCHANGE)
10100 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10101 new_dentry);
10102
10103 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010104}
10105
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106static void btrfs_run_delalloc_work(struct btrfs_work *work)
10107{
10108 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010109 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110
10111 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10112 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010113 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010114 filemap_flush(inode->i_mapping);
10115 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10116 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010117 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010118
10119 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010120 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010121 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010122 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010123 complete(&delalloc_work->completion);
10124}
10125
10126struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010127 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010128{
10129 struct btrfs_delalloc_work *work;
10130
David Sterba100d5702015-12-08 14:39:32 +010010131 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132 if (!work)
10133 return NULL;
10134
10135 init_completion(&work->completion);
10136 INIT_LIST_HEAD(&work->list);
10137 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010139 WARN_ON_ONCE(!inode);
10140 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10141 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010142
10143 return work;
10144}
10145
10146void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10147{
10148 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010149 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010150}
10151
Chris Masond352ac62008-09-29 15:18:18 -040010152/*
10153 * some fairly slow code that needs optimization. This walks the list
10154 * of all the inodes with pending delalloc and forces them to disk.
10155 */
Miao Xie6c255e62014-03-06 13:55:01 +080010156static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10157 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010158{
Chris Masonea8c2812008-08-04 23:17:27 -040010159 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010160 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161 struct btrfs_delalloc_work *work, *next;
10162 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010163 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010165
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010167 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010168
Miao Xie573bfb72014-03-06 13:55:03 +080010169 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_lock(&root->delalloc_lock);
10171 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010172 while (!list_empty(&splice)) {
10173 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010174 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010175
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176 list_move_tail(&binode->delalloc_inodes,
10177 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010178 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010179 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010181 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010182 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010183 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010184
David Sterba651d4942015-11-27 19:24:16 +010010185 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010186 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010187 if (delay_iput)
10188 btrfs_add_delayed_iput(inode);
10189 else
10190 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010191 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010192 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010193 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010194 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010195 btrfs_queue_work(root->fs_info->flush_workers,
10196 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010197 ret++;
10198 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010199 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010200 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010201 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010202 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010204
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010205out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010206 list_for_each_entry_safe(work, next, &works, list) {
10207 list_del_init(&work->list);
10208 btrfs_wait_and_free_delalloc_work(work);
10209 }
10210
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 if (!list_empty_careful(&splice)) {
10212 spin_lock(&root->delalloc_lock);
10213 list_splice_tail(&splice, &root->delalloc_inodes);
10214 spin_unlock(&root->delalloc_lock);
10215 }
Miao Xie573bfb72014-03-06 13:55:03 +080010216 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 return ret;
10218}
10219
10220int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10221{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010222 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010223 int ret;
10224
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010225 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010226 return -EROFS;
10227
Miao Xie6c255e62014-03-06 13:55:01 +080010228 ret = __start_delalloc_inodes(root, delay_iput, -1);
10229 if (ret > 0)
10230 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010231 /*
10232 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010233 * we have to make sure the IO is actually started and that
10234 * ordered extents get created before we return
10235 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010236 atomic_inc(&fs_info->async_submit_draining);
10237 while (atomic_read(&fs_info->nr_async_submits) ||
10238 atomic_read(&fs_info->async_delalloc_pages)) {
10239 wait_event(fs_info->async_submit_wait,
10240 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10241 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010242 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010243 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010244 return ret;
10245}
10246
Miao Xie6c255e62014-03-06 13:55:01 +080010247int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10248 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249{
10250 struct btrfs_root *root;
10251 struct list_head splice;
10252 int ret;
10253
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010254 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 return -EROFS;
10256
10257 INIT_LIST_HEAD(&splice);
10258
Miao Xie573bfb72014-03-06 13:55:03 +080010259 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010260 spin_lock(&fs_info->delalloc_root_lock);
10261 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010262 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010263 root = list_first_entry(&splice, struct btrfs_root,
10264 delalloc_root);
10265 root = btrfs_grab_fs_root(root);
10266 BUG_ON(!root);
10267 list_move_tail(&root->delalloc_root,
10268 &fs_info->delalloc_roots);
10269 spin_unlock(&fs_info->delalloc_root_lock);
10270
Miao Xie6c255e62014-03-06 13:55:01 +080010271 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010272 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010273 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010274 goto out;
10275
Miao Xie6c255e62014-03-06 13:55:01 +080010276 if (nr != -1) {
10277 nr -= ret;
10278 WARN_ON(nr < 0);
10279 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010280 spin_lock(&fs_info->delalloc_root_lock);
10281 }
10282 spin_unlock(&fs_info->delalloc_root_lock);
10283
Miao Xie6c255e62014-03-06 13:55:01 +080010284 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010285 atomic_inc(&fs_info->async_submit_draining);
10286 while (atomic_read(&fs_info->nr_async_submits) ||
10287 atomic_read(&fs_info->async_delalloc_pages)) {
10288 wait_event(fs_info->async_submit_wait,
10289 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10290 atomic_read(&fs_info->async_delalloc_pages) == 0));
10291 }
10292 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010293out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010294 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010295 spin_lock(&fs_info->delalloc_root_lock);
10296 list_splice_tail(&splice, &fs_info->delalloc_roots);
10297 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010298 }
Miao Xie573bfb72014-03-06 13:55:03 +080010299 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010300 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010301}
10302
Chris Mason39279cc2007-06-12 06:35:45 -040010303static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10304 const char *symname)
10305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010306 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010307 struct btrfs_trans_handle *trans;
10308 struct btrfs_root *root = BTRFS_I(dir)->root;
10309 struct btrfs_path *path;
10310 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010311 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010312 int err;
10313 int drop_inode = 0;
10314 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010315 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010316 int name_len;
10317 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010318 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010319 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010320 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010321
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010322 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010323 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010324 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010325
Josef Bacik9ed74f22009-09-11 16:12:44 -040010326 /*
10327 * 2 items for inode item and ref
10328 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010329 * 1 item for updating parent inode item
10330 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010331 * 1 item for xattr if selinux is on
10332 */
Filipe Manana9269d122015-12-31 18:16:29 +000010333 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010334 if (IS_ERR(trans))
10335 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010336
Li Zefan581bb052011-04-20 10:06:11 +080010337 err = btrfs_find_free_ino(root, &objectid);
10338 if (err)
10339 goto out_unlock;
10340
Josef Bacikaec74772008-07-24 12:12:38 -040010341 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010342 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10343 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010344 if (IS_ERR(inode)) {
10345 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010346 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010347 }
Chris Mason39279cc2007-06-12 06:35:45 -040010348
Casey Schauflerad19db72011-12-15 10:09:07 -050010349 /*
10350 * If the active LSM wants to access the inode during
10351 * d_instantiate it needs these. Smack checks to see
10352 * if the filesystem supports xattrs by looking at the
10353 * ops vector.
10354 */
10355 inode->i_fop = &btrfs_file_operations;
10356 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010357 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010358 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10359
10360 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10361 if (err)
10362 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010363
Chris Mason39279cc2007-06-12 06:35:45 -040010364 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010365 if (!path) {
10366 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010367 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010368 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010369 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010370 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010371 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010372 datasize = btrfs_file_extent_calc_inline_size(name_len);
10373 err = btrfs_insert_empty_item(trans, root, path, &key,
10374 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010375 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010376 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010377 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010378 }
Chris Mason5f39d392007-10-15 16:14:19 -040010379 leaf = path->nodes[0];
10380 ei = btrfs_item_ptr(leaf, path->slots[0],
10381 struct btrfs_file_extent_item);
10382 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10383 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010384 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010385 btrfs_set_file_extent_encryption(leaf, ei, 0);
10386 btrfs_set_file_extent_compression(leaf, ei, 0);
10387 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10388 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10389
Chris Mason39279cc2007-06-12 06:35:45 -040010390 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010391 write_extent_buffer(leaf, symname, ptr, name_len);
10392 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010393 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010394
Chris Mason39279cc2007-06-12 06:35:45 -040010395 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010396 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010397 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010398 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010399 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010400 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010401 /*
10402 * Last step, add directory indexes for our symlink inode. This is the
10403 * last step to avoid extra cleanup of these indexes if an error happens
10404 * elsewhere above.
10405 */
10406 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010407 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10408 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010409 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010410 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010411 goto out_unlock_inode;
10412 }
10413
10414 unlock_new_inode(inode);
10415 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010416
10417out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010418 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010419 if (drop_inode) {
10420 inode_dec_link_count(inode);
10421 iput(inode);
10422 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010423 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010424 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010425
10426out_unlock_inode:
10427 drop_inode = 1;
10428 unlock_new_inode(inode);
10429 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010430}
Chris Mason16432982008-04-10 10:23:21 -040010431
Josef Bacik0af3d002010-06-21 14:48:16 -040010432static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10433 u64 start, u64 num_bytes, u64 min_size,
10434 loff_t actual_len, u64 *alloc_hint,
10435 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010437 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010438 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10439 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010440 struct btrfs_root *root = BTRFS_I(inode)->root;
10441 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010442 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010443 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010444 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010445 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010446 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010447 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010448 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010449
Josef Bacik0af3d002010-06-21 14:48:16 -040010450 if (trans)
10451 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010452 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010453 if (own_trans) {
10454 trans = btrfs_start_transaction(root, 3);
10455 if (IS_ERR(trans)) {
10456 ret = PTR_ERR(trans);
10457 break;
10458 }
Yan Zhengd899e052008-10-30 14:25:28 -040010459 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010460
Byongho Leeee221842015-12-15 01:42:10 +090010461 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010462 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010463 /*
10464 * If we are severely fragmented we could end up with really
10465 * small allocations, so if the allocator is returning small
10466 * chunks lets make its job easier by only searching for those
10467 * sized chunks.
10468 */
10469 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010470 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10471 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010472 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010473 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010474 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010475 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010476 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010477 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010478
Josef Bacik0b670dc2015-09-23 17:11:16 -040010479 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010480 ret = insert_reserved_file_extent(trans, inode,
10481 cur_offset, ins.objectid,
10482 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010483 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010484 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010485 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010486 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010487 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010488 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010489 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010490 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010491 break;
10492 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010493
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010494 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010495 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010496
Josef Bacik5dc562c2012-08-17 13:14:17 -040010497 em = alloc_extent_map();
10498 if (!em) {
10499 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10500 &BTRFS_I(inode)->runtime_flags);
10501 goto next;
10502 }
10503
10504 em->start = cur_offset;
10505 em->orig_start = cur_offset;
10506 em->len = ins.offset;
10507 em->block_start = ins.objectid;
10508 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010509 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010510 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010511 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010512 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10513 em->generation = trans->transid;
10514
10515 while (1) {
10516 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010517 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010518 write_unlock(&em_tree->lock);
10519 if (ret != -EEXIST)
10520 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010521 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010522 cur_offset + ins.offset - 1,
10523 0);
10524 }
10525 free_extent_map(em);
10526next:
Yan Zhengd899e052008-10-30 14:25:28 -040010527 num_bytes -= ins.offset;
10528 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010529 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010530
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010531 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010532 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010533 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010534 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010535 (actual_len > inode->i_size) &&
10536 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010537 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010538 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010539 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010540 i_size = cur_offset;
10541 i_size_write(inode, i_size);
10542 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010543 }
10544
Yan Zhengd899e052008-10-30 14:25:28 -040010545 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010546
10547 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010548 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010549 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010550 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010551 break;
10552 }
Yan Zhengd899e052008-10-30 14:25:28 -040010553
Josef Bacik0af3d002010-06-21 14:48:16 -040010554 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010555 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010556 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010557 if (cur_offset < end)
10558 btrfs_free_reserved_data_space(inode, cur_offset,
10559 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010560 return ret;
10561}
10562
Josef Bacik0af3d002010-06-21 14:48:16 -040010563int btrfs_prealloc_file_range(struct inode *inode, int mode,
10564 u64 start, u64 num_bytes, u64 min_size,
10565 loff_t actual_len, u64 *alloc_hint)
10566{
10567 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10568 min_size, actual_len, alloc_hint,
10569 NULL);
10570}
10571
10572int btrfs_prealloc_file_range_trans(struct inode *inode,
10573 struct btrfs_trans_handle *trans, int mode,
10574 u64 start, u64 num_bytes, u64 min_size,
10575 loff_t actual_len, u64 *alloc_hint)
10576{
10577 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10578 min_size, actual_len, alloc_hint, trans);
10579}
10580
Chris Masone6dcd2d2008-07-17 12:53:50 -040010581static int btrfs_set_page_dirty(struct page *page)
10582{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010583 return __set_page_dirty_nobuffers(page);
10584}
10585
Al Viro10556cb22011-06-20 19:28:19 -040010586static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010587{
Li Zefanb83cc962010-12-20 16:04:08 +080010588 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010589 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010590
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010591 if (mask & MAY_WRITE &&
10592 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10593 if (btrfs_root_readonly(root))
10594 return -EROFS;
10595 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10596 return -EACCES;
10597 }
Al Viro2830ba72011-06-20 19:16:29 -040010598 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010599}
Chris Mason39279cc2007-06-12 06:35:45 -040010600
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010601static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10602{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010603 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010604 struct btrfs_trans_handle *trans;
10605 struct btrfs_root *root = BTRFS_I(dir)->root;
10606 struct inode *inode = NULL;
10607 u64 objectid;
10608 u64 index;
10609 int ret = 0;
10610
10611 /*
10612 * 5 units required for adding orphan entry
10613 */
10614 trans = btrfs_start_transaction(root, 5);
10615 if (IS_ERR(trans))
10616 return PTR_ERR(trans);
10617
10618 ret = btrfs_find_free_ino(root, &objectid);
10619 if (ret)
10620 goto out;
10621
10622 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010623 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010624 if (IS_ERR(inode)) {
10625 ret = PTR_ERR(inode);
10626 inode = NULL;
10627 goto out;
10628 }
10629
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010630 inode->i_fop = &btrfs_file_operations;
10631 inode->i_op = &btrfs_file_inode_operations;
10632
10633 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010634 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10635
Chris Masonb0d5d102014-09-08 13:08:51 -070010636 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10637 if (ret)
10638 goto out_inode;
10639
10640 ret = btrfs_update_inode(trans, root, inode);
10641 if (ret)
10642 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010643 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010644 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010645 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010646
Filipe Manana5762b5c2014-08-01 00:10:32 +010010647 /*
10648 * We set number of links to 0 in btrfs_new_inode(), and here we set
10649 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10650 * through:
10651 *
10652 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10653 */
10654 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010655 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656 d_tmpfile(dentry, inode);
10657 mark_inode_dirty(inode);
10658
10659out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010660 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010661 if (ret)
10662 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010663 btrfs_balance_delayed_items(fs_info);
10664 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010665 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010666
10667out_inode:
10668 unlock_new_inode(inode);
10669 goto out;
10670
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010671}
10672
David Sterba20a7db82017-02-17 16:24:29 +010010673__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010674static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010675{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010676 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010677}
10678
Josef Bacikc6100a42017-05-05 11:57:13 -040010679static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10680{
10681 struct inode *inode = private_data;
10682 return btrfs_sb(inode->i_sb);
10683}
10684
10685static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10686 u64 start, u64 end)
10687{
10688 struct inode *inode = private_data;
10689 u64 isize;
10690
10691 isize = i_size_read(inode);
10692 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10693 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10694 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10695 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10696 }
10697}
10698
10699void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10700{
10701 struct inode *inode = private_data;
10702 unsigned long index = start >> PAGE_SHIFT;
10703 unsigned long end_index = end >> PAGE_SHIFT;
10704 struct page *page;
10705
10706 while (index <= end_index) {
10707 page = find_get_page(inode->i_mapping, index);
10708 ASSERT(page); /* Pages should be in the extent_io_tree */
10709 set_page_writeback(page);
10710 put_page(page);
10711 index++;
10712 }
10713}
10714
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010715static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010716 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010717 .lookup = btrfs_lookup,
10718 .create = btrfs_create,
10719 .unlink = btrfs_unlink,
10720 .link = btrfs_link,
10721 .mkdir = btrfs_mkdir,
10722 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010723 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010724 .symlink = btrfs_symlink,
10725 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010726 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010727 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010728 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010729 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010730 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010731 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010732 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010733};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010734static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010735 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010736 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010737 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010738};
Yan, Zheng76dda932009-09-21 16:00:26 -040010739
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010740static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010741 .llseek = generic_file_llseek,
10742 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010743 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010744 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010745#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010746 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010747#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010748 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010749 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010750};
10751
David Sterba20e55062015-11-19 11:42:28 +010010752static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010753 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010754 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010755 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010756 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010757 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010758 .tree_fs_info = iotree_fs_info,
10759 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010760
10761 /* optional callbacks */
10762 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010763 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010764 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010765 .set_bit_hook = btrfs_set_bit_hook,
10766 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010767 .merge_extent_hook = btrfs_merge_extent_hook,
10768 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010769 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010770};
10771
Chris Mason35054392009-01-21 13:11:13 -050010772/*
10773 * btrfs doesn't support the bmap operation because swapfiles
10774 * use bmap to make a mapping of extents in the file. They assume
10775 * these extents won't change over the life of the file and they
10776 * use the bmap result to do IO directly to the drive.
10777 *
10778 * the btrfs bmap call would return logical addresses that aren't
10779 * suitable for IO and they also will change frequently as COW
10780 * operations happen. So, swapfile + btrfs == corruption.
10781 *
10782 * For now we're avoiding this by dropping bmap.
10783 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010784static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010785 .readpage = btrfs_readpage,
10786 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010787 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010788 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010789 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010790 .invalidatepage = btrfs_invalidatepage,
10791 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010792 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010793 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010794};
10795
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010796static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010797 .readpage = btrfs_readpage,
10798 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010799 .invalidatepage = btrfs_invalidatepage,
10800 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010801};
10802
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010803static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010804 .getattr = btrfs_getattr,
10805 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010806 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010807 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010808 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010809 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010810 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010811 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010812};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010813static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010814 .getattr = btrfs_getattr,
10815 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010816 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010817 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010818 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010819 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010820 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010821};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010822static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010823 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010824 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010825 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010826 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010827 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010828 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010829};
Yan, Zheng76dda932009-09-21 16:00:26 -040010830
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010831const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010832 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010833 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010834};